Skip to content
WRAITHERIXDocsENRU

Streaming

Set stream: true to receive the answer incrementally as server-sent events. Streaming works on every endpoint: Chat Completions, Responses, Anthropic Messages and Gemini.

stream = client.chat.completions.create(
model="gpt-5.6-luna",
messages=[{"role": "user", "content": "Count from 1 to 5."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print("\n", chunk.usage)

Each event is a data: line with a chat.completion.chunk object. The stream ends with data: [DONE]:

data: {"id":"resp_0bcb…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"1"}}]}
data: {"id":"resp_0bcb…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":", 2"}}]}
data: {"id":"resp_0bcb…","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":14,"completion_tokens":9,"total_tokens":23}}
data: [DONE]

Add "stream_options": {"include_usage": true} to receive a final chunk with token usage.

  • Use curl -N (no buffering) when testing from the terminal.
  • If you proxy the stream through your own server, disable response buffering (e.g. X-Accel-Buffering: no on nginx).
  • A stream that is interrupted by the client is billed only for the tokens generated so far.