DocumentationStreaming
Ask AI

Take the docs with you

Ask ChatGPTOpen a new chat ↗Ask ClaudeOpen a new chat ↗Ask on GeminiCopy prompt, then paste in Gemini ↗View llms.txtDocumentation index ↗

External chats receive links to public docs.

Streaming

Only endpoints that intentionally differ from the standard API carry a chip; the chip links to the note that explains the difference.

Set stream: true for server-sent events in exactly OpenAI's format. Chunks arrive as data: lines and the stream ends with data: [DONE].

/v1/chat/completions
python
stream = client.chat.completions.create(
    model="cbcn/glm-5.3",
    messages=[{"role": "user", "content": "Count to five"}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content if chunk.choices else None
    if delta:
        print(delta, end="", flush=True)

Usage on the final frame

When stream_options is omitted, the gateway requests usage reporting. Providers may send usage before the final event, and interrupted or unsupported streams may omit it. Set stream_options.include_usage to true explicitly when your client needs usage frames; do not assume the last data frame always contains usage.

text
data: {"choices":[{"delta":{"content":"1"}}],"object":"chat.completion.chunk"}

data: {"choices":[],"usage":{"prompt_tokens":9,"completion_tokens":25,"total_tokens":34}}

data: [DONE]

Cancelling a stream

Disconnecting cancels the gateway request. How quickly the provider stops and reports usage depends on the upstream. An interrupted request can be billed for processed tokens; check its settled usage row.

If you sit behind a reverse proxy, disable response buffering for this endpoint or chunks will be held back and appear to arrive all at once. We send X-Accel-Buffering: no to help nginx do the right thing.
Retry only what is transient. Retry 429, 502 and 503 with exponential backoff. 402 means your balance is empty, so top up instead of retrying. Never retry 400, 401, 403 or 404.
Interrupted stream billing. An explicit upstream stream error before output is refunded. Partial responses can be charged for processed tokens. Use the settled usage log rather than estimating cost from visible text.

Anthropic streaming

Messages uses named events and ends with message_stop, rather than OpenAI's [DONE] marker. Use the native SDK parser for this protocol.

python
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ATMOROUTER_API_KEY"],
                   base_url="https://atmorouter.dev")
with client.messages.stream(
    model="YOUR_MODEL_ID", max_tokens=256,
    messages=[{"role":"user", "content":"Count to five"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
text
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}

event: message_stop
data: {"type":"message_stop"}

Partial output and errors after HTTP 200

HTTP headers may already have been sent when an upstream fails. Inspect OpenAI error frames or Anthropic event: error, and detect a missing terminal event. Do not interpret HTTP 200 alone as a complete answer. Replaying after output can duplicate generated text, tool actions, and charges.

A network chunk is not an SSE event: JSON, tool arguments, and UTF-8 characters can span multiple chunks. Buffer complete events or use a protocol-aware SDK. Reasoning, keepalive, and metadata may arrive before the first answer text; see Usage & request timing.