# Streaming

> Receive a reply as it is written, as server-sent events.

## What it is for

Without streaming, you wait for the whole reply. With `stream` set to true, the reply arrives in pieces as it is written, so you can show text straight away.

## Stream a reply

```bash
curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "stream": true,
    "messages": [
      {"role": "user", "content": "Draft a reply that declines the meeting."}
    ]
  }'
```

```python
from antarya import Antarya

client = Antarya()

with client.chat.stream(
    messages=[{"role": "user", "content": "Draft a reply that declines the meeting."}],
) as stream:
    for text in stream.text:
        print(text, end="")
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const stream = await client.chat.create({
  messages: [{ role: "user", content: "Draft a reply that declines the meeting." }],
  stream: true,
});

for await (const event of stream) {
  if (event.type === "content.delta" && typeof event.text === "string") process.stdout.write(event.text);
}
```

Output:

```
event: reply.start
data: {"id": "req_03", "model": "antarya"}

event: content.delta
data: {"index": 0, "type": "text", "text": "Thank you for the invitation. "}

event: content.delta
data: {"index": 0, "type": "text", "text": "I am unable to join on Thursday."}

event: reply.done
data: {"stop_reason": "end_turn", "usage": {"input_tokens": 14, "output_tokens": 19}}
```

## Events

The response is a stream of server-sent events. Each has an `event:` name and a `data:` line of JSON.

| Event | When it arrives | Data |
|---|---|---|
| `reply.start` | Once, first. | `id` and `model`. |
| `thinking` | Only when `include_thinking` is true, between `reply.start` and the first `content.delta`. | `step`, `stage` and `summary`. |
| `content.delta` | Many times. | `index`, `type` and the next piece of `text`. |
| `reply.done` | Once, last. | `stop_reason` and `usage`. |
| `error` | If the request fails after the stream has begun. | The same `error` object as other failures, with a `request_id`. |

While Antarya works, the server sends a `: ping` comment line every 15 seconds so the connection stays open. Ignore it. A `thinking` event is a short note on progress: `stage` is a short label and `summary` says what that step did. Thinking notes are not counted as output tokens.

Join the `text` of every `content.delta` in order to get the full reply. Treat `reply.done` as the end: `usage` is final there. A request that fails before the stream begins returns an ordinary error status instead of an `error` event.

## Parameters

- `stream` (boolean, optional): Set to true to receive server-sent events instead of one JSON body. The default is false.
- `include_thinking` (boolean, optional): Set to true to receive `thinking` events before the first `content.delta`. The default is false.
- `messages` (array, required): The conversation, as in any chat request.

## Next steps

- [Errors](https://antarya.ai/docs/api.md): What each failure looks like and how to handle it.
- [Python SDK](https://antarya.ai/docs/sdk/python.md): The stream helper and its options.
- [TypeScript SDK](https://antarya.ai/docs/sdk/typescript.md): Loop over events as they arrive.
