# SSE Streaming

Both OpenAI and Anthropic endpoints accept `stream: true` and return
Server-Sent Events. The wire format differs between vendors.

## OpenAI-compatible (`/v1/chat/completions`, `/v1/responses`)

Each event is a JSON object on a single line prefixed with `data: `:

    data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1725000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

    data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1725000000,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

    data: [DONE]

The stream terminates with the literal line `data: [DONE]`. After
receiving it, no more events will arrive - close the connection.

## Anthropic-compatible (`/v1/messages`)

Anthropic sends typed events:

    event: message_start
    data: {"type":"message_start","message":{"id":"msg_xxx","role":"assistant","content":[]}}

    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":"Hello"}}

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

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

The stream terminates with `message_stop`, not `[DONE]`. The gateway
does not synthesize a `[DONE]` line on Anthropic streams - treat
`message_stop` (or connection close) as the terminator.

## Half-close

The server closes the connection after the terminal event. Clients
should not pre-close - that aborts the request. If the upstream closes
mid-stream without a terminal event, the gateway terminates your stream
cleanly (no partial sentinel frame); treat a missing terminator as an
upstream failure and retry.

## Errors in stream

OpenAI: errors are sent as a final `data:` event before the connection
closes, with `error.code` and `error.message`. Anthropic: errors come as
an `event: error` with structured `error` data. See
[`/agents/patterns/error-recovery.md`](/agents/patterns/error-recovery.md).
