Streaming
Stream tokens as Server-Sent Events with the same protocol as OpenAI, including final usage counts.
Set stream: true and tokens arrive as Server-Sent Events. The protocol is the same as OpenAI's, so an existing stream handler works unchanged.
Stream a response#
stream = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Count to ten."}],
stream=True,
)
for event in stream:
delta = event.choices[0].delta.content
if delta:
print(delta, end="", flush=True)The stream ends with a data: [DONE] line, exactly like OpenAI.
Get token counts while streaming#
Add "stream_options": {"include_usage": true} to the request body to get a final chunk with the token counts for the whole request.
stream = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Count to ten."}],
stream=True,
stream_options={"include_usage": True},
)Streaming and spend limits#
Budgets are checked when a request starts, not while it runs. A stream in progress is never cut off, so a request that starts just under a limit can finish and push usage slightly over it. The next request is refused.
Reasoning models#
Models that return their thinking send it separately from the answer in the stream. See Reasoning & tracing.