Command Palette

Search for a command to run...

Streaming

The server-sent events shape, the exact chunk and the `[DONE]` sentinel, how to get token usage on the stream, and what a dropped connection means for your bill.

Turn it on

Add `"stream": true` to the same request you already send. The endpoint and the body are unchanged; only the response becomes `Content-Type: text/event-stream`, one JSON object per `data:` line.

cURL
curl https://api.idroutebase.com/v1/chat/completions \  -H "Authorization: Bearer $IDROUTEBASE_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "<vendor>/<model-id>",    "messages": [{ "role": "user", "content": "Hello" }],    "stream": true  }'

With `-N` (no buffer) curl prints each event as it arrives. The response is relayed byte-for-byte from the provider, so the chunks below are the shape you can rely on.

The byte shape

Each event is a line starting `data: ` followed by a JSON object, then a blank line. Every object is an OpenAI `chat.completion.chunk`: the same `id` and `model` throughout, one `delta` per event, and `finish_reason` set — to `stop` or `length` — on the final content chunk.

A stream
data: {"id":"chatcmpl-<id>","object":"chat.completion.chunk","created":1760000000,"model":"<vendor>/<model-id>","choices":[{"index":0,"delta":{"content":"Hel"},"finish_reason":null}]}

data: {"id":"chatcmpl-<id>","object":"chat.completion.chunk","created":1760000000,"model":"<vendor>/<model-id>","choices":[{"index":0,"delta":{"content":"lo"},"finish_reason":null}]}

data: {"id":"chatcmpl-<id>","object":"chat.completion.chunk","created":1760000000,"model":"<vendor>/<model-id>","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

The deltas are incremental: `Hel` and `lo` are two pieces of one answer, not two answers. Concatenate them in order. `data: [DONE]` is the sentinel that the stream finished cleanly; everything before it is the answer, and nothing arrives after it.

A client loop

A minimal reader: split the body on newlines, skip anything that is not a `data:` line, and stop at `[DONE]`.

JavaScript
const response = await fetch("https://api.idroutebase.com/v1/chat/completions", {  method: "POST",  headers: {    "Authorization": "Bearer " + process.env.IDROUTEBASE_API_KEY,    "Content-Type": "application/json",  },  body: JSON.stringify({    model: "<vendor>/<model-id>",    messages: [{ role: "user", content: "Hello" }],    stream: true,    stream_options: { include_usage: true },  }),});
const reader = response.body.getReader();const decoder = new TextDecoder();let buffer = "";let finished = false;
while (!finished) {  const { done, value } = await reader.read();  if (done) break;  buffer += decoder.decode(value, { stream: true });
  let newline;  while ((newline = buffer.indexOf("\n")) !== -1) {    const line = buffer.slice(0, newline).trim();    buffer = buffer.slice(newline + 1);    if (!line.startsWith("data:")) continue;
    const payload = line.slice(5).trim();    if (payload === "[DONE]") {      finished = true;      break;    }
    const chunk = JSON.parse(payload);    const delta = chunk.choices?.[0]?.delta?.content;    if (delta) process.stdout.write(delta);    if (chunk.usage) console.log("\nusage:", chunk.usage);  }}

The same loop in Python: `client.chat.completions.create(..., stream=True)` yields one chunk per event, and `for completion in stream:` over `completion.choices[0].delta.content` gives you the text. The SDKs hide the `data:` framing entirely — the shape above is what is on the wire.

Token usage on the stream

Chunks carry no token counts unless you ask. Send `stream_options: { "include_usage": true }` and the event just before `[DONE]` carries a `usage` object with empty `choices`.

Final chunk (with include_usage)
{  "choices": [],  "usage": {    "prompt_tokens": 12,    "completion_tokens": 42,    "total_tokens": 54  }}

The gateway fills this flag in for you when you set `stream: true` and send no `stream_options` at all. It does not overwrite one you send yourself — so if you set `stream_options` for other reasons, put `include_usage: true` in it or you will get no usage block and no way to reconcile the stream from the client side. Billing itself does not depend on the flag: the call is metered from the gateway's log by request id either way, but the usage object is the number your own analytics can trust.

When the stream drops

A clean stream ends with `[DONE]`. Anything else — an abrupt close, a timeout, a `5xx` that cuts the body — means you did not receive a complete answer, and the right move is to re-send the request. Do not treat the partial text as the answer.

A dropped stream is not free. The request was logged, so it is billed for the tokens the provider actually produced; a retry is a new request that bills separately. That is also why retrying a streamed call that died after emitting most of the answer can look like a doubled charge — it is two calls, not one.

A provider failure mid-stream can arrive as an `error` event followed by `[DONE]`, or as an abrupt close. Check the HTTP status before reading the body: some failures happen before any content and the status tells you without parsing. The Errors page covers the refusals the API produces itself.

Let nothing between you and it buffer

Streaming only works when every hop forwards bytes as they arrive. The API already sends `Cache-Control: no-cache, no-transform` and `X-Accel-Buffering: no`; if you also front it with your own proxy or an application server, disable response buffering and compression for this route, or all you will get is one delayed burst at the end — the stream is still billed as a stream, it just no longer feels like one.