# Streaming

> Receive the reply as server-sent events while it's being written, and show it to your users word by word.

Source: https://docs.strokes.ink/guides/streaming

Set `"stream": true` and the reply arrives as [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instead of one JSON object.

## With an SDK

<Tabs items={["TypeScript","Python"]}>
<Tab value="TypeScript">
```ts
const stream = client.messages.stream({
  model: "swim-1.0",
  max_tokens: 32000,
  messages: [{ role: "user", content: "Write a haiku about deadlines." }],
});

stream.on("text", (text) => process.stdout.write(text));

const message = await stream.finalMessage();
console.log("\n", message.usage);
```
</Tab>

<Tab value="Python">
```python
with client.messages.stream(
    model="swim-1.0",
    max_tokens=32000,
    messages=[{"role": "user", "content": "Write a haiku about deadlines."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

    print(stream.get_final_message().usage)
```
</Tab>
</Tabs>

`client` is set up as in [SDKs](/get-started/sdks).

## With curl

```bash
curl -N https://api.strokes.ink/v1/messages \
  -H "x-api-key: $STROKES_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "swim-1.0",
    "stream": true,
    "messages": [{ "role": "user", "content": "Write a haiku about deadlines." }]
  }'
```

```text
event: message_start
data: {"type":"message_start","message":{"id":"msg_…","type":"message","role":"assistant","model":"swim-1.0","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":8,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0}}}

event: ping
data: {"type":"ping"}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Clock "}}

…

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":14,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":212}}

event: message_stop
data: {"type":"message_stop"}
```

To show the reply as it's written, append the `text` of each `text_delta`. The final `stop_reason` and `usage` come in `message_delta`; the `input_tokens` in `message_start` is an estimate, and what was read from the [prompt cache](/guides/messages#prompt-caching) is only counted in `message_delta`. At a high [reasoning level](/guides/messages#reasoning-level) the first text can take a while: the model reasons before it writes. To show the reasoning as it comes, send `thinking` of type `adaptive`: it streams first, as a `thinking` block of `thinking_delta`s.

Tool calls stream as blocks of their own, with `input_json_delta` pieces; several calls arrive one after another, each complete. With [web search](/guides/web-search#streaming), results arrive as whole blocks between rounds. Every event is described in [Streaming events](/api/streaming-events).

## Errors

Errors found before the reply starts — a bad key, no credits, a bad request, a model too busy to take it — come back as an ordinary JSON [error](/guides/errors) with its status code, not as a stream.

A stream that fails after it started ends with an `error` event instead of `message_stop`:

```text
event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"The model is overloaded right now. Retry the request in a moment."}}
```

Retry the request then.
