Skip to main content
Set stream to true and the reply arrives as server-sent events while the model writes it. This page covers the events of each API, stopping early, errors after the stream starts and where usage arrives.

Stream a response

Responses streams typed events. The text arrives in response.output_text.delta events.
Moss sends only these event types, in OpenAI’s streaming event shapes: The stream ends with one event that carries the whole response:
  • response.completed: the reply is done.
  • response.incomplete: max_output_tokens or a content filter cut the reply short.
  • response.failed: the call failed. See Errors after the stream starts.
Responses sends no data: [DONE]. In the SDKs, await stream.get_final_response() in Python and await stream.finalResponse() in JavaScript read the rest and return that response.

Stream Chat Completions

Chat Completions streams chat.completion.chunk objects and ends with data: [DONE]. The text arrives in choices[0].delta.content.
On the wire, the stream looks like this:
The last chunk before data: [DONE] has empty choices and the whole reply’s usage. Check choices before you read choices[0].

Stop early

In the Moss SDKs, leave the loop to stop. That closes the connection, which stops the model. This example stops after the first sentence:
In Python, break leaves the async with stream: block, which closes the connection. JavaScript can also stop with stream.controller.abort(). In JavaScript, neither way throws.

Errors after the stream starts

Once the first event is sent, the status is already 200. A failure after that arrives in the body:
  • Responses ends the stream with response.failed. Its response.error has type, code and message. Its response.output holds the items so far.
  • Chat Completions sends a final data: frame that carries error instead of choices, then data: [DONE].
Check the body of every stream for error: the status is already 200. The Moss SDKs check for you. They raise their inference error from the loop, and a response.failed event raises it too. A stream that ends before its last event raises it with type incomplete_stream. The SDKs retry only before the response starts, so a failure mid-stream is not retried. If the stream fails after an MCP call ran, error.code is failed_after_tool_call. Do not replay that request: the tool may already have changed data. See MCP servers. Every code is in Errors and limits.

Read usage on a stream

Both counts include the retrieved facts. On moss/fastlane-1, the Chat Completions field usage.prompt_tokens_details.cached_tokens is the part of the prompt held in the prefix cache. See Pricing.