cheapaimodel

Streaming

Receive tokens as they are generated with server-sent events.

Copy

Set "stream": true and the response arrives as server-sent events instead of one JSON body. Each format keeps its own event shape:

EndpointStream format
/v1/chat/completionsdata: lines of chat.completion.chunk objects, ending with data: [DONE]
/v1/responsesTyped events such as response.output_text.delta and response.completed
/v1/messagesAnthropic 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)
Copy
Open in

On this page