Skip to main content
MossInference is the JavaScript client for Moss Inference. Its responses.create takes OpenAI’s Responses arguments plus indexes, searches those indexes, and returns OpenAI’s shapes.

Install

MossInference needs @moss-js/moss 1.22.0 or later. Earlier versions have chat.completions.create instead. See the changelog. For the rest of the SDK setup, see Install.

Create a client

Copy the project id and key from the Moss portal. Build one client and reuse it for every call. It has nothing to close. The examples below use this client. With MOSS_PROJECT_ID and MOSS_PROJECT_KEY set, import { inference } from '@moss-js/moss' gives you inference.responses.create with no client to build.

Get a response

response.output_text joins the text of the response’s messages. response.output holds its items. response.requestId is the response’s X-Moss-Request-Id. A stream has the same stream.requestId. Include it when you report a problem. Both are undefined when the response has none. Moss stores no response, so input holds the whole conversation. Pass a string, or a list of items such as { role: 'user', content: 'Hi' } and the output items of earlier responses. See Conversations.

Stream a response

Each event is the JSON Moss Inference sent, with OpenAI’s type, such as response.output_text.delta. The stream ends after one of two events:
  • response.completed, whose event.response is the whole response.
  • response.incomplete, when max_output_tokens or a content filter cut the response short.
await stream.finalResponse() reads the rest of the stream and returns that response. To stop early, leave the loop or call stream.controller.abort(). Either one closes the connection, which stops the model, and neither throws. Streaming lists every event.

Ground it in your indexes

Moss searches the faq index and adds what it finds to the prompt. The Quickstart creates that index. Without indexes, Moss does not search and the model answers from input alone. You can name at most 32. retrieve: false skips the search on a call that names indexes. Ground answers in your indexes covers the options and the cost.

Use a vendor model

Name a model from OpenAI, Anthropic or Google in model. The search of your indexes works the same for every model. Models lists them all.

On the Moss key

Without your own key, a listed vendor model runs on the Moss key and bills your credits. Routes shows who bills each call. A listed model that is not served right now throws InferenceError with errorCode 'model_not_enabled'. Send moss/fastlane-1 or use your own key.

With your own key

Pass your OpenAI, Anthropic or Google key to a client as apiKey:
The key goes in the X-Moss-Provider-Key header, never in the body. To use a key for one call only, pass { apiKey } as the second argument of responses.create. The response’s model is the name you sent. Bring your own key covers model names and billing.

Tool calls

tools, tool_choice and parallel_tool_calls take OpenAI’s Responses shapes, so a function tool is flat. Each call the model makes comes as a function_call item of output. Push the output items onto input unchanged. Add one function_call_output item per call, and call again until the response has no calls:
lookupOrder stands for your own code. Push the items unchanged to keep every field the model sent. Gemini, for one, needs its thought signature in the next turn. finalResponse() also works without the for await loop. Without stream: true, create returns the response directly. Tool calling covers tool_choice, parallel_tool_calls and structured output.

MCP servers

An McpServer entry in tools names an MCP server. Moss Inference calls its tools for the model during the response, so your code sends no function_call_output items for them.
Each call comes as an mcp_call item. It arrives in response.output_item.added when it starts, and in response.output_item.done when it ends. The same items are in the response’s output. Send the items back in input with the rest of output. The model then sees the earlier tool results. MCP failures throw InferenceError with status, type, errorCode and retryAfter. A call that fails after a tool ran has errorCode 'failed_after_tool_call'. The tool may already have changed data, so Moss Inference does not retry the call. Never replay a request that failed with failed_after_tool_call. MCP servers covers the server fields and every MCP error.

Errors

Every failure other than an abort throws InferenceError. Once a stream has started, it throws from the for await loop. A response.failed event throws it too. A field the failure lacks is undefined. The error also has a Moss code, so isMossError accepts it. The SDK sets these types itself, with status undefined: Every other error carries what Moss Inference sent. Errors and limits lists every code. For a 429, see Limits.

Options

new MossInference(projectId, projectKey, options?): responses.create(params, options?) takes OpenAI’s fields and the Moss fields in params. Settings for this call only go in options: