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 inresponse.output_text.delta events.
The stream ends with one event that carries the whole response:
response.completed: the reply is done.response.incomplete:max_output_tokensor a content filter cut the reply short.response.failed: the call failed. See Errors after the stream starts.
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 streamschat.completion.chunk objects and ends with data: [DONE]. The text arrives in choices[0].delta.content.
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: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 already200. A failure after that arrives in the body:
- Responses ends the stream with
response.failed. Itsresponse.errorhastype,codeandmessage. Itsresponse.outputholds the items so far. - Chat Completions sends a final
data:frame that carrieserrorinstead ofchoices, thendata: [DONE].
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.