Set "stream": true on any of the three formats to receive the answer as server-sent events
(text/event-stream). Events are passed on as the model produces them, in the format you called.
OpenAI format
curl -N https://api.yunshurouter.com/v1/chat/completions \
-H "Authorization: Bearer $YUNSHU_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"stream": true,
"stream_options": {"include_usage": true},
"messages": [{"role": "user", "content": "Count to five."}]
}'
from openai import OpenAI
client = OpenAI(base_url="https://api.yunshurouter.com/v1", api_key="tq-...")
stream = client.chat.completions.create(
model="deepseek-v4-pro",
stream=True,
stream_options={"include_usage": True},
messages=[{"role": "user", "content": "Count to five."}],
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
elif chunk.usage:
print("\n", chunk.usage)
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.yunshurouter.com/v1", apiKey: "tq-..." });
const stream = await client.chat.completions.create({
model: "deepseek-v4-pro",
stream: true,
stream_options: { include_usage: true },
messages: [{ role: "user", content: "Count to five." }],
});
for await (const chunk of stream) {
if (chunk.choices.length) process.stdout.write(chunk.choices[0].delta.content ?? "");
else if (chunk.usage) console.log("\n", chunk.usage);
}
What you receive:
chat.completion.chunkevents withdeltacontent, tool-call pieces and, for reasoning models, reasoning text.- A usage chunk only if you ask for it with
stream_options.include_usage: true. It comes last, with an emptychoiceslist. Code that readschunk.choices[0]without checking must not turn this on. data: [DONE]at the end of a stream that finished normally, even if the model did not send one.
Anthropic format
curl -N https://api.yunshurouter.com/v1/messages \
-H "x-api-key: $YUNSHU_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"max_tokens": 512,
"stream": true,
"messages": [{"role": "user", "content": "Count to five."}]
}'
import anthropic
client = anthropic.Anthropic(base_url="https://api.yunshurouter.com", api_key="tq-...")
with client.messages.stream(
model="deepseek-v4-pro",
max_tokens=512,
messages=[{"role": "user", "content": "Count to five."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n", stream.get_final_message().usage)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://api.yunshurouter.com", apiKey: "tq-..." });
const stream = client.messages.stream({
model: "deepseek-v4-pro",
max_tokens: 512,
messages: [{ role: "user", content: "Count to five." }],
});
stream.on("text", (text) => process.stdout.write(text));
const final = await stream.finalMessage();
console.log("\n", final.usage);
You receive the Anthropic event sequence: message_start, then content_block_start,
content_block_delta and content_block_stop for each block, then message_delta with the
final usage, and message_stop. Usage is always included on this format.
Responses format
POST /v1/responses with stream: true sends Responses events, ending with
response.completed or response.incomplete. See Responses API.
Headers on a stream
The headers arrive before the first event, so you can read them at once:
| Header | Value |
|---|---|
x-yunshu-request-id |
The request id. Look up the cost afterwards with GET / |
x-yunshu-model |
The model that is answering |
x-yunshu-cache |
hit or miss |
with client.chat.completions.with_streaming_response.create(
model="deepseek-v4-pro", stream=True,
messages=[{"role": "user", "content": "Hi"}],
) as response:
request_id = response.headers.get("x-yunshu-request-id")
for chunk in response.parse():
...
The cost of a stream is known only when it ends, and it is not sent in the stream. Look it up with the request id. See Cost and usage.
When a stream fails part-way
Once the first event is sent, the HTTP status is already 200. If the model fails after that,
you receive an error event in the format you called, not a stream that simply stops. A stream
that ends without one of these is complete.
| Format | What you receive |
|---|---|
| OpenAI | A data: event with an error object: data: {"error": {"type": "upstream_. The OpenAI SDKs raise an error on it |
| Anthropic | event: error with {"type": "error", "error": {"type": "api_. The Anthropic SDKs raise an error on it |
| Responses | response.failed, with error.code and error.message |
A stream that fails part-way is not charged. A stream that completes is billed on the usage the
model reports; if it reports none, on the text actually returned, never on the max_tokens
ceiling.
Errors before the first event (bad key, empty balance, unknown model) are ordinary JSON error responses with a real status code. See Errors.
Fallbacks and streams
A stream can move to a fallback model only before its first event. After that, a failure is reported as above. See Fallbacks and combos.
Cache hits
An answer served from our cache is streamed too, with tool calls and the stop reason exactly as
first returned. x-yunshu-cache is hit, and it is not charged.