Skip to main content

Streaming responses

Consume protocol-specific SSE and distinguish completion, errors and disconnection.

Enable streaming

Confirm the selected profile declares stream, then set "stream":true. Use cURL --no-buffer to display events promptly. Credentials, profile UUID, budget and price version are the same as for a non-streaming request.

The response is text/event-stream. Events are separated by blank lines; combine multiple data: lines within an event. Network reads can split UTF-8 characters and events, so a socket chunk is not a complete JSON message.

OpenAI format

Simplified illustrative events:

text
data: {"choices":[{"index":0,"delta":{"content":"Hello"}}]}

data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

Accumulate choices[].delta.content and merge tool-call fragments by index/ID. Require the normal protocol terminal signal, not merely a closed connection. Usage availability and location depend on the model; missing usage is not zero usage.

Anthropic format

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

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

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

This is an excerpt, not the full event sequence. Clients must also handle the selected model’s content_block_start/stop, message_delta, tool-input fragments and other events. Use message_stop for normal message completion, not OpenAI [DONE].

Errors, cancellation and costs

EventAction
Non-2xx before streaming beginsRead the protocol error and retain request IDs
Comments such as : keep-aliveKeepalive only, not generated output
Error after the stream beginsMark failure or pending reconciliation; HTTP 200 is insufficient
Cancel, disconnect or missing normal terminalKeep delivered output and query the original request; do not auto-repeat
Normal protocol terminalOutput is complete; settlement still follows the original consumption record

Cancelling after partial output does not guarantee a full refund. Inspect My requests and Billing. Agent Run has its own event contract; this is not a universal WebSocket/SSE promise.

OpenAI reference · Anthropic reference · Errors