Stream responses token-by-token using Server-Sent Events (SSE).
Enable Streaming
{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Write a poem"}],
"stream": true
}
Response Format
Each chunk is a data: line:
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":" world"},"index":0}]}
data: [DONE]
Python Example
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.boundlessapi.com/v1",
)
stream = client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Count to 5"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
TypeScript Example
const stream = await client.chat.completions.create({
model: "claude-sonnet-4-6",
messages: [{ role: "user", content: "Count to 5" }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
Billing
Streaming requests are billed on total tokens when the stream completes, same as non-streaming.
If stream is interrupted client-side, you are billed for tokens generated up to cancellation.
Errors During Stream
Errors may appear as:
data: {"error": {"message": "...", "code": "..."}}
Handle in your stream parser. See Error Codes.