Streaming
Read streamed answers, final usage, and errors over server-sent events.
Request a stream
Set stream: true and request usage information. With an existing configured Python client:
stream = client.chat.completions.create(
model="deepseek/deepseek-flash:cyberrouter",
messages=[{"role": "user", "content": "Summarize this maintenance note: the backup completed."}],
max_completion_tokens=256,
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="")
if not chunk.choices and chunk.usage:
print(chunk.usage.model_extra.get("cost"))Read the event sequence
Ignore SSE comment frames beginning with :. Content events carry choice deltas. The terminal usage event has choices: [], followed by data: [DONE]. Do not assume every chunk has a choice.
: keepalive
data: {"id":"gen-example","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"gen-example","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"gen-example","choices":[],"usage":{"prompt_tokens":100,"completion_tokens":20,"total_tokens":120,"cost":0.0001}}
data: [DONE]Enhanced responses are buffered before SSE delivery, so receiving an SSE response does not imply tokens arrived live from the provider. The enhanced stream’s final usage event also carries its enhancement metadata.
Handle interrupted streams
After response bytes are sent, HTTP status can no longer change. A failure may arrive as an event containing error and finish_reason: "error". Continue reading for the final usage event when the connection remains available.
A network disconnect can prevent you from receiving that final event. Check the request receipt or Usage page before retrying: provider work may have already incurred a charge.