Guides
Streaming
Receive the response token by token as Server-Sent Events, with token usage on the final chunk.
Set "stream": true to receive the answer as it is generated. The response is a stream of Server-Sent Events (text/event-stream) in the OpenAI format: each event is a chat.completion.chunk, and the stream ends with data: [DONE].
Stream a response
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.larasrouter.com/v1",
api_key=os.environ["LARAS_API_KEY"],
)
stream = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": "Write a haiku about Jakarta rain."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print(f"\n\n{chunk.usage.total_tokens} tokens")
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.larasrouter.com/v1",
apiKey: process.env.LARAS_API_KEY,
});
const stream = await client.chat.completions.create({
model: "deepseek-flash",
messages: [{ role: "user", content: "Write a haiku about Jakarta rain." }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content;
if (text) process.stdout.write(text);
if (chunk.usage) console.log(`\n\n${chunk.usage.total_tokens} tokens`);
}
curl https://api.larasrouter.com/v1/chat/completions \
-H "Authorization: Bearer $LARAS_API_KEY" \
-H "Content-Type: application/json" \
--no-buffer \
-d '{
"model": "deepseek-flash",
"messages": [{"role": "user", "content": "Write a haiku about Jakarta rain."}],
"stream": true,
"stream_options": {"include_usage": true}
}'
What the stream looks like
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Grey sky"}}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":16,"completion_tokens":42,"total_tokens":58,"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":16}}
data: [DONE]
- Reasoning arrives first, in
delta.reasoning_content, before the answer'sdelta.content. See Reasoning content. - Tool calls arrive as
delta.tool_callsfragments that you concatenate byindex. See Tool calling.
Token usage
With stream_options.include_usage set to true, the last chunk before [DONE] carries the usage object and an empty choices array. Without it, you receive no usage chunk — your request is still metered and charged the same way, and you can see it in the console under Usage.
How a stream ends
- Normally: a chunk with a
finish_reason, then (if requested) the usage chunk, thendata: [DONE]. We send[DONE]only after the request's usage has been recorded, so a stream you saw finish is a stream we billed exactly. - With an error mid-stream: if the provider fails after the stream has started, the stream ends with an error event in the usual envelope, for example
data: {"error":{"message":"…","type":"api_error","code":"provider_unavailable","param":null}}. Treat a stream that ends without[DONE]as incomplete. - Errors before the stream starts — an invalid key, an empty wallet, a rate limit — are returned as a normal JSON error response with the matching HTTP status, not as a stream. See Errors & retries.