Skip to Content
Konduit docs

Streaming

Server-sent events, the two usage rules that surprise people, and what a stream that breaks looks like.

Set stream: true and konduit relays the provider's server-sent events as they arrive, frame by frame, flushed immediately.

curl -N https://api.konduit.eu/v1/chat/completions \
  -H "Authorization: Bearer $KONDUIT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-small-3.2",
    "stream": true,
    "messages": [{ "role": "user", "content": "Count to three." }]
  }'

The wire format is OpenAI's: data: frames carrying one chat.completion.chunk object each, terminated by data: [DONE]. Any OpenAI SDK's streaming client reads it without changes.

Response headers

Konduit-Provider and Konduit-Deployment are set before the first frame, because on a stream the headers go out with the first flush and cannot be revised afterwards. konduit also sends Cache-Control: no-cache and X-Accel-Buffering: no; the second is what stops an nginx or a CDN in between from buffering the whole stream into one late delivery.

Connection: keep-alive is deliberately not sent, although most examples show it. It is a hop-by-hop header HTTP/2 forbids, and connection reuse is the server's business.

Usage on a stream

This is the part worth reading twice.

konduit bills only from provider-native token counts — never from a local estimate — and on a stream those counts exist only if somebody asked for them. So konduit asks, on every streaming request, whatever you sent:

On the way out, stream_options.include_usage is set to true.

What you receive is a separate decision, and it depends on what you asked for:

You sent stream_options: {"include_usage": true}. Your request goes upstream byte for byte identical — there was nothing to rewrite — and you get the final usage chunk exactly as the provider sent it: a chunk with an empty choices array and a populated usage object, just before [DONE].

{
  "id": "chatcmpl-…",
  "object": "chat.completion.chunk",
  "model": "mistral-small-3.2",
  "choices": [],
  "usage": { "prompt_tokens": 24, "completion_tokens": 7, "total_tokens": 31 }
}

Everything else in a frame is relayed as the bytes it arrived as — including a "usage": null a provider sent itself. Rewriting every chunk to hide one key would mean re-encoding the completion konduit is supposed to be transparent to.

Ask for usage if you want to see it

There is no way to opt out of konduit requesting usage — a client that could would be a client that could opt out of being billed correctly. There is also no way to receive it without asking. Send stream_options: {"include_usage": true} and you get the numbers konduit bills from, which is the only version of them that matters.

Fallback, and where it stops

konduit may try several deployments for one request (see Routing). On a stream the boundary is precise:

  • Before the first flushed byte — a provider that answers 429, 5xx, or times out while the stream is being opened is replaced by the next candidate. You never see it happen; you get one status and one stream.
  • After the first flushed byte — you already have a 200 and a content type. Nothing can be replaced, and a failure can only be reported inside the stream.

Holding the response head back until the first content frame arrives would buy the case of a provider that answers 200 and dies immediately, at the price of withholding the head for as long as the model spends thinking — which is exactly the wait streaming exists to avoid.

When a stream breaks

Two shapes, and both arrive in band:

  1. The provider fails mid-stream. Its error envelope arrives where a chunk would go, and konduit relays that frame like any other. Your SDK sees an error object inside the stream.

  2. The stream dies while konduit is reading it. konduit appends one frame of its own — the only frame in the whole system konduit authors — carrying the OpenAI error envelope with the upstream_ code that describes what went wrong. See Errors for the vocabulary.

    data: {"error":{"message":"the upstream provider did not respond in time","type":"server_error","param":null,"code":"upstream_timeout"}}

    data: [DONE] is deliberately not written after it. A client that only watches for the terminator can therefore also tell that the completion did not finish, without parsing the frames.

In both cases the HTTP status is already 200, because it was sent before the first frame. A streaming client that only inspects the status code will think the request succeeded; read the frames.

If you disconnect

Hanging up mid-stream is a normal thing to do. konduit stops relaying, but keeps reading the provider's stream for nobody until its usage arrives — for up to a minute unless the installation configures another — and bills the request from those counts like any other, tokens generated after you left included. Only a request whose counts do not reach konduit in that time is marked for reconciliation. It is charged only if the provider's own numbers prove the usage within three days; after that konduit absorbs the cost and the request is never charged. konduit never substitutes an estimate for counts it did not receive.

On this page