# Streaming events

> The server-sent events a streamed message is made of, in order.

Source: https://docs.strokes.ink/api/streaming-events

With `"stream": true`, [`POST /v1/messages`](/api/messages) answers with `content-type: text/event-stream`. Each event has a name and a JSON `data` line; the `data` repeats the name as `type`.

## Order

1. `message_start`
2. For each content block, in order: `content_block_start`, `content_block_delta` (none, once or many times), `content_block_stop`
3. `message_delta`
4. `message_stop`

`ping` events can come at any point, and a stream that fails midway ends with an `error` event instead of `message_delta` and `message_stop`.

Blocks never interleave: one is stopped before the next starts, and `index` counts up from `0` across the whole message — through web searches too. When the model makes several tool calls at once, each arrives as its own complete block.

## Events

### message_start

The message, with empty `content`. `usage.input_tokens` here is an estimate and `cache_read_input_tokens` is `0`; the final counts come in `message_delta`.

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

### content_block_start

A block begins at `index`. A text block starts empty:

```json
{
  "type": "content_block_start",
  "index": 0,
  "content_block": {
    "type": "text",
    "text": ""
  }
}
```

A `thinking` block — only when you asked for the [reasoning](/guides/messages#see-the-reasoning) — starts empty, before the text:

```json
{
  "type": "content_block_start",
  "index": 0,
  "content_block": {
    "type": "thinking",
    "thinking": "",
    "signature": ""
  }
}
```

A `tool_use` or `server_tool_use` block starts with an empty `input`, filled by the deltas that follow:

```json
{
  "type": "content_block_start",
  "index": 1,
  "content_block": {
    "type": "tool_use",
    "id": "toolu_…",
    "name": "get_word_count",
    "input": {}
  }
}
```

A `web_search_tool_result` or `web_fetch_tool_result` block arrives whole in `content_block_start`, with no deltas — see [Web search](/guides/web-search#streaming).

### content_block_delta

The next piece of the block at `index`. For text, append `delta.text`:

```json
{
  "type": "content_block_delta",
  "index": 0,
  "delta": {
    "type": "text_delta",
    "text": "Hello "
  }
}
```

For reasoning, append `delta.thinking`:

```json
{
  "type": "content_block_delta",
  "index": 0,
  "delta": {
    "type": "thinking_delta",
    "thinking": "The reader is "
  }
}
```

For a tool call, append `delta.partial_json`; the pieces together are the call's `input` as JSON:

```json
{
  "type": "content_block_delta",
  "index": 1,
  "delta": {
    "type": "input_json_delta",
    "partial_json": "{\"draft_id\": \"d_42\"}"
  }
}
```

### content_block_stop

The block at `index` is complete.

```json
{
  "type": "content_block_stop",
  "index": 0
}
```

### message_delta

The message's `stop_reason` and its final `usage`, `server_tool_use` included when the model searched the web.

```json
{
  "type": "message_delta",
  "delta": {
    "stop_reason": "end_turn",
    "stop_sequence": null
  },
  "usage": {
    "input_tokens": 12,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0,
    "output_tokens": 39
  }
}
```

### message_stop

The stream is over.

```json
{ "type": "message_stop" }
```

### ping

Keeps the connection alive — after `message_start`, and every few seconds while the API searches the web. Ignore it.

```json
{ "type": "ping" }
```

### error

The stream failed after it started. Nothing more follows; retry the request.

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

`error.type` is one of the [error types](/api/errors#error-types).

## Errors before the stream

Errors found before the reply starts — authentication, billing, validation, or a model too busy to take the request — come back as an ordinary JSON [error](/api/errors) with its status code, not as a stream.

For a walkthrough with the SDKs, see [Streaming](/guides/streaming).
