Streaming
Step-by-step answers with server-sent events, and what happens when a provider fails midway.
Send "stream": true on any of the three endpoints to receive the answer as server-sent events.
stream = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="")
Behaviour
- The gateway forwards each part as soon as it arrives from the provider, without holding it back.
- On
/v1/chat/completions,stream_options.include_usagemakes the last part carryusage. Turn it on if you want to count usage yourself. - The bill is computed from the actual usage whether or not
include_usageis sent. - If the first part from the provider is late and no byte has reached you yet, the gateway cancels that attempt and tries another provider. After the first byte is sent there is no switch.
- If a provider closes the stream without a finish signal and without usage figures, the stream ends. Output is billed for the text already received, and input is billed at the cache-read price. The same rule applies on
/v1/chat/completions,/v1/messagesand/v1/responses. - If your connection drops, the gateway keeps reading the provider’s answer for up to 60 seconds so the bill uses the real usage.
- If every provider fails before any byte is sent, you receive
upstream_unavailable(503) orupstream_timeout(504).
Time to first token and tokens per second of every request show on the Usage page.