Streaming
Streaming lets your UI render tokens as soon as the model produces them, instead of waiting for the full response. The InfinityBlue gateway streams chat completions over Server-Sent Events (SSE) using the same format as the OpenAI streaming API.
Why stream
- Lower perceived latency. Users see the first word in ~300 ms instead of waiting 2-5 seconds.
- Faster cancellation. Stop paying for in-flight tokens the moment the user navigates away.
- Better for long answers. A 1,000-token response is unusable as a single chunk but feels responsive when it streams.
Enable streaming
Set "stream": true in the request body. The server returns Content-Type: text/event-stream and emits one event per token.
Each event is a single line that starts with data: followed by a JSON chunk. The stream ends with a literal data: [DONE].
Client examples
The OpenAI SDK handles the parsing for you. Stream chunks arrive as delta objects you append to your local state.
Use fetch in the browser
If you cannot ship an SDK, the raw fetch API works. Parse each data: line as a separate JSON object and append the delta.
When the model produces a tool call, the deltas stream in pieces of the JSON. Accumulate the argument fragments into the same object the model would have returned in a non-streaming response, and only execute the tool once finish_reason is tool_calls. Structured output still streams token by token, but the final parsed object matches what response_format declared.
If you abort a stream mid-response, the model may have already consumed prompt tokens. The next billing cycle still charges for the input. Cancel early, not after a long pause.