# Streaming

> Streaming output over SSE — events per format, usage, keep-alive and mid-stream errors.

Text endpoints stream the response when the request has `"stream": true`. The
stream is Server-Sent Events (`text/event-stream`) in the same format as the
original API. Images and video do not stream.

## Example

```bash [curl]
curl -N https://llmbox.cc/v1/chat/completions \
  -H "Authorization: Bearer $LLMBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "stream": true,
    "stream_options": {"include_usage": true},
    "messages": [{"role": "user", "content": "Tell me a short story"}]
  }'
```

```python [Python]
from openai import OpenAI

client = OpenAI(base_url="https://llmbox.cc/v1", api_key="$LLMBOX_API_KEY")
stream = client.chat.completions.create(
    model="openai/gpt-5.6-sol",
    stream=True,
    stream_options={"include_usage": True},
    messages=[{"role": "user", "content": "Tell me a short story"}],
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
```

## Events and usage per format

| Endpoint | Events | Where usage is |
| --- | --- | --- |
| `/v1/chat/completions` | `chat.completion.chunk`, then `data: [DONE]` | The last chunk — only with `"stream_options": {"include_usage": true}` |
| `/v1/responses` | `response.created`, `response.output_text.delta`, …, `response.completed` | `response.completed` |

## Keep-alive and long silences

- Reasoning models may stay silent for tens of seconds before the first token.
  The gateway waits about a minute for the provider's response to start; give
  your client a generous timeout.
- Until the provider starts its response, the client receives nothing — no
  headers and no comments: the stream opens only together with the provider's
  response. If it does not come, the gateway may retry (again up to a minute)
  or answers with a `502` error.
- Once the stream is open and the provider is silent, the gateway sends a
  `: keepalive` comment every 15 seconds so intermediate proxies do not drop
  the connection. The stream may contain other comment lines starting with
  `:` too. SDKs skip them; if you parse SSE yourself, ignore such lines.
- Stream time limits. Only response fragments count as progress; provider
  pings and keep-alive events do not:
  - the first fragment must arrive within 10 minutes of the stream opening;
  - after it, the gateway waits up to five minutes for each next fragment;
  - a whole stream lasts at most one hour.

  Past any of these the gateway closes the stream with an error event with
  the `upstream_timeout` code (see below).

## How a stream ends

A stream always ends in one of two ways:

- **a final event** of the format — `data: [DONE]`, `response.completed` (or
  `response.incomplete`), `message_stop`. The gateway closes the stream right
  after it;
- **an error event** — no `[DONE]` follows it.

The gateway sends the error event itself, in the endpoint's format:

| Endpoint | Error event |
| --- | --- |
| `/v1/chat/completions` | `data: {"id":…,"object":"chat.completion.chunk","error":{"message":…,"type":"server_error","code":"upstream_interrupted"}}` |
| `/v1/responses` | `event: response.failed` with `response.error.code` = `upstream_interrupted` |

The `upstream_interrupted` code means the provider cut the response off or
sent an error; `upstream_timeout` means the provider did not deliver the
response within the stream time limits (see above); `server_restarting`
means the server is restarting. In all cases retry the request. Details — [Errors](https://llmbox.cc/en/docs/errors).

## Mid-stream errors

- If the provider fails before the response starts, you get a regular JSON
  error with an HTTP status — see [Errors](https://llmbox.cc/en/docs/errors).
- Once the response has started (HTTP 200 sent), there is no retry: the
  stream closes with an error event (see above). Whatever part of the
  response arrived before it stays with you.
- A provider failure before the first meaningful piece of the response is not
  charged; after it, the input and the received part are charged.
- Canceling after the provider accepted the request is charged: the input and
  the received part of the response. Canceling after a provider refusal with an
  HTTP status is free (see
  [Balance and pricing](https://llmbox.cc/en/docs/pricing)).
