Streaming
Pass stream: true to receive Server-Sent Events as tokens generate. Identical to the OpenAI SSE format — every chat-completion library has built-in support.
Enable streaming
stream = client.chat.completions.create( model="deepseek-v4-flash", messages=msgs, stream=True, stream_options={"include_usage": True}, ) for chunk in stream: delta = chunk.choices[0].delta.content or "" print(delta, end="", flush=True)
Event shape
Each event is a line: data: <JSON>\n\n. The stream terminates with data: [DONE]\n\n.
data: {"choices":[{"delta":{"role":"assistant"}}]}
data: {"choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[{"delta":{"content":", world"}}]}
data: {"choices":[{"finish_reason":"stop"}],"usage":{...}}
data: [DONE]
Token counts on the final chunk
Set stream_options.include_usage: true — the last non-DONE chunk carries the usage object so you can bill / log without an extra round-trip. Some reasoning models (qwen3, mimo-v2) emit reasoning tokens via a separate channel; we strip the channel by default so your delta.content stream stays clean.
Cancellation
Close the HTTP connection. We cancel the in-flight upstream request within ~1s and stop billing the rest. The partial output you already received is still billed.
Calling from a browser
The /v1/* surface accepts CORS preflights from https://app.ollima.com by default. To allow other origins (your own app), set OLLIMA_CORS_ORIGINS on the gateway env (self-hosted) or talk to us for the hosted plan. Note: shipping an sk-t0-… key in browser code is a security mistake; proxy the call from your server.