Guides · curl · Python · JavaScript · 5 min
Stream a response
Streaming sends the response as it is generated, so your interface can show words as they arrive instead of waiting for the whole answer.
Ask for a stream
Set stream to true. The response is a stream of server-sent events. Each event is a chat.completion.chunk object, and the stream ends with data: [DONE].
curl -N https://api.useunbound.ai/v1/chat/completions \
-H "Authorization: Bearer $UNBOUND_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<model-id>",
"stream": true,
"messages": [{ "role": "user", "content": "Explain tides in two sentences." }]
}'
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Tides are"},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
With the SDKs
from openai import OpenAI
client = OpenAI(base_url="https://api.useunbound.ai/v1", api_key="<your Unbound key>")
stream = client.chat.completions.create(
model="<model-id>",
messages=[{"role": "user", "content": "Explain tides in two sentences."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
elif chunk.usage:
print(f"\n{chunk.usage.total_tokens} tokens")
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.useunbound.ai/v1", apiKey: process.env.UNBOUND_API_KEY });
const stream = await client.chat.completions.create({
model: "<model-id>",
messages: [{ role: "user", content: "Explain tides in two sentences." }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
if (chunk.usage) console.log(`\n${chunk.usage.total_tokens} tokens`);
}
Token usage
Usage is not part of a stream unless you ask for it. With stream_options.include_usage set, one extra chunk arrives after the last content chunk. Its choices array is empty and its usage object carries the token counts that were billed.
Reasoning and tool calls
Models that return reasoning text send it in delta.reasoning_content, separate from delta.content. Tool calls arrive in delta.tool_calls as fragments: the first fragment for an index carries the call id and function name, and later fragments append to function.arguments.
Stopping early
Close the connection to stop generation. The request is settled with what was produced up to that point: you pay for the prompt and the output already delivered, and the rest of the hold is returned.
Errors during a stream
If a request fails before any output is sent, you get a normal HTTP error with the usual JSON envelope. Once a stream has started the status code is already 200, so a later failure arrives as a final event:
data: {"error":{"message":"Stream interrupted. You were not charged.","type":"server_error","code":"server_error"}}
Handle both cases. A stream that ends without data: [DONE] did not complete.