> ## Documentation Index
> Fetch the complete documentation index at: https://docs.antarah.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming

> Receive output as server-sent events while the model generates it.

Set `stream: true` on Chat Completions, Responses, or Messages. The response is `text/event-stream` in that API's own event format, so SDK stream helpers work unchanged.

```bash Chat Completions theme={null}
curl -N https://api.antarah.dev/v1/chat/completions \
  -H "Authorization: Bearer $ANTARA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "a1",
    "stream": true,
    "stream_options": { "include_usage": true },
    "messages": [{ "role": "user", "content": "Count to five." }]
  }'
```

```python OpenAI SDK theme={null}
stream = client.chat.completions.create(
    model="a1",
    messages=[{"role": "user", "content": "Count to five."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="")
    if chunk.usage:
        print("\n", chunk.usage)
```

```python Anthropic SDK theme={null}
with client.messages.stream(
    model="a1", max_tokens=1024, messages=[{"role": "user", "content": "Count to five."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="")
    print(stream.get_final_message().usage)
```

| Endpoint | Events | Ends with |
| - | - | - |
| Chat Completions | `chat.completion.chunk` objects | `data: [DONE]` |
| Responses | `response.*` events, each with its `event:` name | `response.completed` (or `response.incomplete`) |
| Messages | `message_start`, `content_block_*`, `message_delta`, `ping` | `message_stop` |

## Usage

Chat Completions sends a final chunk with `usage` only if you ask for it with `stream_options.include_usage: true`. Responses and Messages always include usage in their final events.

## Keepalives

While the model is thinking, or while a [response policy](https://docs.antarah.dev/guides/policies.md) holds the output, a stream can go quiet. After 15 seconds of silence Antara sends a keepalive: an SSE comment (`: keepalive`) on Chat Completions and Responses, and a `ping` event on Messages. SDKs ignore them. They keep proxies and load balancers from closing an idle connection.

Long reasoning and long outputs don't time out between your client and Antara. Toward the model, a request may run for up to 15 minutes in total, and the model may go up to 2 minutes without sending anything; past either limit the request ends with `504 upstream_timeout` (or that terminal error mid-stream).

## Errors during a stream

An error before the stream starts is a normal HTTP error. Once the `200` and the first bytes are sent, a failure ends the stream with that API's terminal error event instead:

| Endpoint | Terminal error |
| - | - |
| Chat Completions | `data: {"error": {…}}`, then the connection closes, with **no** `data: [DONE]` |
| Responses | `event: response.failed` with `response.error = {code, message}`, and no `response.completed` |
| Messages | `event: error` with `{"type": "error", "error": {…}}`, and no `message_stop` |

A stream that ends without its normal last event didn't finish. Treat it as failed.

## Stopping early

Close the connection to stop. Antara cancels the model's work at once and bills only the tokens generated until then.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.