Streaming
Receive tokens as they are generated with server-sent events.
Set "stream": true and the response arrives as server-sent events instead of one JSON body. Each format keeps its own event shape:
| Endpoint | Stream format |
|---|---|
/v1/chat/completions | data: lines of chat.completion.chunk objects, ending with data: [DONE] |
/v1/responses | Typed events such as response.output_text.delta and response.completed |
/v1/messages | Anthropic events: message_start, content_block_delta, message_stop |
Token usage in streams
For chat completions, add "stream_options": { "include_usage": true } to get a final chunk with token counts. The Responses and Messages streams report usage in their closing events.
Example
stream = client.chat.completions.create(
model="deepseek-v4-1-flash",
messages=[{"role": "user", "content": "Write a haiku about rivers."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)