Streaming
SSE responses, usage chunks, and differences between the two protocols.
Add stream: true to a request for any model to receive incremental SSE (Server-Sent Events) output and reduce the wait for the first token.
OpenAI protocol
stream = client.chat.completions.create(
model="z-ai/glm-5.2",
messages=[{"role": "user", "content": "Write a short poem about computing power"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="")Reasoning models return incremental reasoning in delta.reasoning_content and answer text in delta.content.
Get usage in a stream
Streaming responses do not include usage by default. Add stream_options to receive complete usage in the final data chunk, whose choices is an empty array:
{"stream": true, "stream_options": {"include_usage": true}}Anthropic protocol
The Messages endpoint follows the Anthropic SSE sequence: message_start → content_block_start → content_block_delta (text_delta / thinking_delta / input_json_delta) → content_block_stop → message_delta (including stop_reason and output token counts) → message_stop.
Use client.messages.stream(...) from the official Anthropic SDK directly.
Recommendations
- Enable streaming for long outputs and complex reasoning to avoid client or intermediate proxy read timeouts.
- Streaming and non-streaming requests are billed the same way, using the number of tokens actually generated.
- An upstream error during generation terminates the stream. Treat early termination without
finish_reason/message_stopas a failure and retry.