# Streaming

> Stream tokens from 7API with server-sent events on Chat Completions, Responses and Anthropic Messages. Usage in the final chunk, keep-alive comments and mid-stream error events.


Set `"stream": true` on any generation endpoint to receive tokens as server-sent events (SSE). Each format's events match its original API, so the SDKs' streaming helpers work unchanged.

| Endpoint | Event format |
|---|---|
| `/v1/chat/completions` | `data: {chat.completion.chunk}` lines, ending with `data: [DONE]` |
| `/v1/responses` | Typed `response.*` events, such as `response.output_text.delta` and `response.completed` |
| `/v1/messages` | Anthropic events: `message_start`, `content_block_delta`, `message_delta`, `message_stop` |

```python
from openai import OpenAI
client = OpenAI(base_url="https://7api.st/v1", api_key="sk-your-key")
for chunk in client.chat.completions.create(
    model="glm-5.3-flash", stream=True,
    messages=[{"role": "user", "content": "Count to ten."}],
):
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

## Usage in streams

On Chat Completions, the final chunk before `[DONE]` carries a `usage` object with token counts, including `prompt_tokens_details.cached_tokens`. Its `choices` array is empty. The gateway requests usage from every upstream, so you don't need to send `stream_options.include_usage`. Anthropic streams report usage in `message_start` and `message_delta`, and Responses streams report it in `response.completed`.

## Keep-alive

Long-thinking models can take a while before the first token. To stop proxies and load balancers from closing an idle connection, the gateway may send the response headers early and then an SSE comment line every 15 seconds:

```text
: 7api keep-alive
```

Lines that start with `:` are comments under the SSE specification, and compliant clients ignore them. If you parse SSE by hand, skip them.

## Errors during a stream

If something fails after the stream has started, the gateway can't change the HTTP status anymore. It sends an error event in the stream's own format instead:

```text
event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"…"}}
```

On OpenAI formats, the error is a `data: {"error": {…}}` line. Treat it as a failed request. You are charged only for output that was actually delivered.

## Non-streaming requests

With `stream` omitted or `false`, you get one JSON response. For very long generations, streaming is still recommended, because it avoids client-side read timeouts.
