Streaming
Stream model output as it's generated. Sub-second time-to-first-token, full guardrail metadata in the final event, and graceful mid-stream error handling.
Enable streaming
Set stream: true on the request body. The response uses Server-Sent Events with Content-Type: text/event-stream. The OpenAI and Anthropic SDKs handle the SSE parsing automatically.
Python — OpenAI SDK
TypeScript — OpenAI SDK
cURL
Edge proxy note. Our edge proxies forward SSE frames as they arrive — each chunk resets the proxy timeout, so streaming requests survive arbitrarily long completions. Non-streaming requests are subject to a 100-second proxy timeout; use streaming for long-thinking models (e.g., R1, Opus).
Three endpoints support streaming
Pass stream:true on any of these endpoints and the gateway responds with Server-Sent Events. The full guardrail + metering pipeline runs identically on every path.
POST /v1/chat/completions— the OpenAI-compatible chat endpoint. Chunks follow OpenAI'schat.completion.chunkformat (shown above).POST /anthropic/v1/messages— the Anthropic Messages endpoint. Frames follow the Anthropic event format (message_start → content_block_delta → message_stop) instead of OpenAI chunks. See /docs/endpoints/anthropic-messages.POST /anoman/v1/playground/run— the dashboard/studio playground endpoint that powers the 3-pane compare tool.
Chunk anatomy
Each frame is a single line: data: <json> followed by a blank line. The stream is terminated with data: [DONE]. Chunks match OpenAI's chat.completion.chunk shape, except the second-to-last frame is the Anoman metadata block.
Wire format — example chunks
- First chunk:
delta.role: "assistant"with empty content. - Intermediate chunks:
delta.contenttoken by token. - Last completion chunk:
finish_reasonpopulated +usagetotals. - Penultimate frame:
{"_anoman": {...}}— guardrail results, routing region, cache hit, cost, weighted tokens. - Terminator:
data: [DONE].
When upstream breaks halfway
Pre-call failures (auth, rate limit, guardrail block) return a normal HTTP error before the SSE response opens. Once streaming has started, the HTTP status is already 200 — so mid-stream upstream failures are signaled by an error frame in the stream body:
Mid-stream error frame
Your client should check every parsed chunk for an error key. When present, treat the response as failed (don't commit the partial output) and apply your normal retry logic. The OpenAI SDK raises an exception automatically when it sees this frame.
Partial content note. The
_anoman.completion_tokensfield tells you how many completion tokens the model emitted before the failure. Useful for telemetry — and for deciding whether a partial result is salvageable in your UX.
Without an SDK
When you can't use the OpenAI/Anthropic SDK — edge runtimes, alternative HTTP clients, custom proxies — here's a minimal SSE parser:
TypeScript — raw fetch (no SDK)
Stopping a stream early
Closing the HTTP connection cancels the upstream call within ~50 ms. Tokens generated before cancellation are still billed; the dashboard logs the cancellation as routing_mode: streaming_cancelled.
Python — cancel mid-stream
TypeScript — cancel via AbortController
Identical to non-streaming
Streaming changes only how bytes reach you — not what the gateway does around the call. Every request runs through the same pipeline as a non-streaming request.
- Guardrails run before the first token. Pre-call checks (injection, PII, content moderation, policy) execute before the stream opens. A blocked request never starts streaming — it returns a normal HTTP error (e.g. 403), not an SSE body.
- Metering is recorded at stream close. Token usage, weighted tokens, cost, and the trace come from the provider's final usage — the same numbers as a non-streaming call. A cancelled stream still meters the partial usage.
- Errors leak nothing. A mid-stream provider failure emits a generic error frame (
upstream_error) with no internal detail — the raw provider message stays in server logs only.
Known limitations
- Batch routing — streaming requests always use the real-time path. Batch is for non-interactive workloads where 5–30 minute SLA is acceptable in exchange for ~50% cost.
- MCP per-tool RBAC — currently enforced on JSON responses only. If your workflow needs strict per-tool denial, call with
stream: falseuntil streaming RBAC ships. - Response cache writes happen at stream close, not per-chunk. Response-cache hits replay as a normal SSE stream.
Try streaming in the dashboard playground
3-pane compare with streaming output side-by-side.