Streaming Responses
RouteAPI streaming output uses Server-Sent Events (SSE). After setting stream: true in the request, the server returns incremental content step by step.
Request example
Section titled “Request example”curl https://www.routeapi.ai/v1/chat/completions \ -H "Authorization: Bearer $ROUTEAPI_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o", "stream": true, "messages": [ { "role": "user", "content": "Explain streaming output step by step" } ] }'SSE fragment example
Section titled “SSE fragment example”data: {"id":"chatcmpl_xxx","choices":[{"delta":{"content":"RouteAPI"}}]}
data: {"id":"chatcmpl_xxx","choices":[{"delta":{"content":" supports"}}]}
data: [DONE]Clients should read data: line by line and end the response after receiving [DONE].
usage return
Section titled “usage return”If the current model supports it, streaming requests can ask for final usage through stream_options:
{ "stream": true, "stream_options": { "include_usage": true }}Support for this field depends on the selected model and service capability.
Mid-stream errors
Section titled “Mid-stream errors”Streaming requests may encounter service-side errors after the connection is established, such as:
- Model service timeout
- Insufficient account quota
- Network interruption
- Client closes the connection actively
- Tool call parameter generation failure
Recommended client behavior:
- Record a request id for every request.
- Retry network interruptions only with limits.
- Avoid automatically retrying tool calls that have already produced side effects.
- Use console usage logs to troubleshoot service-side errors.