Streaming
Server-sent events, disconnects and proxy buffering.
Set stream: true and the response is a server-sent event stream in the OpenAI format, terminated by data: [DONE].
curl -N https://api.tiny-router.dev/v1/chat/completions \
-H "Authorization: Bearer $TR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "~deepseek/deepseek-v4-flash-latest",
"stream": true,
"messages": [{"role": "user", "content": "Count to five."}]
}'What we do not do to your stream
- Bytes are forwarded as they arrive and flushed on every read. Nothing is buffered to re-encode it.
- Events are not reordered, merged or reformatted. Metering reads the same buffer that is being written to you.
- Upstream keep-alive comments are passed through, so a long first token does not look like a dead connection.
Disconnecting
Closing the connection cancels the upstream request, which is what stops the provider billing us and you. Settlement still runs, on a context detached from your request, so the tokens already generated are charged and the rest of the hold is released.
Proxies buffer
If you put a proxy in front of this, disable response buffering. Nginx needs
proxy_buffering off; Caddy needs flush_interval -1. Otherwise your time to first token becomes your time to last token.