- Generating the complete output takes a relatively long time.
- The first tokens are useful without the rest of the output.
- Reducing the time to first token improves the user experience.
Enable streaming
On OpenAI-compatible endpoints, setstream to true and read the response as it arrives. For custom Truss models, your model code defines the input fields and yields the response chunks.
[DONE]. The Anthropic example iterates events because text_stream hides message_stop. Both examples raise an error if the stream ends without its completion event or stop reason. Transport and API errors also propagate to the caller. Printed text is partial until these checks pass.
A completion event can still accompany an output token limit. Check the printed stop reason before accepting the answer. These examples cover Chat Completions and Messages; the Responses API uses different events.
Streaming changes when the caller sees output, not how much the model produces. The following diagram puts both delivery modes on one clock.
The top lane streams: after a short prefill, tokens fill in one at a time from the first-token mark (TTFT). The bottom lane is non-streaming: it stays empty through the same generation, then the whole response lands at once at the end. Both finish together, so the only difference is when the caller first sees output. Token timing here is illustrative, not a measured latency.
Handle incomplete responses
For Model API requests using Chat Completions or Messages, check the stream-end marker and the reason generation stopped before accepting the result. A response can reach its token limit or lose the connection after returning HTTP200. See completion markers and recovery examples.
Set a read timeout that allows for the expected wait before output and between chunks. Use bounded retries for transient failures. Retrying starts a new generation, so keep partial output separate from the new response.