# Streaming

> Receive completions incrementally via server-sent events to cut perceived latency and render tokens as they arrive.


# Stream responses token by token

Set `stream: true` on a Chat Completions or Messages request and AnyRouter returns [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instead of one buffered reply. Use an API key (`sk-ar-...`) from the [dashboard](https://anyrouter.dev/dashboard/keys).

::::tabs

:::tab{label="curl"}
`-N` disables curl's buffering.

```bash
curl -N https://anyrouter.dev/api/v1/chat/completions \
  -H "Authorization: Bearer sk-ar-your-key" \
  -H "Content-Type: application/json" \
  -d '{"model": "openai/gpt-5.4-mini", "stream": true,
       "messages": [{"role": "user", "content": "Count to 10"}]}'
```
:::

:::tab{label="TypeScript"}
```typescript
const stream = await client.chat.completions.create({
  model: "openai/gpt-5.4-mini",
  messages: [{ role: "user", content: "Count to 10" }],
  stream: true,
})

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
}
```
:::

:::tab{label="Python"}
```python
stream = client.chat.completions.create(
    model="openai/gpt-5.4-mini",
    messages=[{"role": "user", "content": "Count to 10"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)
```
:::

::::

## Events

```text
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"Hel"}}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"lo"}}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"!"},"finish_reason":"stop"}]}
data: [DONE]
```

- `delta.content`: the tokens added in this event.
- `delta.role`: set only on the first event.
- `finish_reason`: set on the final event.
- `[DONE]`: no more events follow.

## Cancellation

Close the HTTP connection (for example `controller.abort()` on an `AbortController`). AnyRouter propagates the abort upstream, and you pay only for tokens emitted before it.


## Related

- [Chat Completions API](/docs/api-reference/chat-completions.md)
- [Messages API](/docs/api-reference/messages.md)
- [Responses API](/docs/api-reference/responses.md)
- [Errors](/docs/guides/errors.md)
