stream: true in your request body.
- Python (OpenAI)
- Node.js (OpenAI)
- Go
- cURL
- fetch (raw)
Anthropic Streaming
The Anthropic SDK uses its own streaming format:Gemini Streaming
Gemini usesstreamGenerateContent instead of generateContent:
SSE Format
Under the hood, streaming uses Server-Sent Events. Each content event looks like:[DONE] contains usage data with an empty choices array:
ARouter may occasionally send SSE comments (lines starting with
:) to prevent connection timeouts. These can be safely ignored per the SSE specification.Recommended SSE Client Libraries
Some SSE client implementations may not parse the payload correctly. We recommend:- eventsource-parser — lightweight SSE parser
- OpenAI SDK — handles SSE, tool calls, and usage automatically
- Vercel AI SDK — React/Next.js streaming helpers
Stream Cancellation
Streaming requests can be cancelled by aborting the connection. For supported providers, this immediately stops model processing.- Node.js (AbortController)
- Python
- fetch (AbortController)
Handling Errors During Streaming
ARouter handles errors differently depending on when they occur during the streaming process.Errors Before Any Tokens Are Sent
If an error occurs before any tokens have been streamed, ARouter returns a standard JSON error response with the appropriate HTTP status code:Errors After Tokens Have Been Sent (Mid-Stream)
If an error occurs after some tokens have already been streamed, ARouter cannot change the HTTP status code (which is already 200 OK). Instead, the error is sent as an SSE event:- The error appears at the top level alongside standard response fields
- A
choicesarray is included withfinish_reason: "error"to terminate the stream - The HTTP status remains 200 OK since headers were already sent
Error Handling Code Examples
- Python (OpenAI)
- Node.js (OpenAI)
- fetch (raw)