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

# Streaming responses over SSE

> Consume Server-Sent Events from Chat Completions, the Responses API and Anthropic Messages, with the exact event shapes each surface sends back.

Set `"stream": true` to receive the response incrementally as Server-Sent Events
(SSE). Pass `-N` (or `--no-buffer`) to curl so it prints chunks as they arrive.

## Chat Completions

Each event is a line `data: <json>` carrying a `chat.completion.chunk`:

```bash theme={null}
curl -N https://api.impossibl.com/v1/chat/completions \
  -H "Authorization: Bearer imp-rt-..." \
  -H 'content-type: application/json' \
  -d '{
    "model": "openai/gpt-4o-mini",
    "stream": true,
    "stream_options": { "include_usage": true },
    "messages": [{"role":"user","content":"Count to 3"}]
  }'
```

```text Stream (abridged) theme={null}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"1"},"finish_reason":null}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":9,"completion_tokens":5,"total_tokens":14}}

data: [DONE]
```

* **First chunk** → `delta.role: "assistant"`, `finish_reason: null`
* **Content chunks** → `delta.content` holds the next token(s)
* **Final chunk** → empty `delta`, `finish_reason` set (`stop`, `length`, …)
* **Usage chunk** → only when `stream_options.include_usage` is `true`; it has an
  empty `choices` array and a `usage` object
* **Terminator** → `data: [DONE]`

To reassemble the text, concatenate every `choices[0].delta.content`. Because the
gateway emits the exact OpenAI shape, `client.chat.completions.create(..., stream=True)`
in any OpenAI SDK works unchanged.

## Responses API

The Responses API streams **semantic events** instead of generic chunks. Each
event has both an `event:` line and a `data:` line, and a monotonically
increasing `sequence_number`.

```bash theme={null}
curl -N https://api.impossibl.com/v1/responses \
  -H "Authorization: Bearer imp-rt-..." \
  -H 'content-type: application/json' \
  -d '{"model":"anthropic/claude-haiku-4-5","stream":true,"input":"Count to 3"}'
```

Events arrive in this order:

| Event                         | Meaning                                              |
| ----------------------------- | ---------------------------------------------------- |
| `response.created`            | response started (status `in_progress`)              |
| `response.in_progress`        | progress heartbeat                                   |
| `response.output_item.added`  | the assistant message item begins                    |
| `response.content_part.added` | an `output_text` part begins                         |
| `response.output_text.delta`  | a text delta (`delta` field) — repeats               |
| `response.output_text.done`   | the text part finished                               |
| `response.content_part.done`  | the content part finished                            |
| `response.output_item.done`   | the message item finished                            |
| `response.completed`          | final — carries the full `response` object + `usage` |

Aggregate the `delta` fields from `response.output_text.delta` events to build the
text; read final token `usage` from `response.completed`.

## Messages (Anthropic)

`POST /v1/messages` streams Anthropic's semantic events, so the official
`anthropic` SDK's `client.messages.stream(...)` works unchanged. Like the
Responses API, each event has an `event:` line and a `data:` line. (A bare
`claude-...` model id routes to Anthropic; any `provider/model` id also works
here — see [SDKs & integrations](/docs/integrations#anthropic-sdk).)

```bash theme={null}
curl -N https://api.impossibl.com/v1/messages \
  -H "x-api-key: imp-rt-..." \
  -H 'content-type: application/json' \
  -d '{"model":"claude-haiku-4-5","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"Count to 3"}]}'
```

Events arrive in this order:

| Event                 | Meaning                                                        |
| --------------------- | -------------------------------------------------------------- |
| `message_start`       | the assistant `message` begins (empty `content`, zeroed usage) |
| `content_block_start` | a `text` content block begins (`index` 0)                      |
| `ping`                | keep-alive heartbeat                                           |
| `content_block_delta` | a `text_delta` (`delta.text`) — repeats                        |
| `content_block_stop`  | the content block finished                                     |
| `message_delta`       | carries the final `stop_reason` and cumulative `usage`         |
| `message_stop`        | final — the stream is complete                                 |

Concatenate `delta.text` from `content_block_delta` events to build the output;
read the final token `usage` (and `stop_reason`) from `message_delta`. Note the
usage on `message_start` is zeroed — real token counts are only known at the end.
