Streaming API
Claude API streaming — real-time token-by-token responses. SSE format for both Anthropic and OpenAI-compatible endpoints.
Updated: July 25, 2026.
How to enable streaming
Add stream: true to a normal Messages API request. The server returns a Server-Sent Events stream instead of one JSON response.
{
"model": "claude-sonnet-4-6",
"max_tokens": 512,
"stream": true,
"messages": [
{"role": "user", "content": "Explain SSE in one paragraph"}
]
}Anthropic and OpenAI-compatible
Streaming with SDKs and cURL
Anthropic SDKs parse SSE and expose a convenient text stream. With direct HTTP, cURL shows the raw event and data lines.
curl https://api.llm-gate.tech/v1/messages \ --header "x-api-key: $CLAUDE_API_KEY" \ --header "anthropic-version: 2023-06-01" \ --header "content-type: application/json" \ --no-buffer \ --data '{ "model": "claude-sonnet-4-6", "max_tokens": 512, "stream": true, "messages": [ {"role": "user", "content": "Explain SSE in one paragraph"} ] }'SSE event sequence
Each stream begins with message_start, contains one or more content blocks, and ends with message_stop. Ping events may appear in between.
message_start— a Message object with an empty content array.content_block_start— the start of a text, tool_use, or thinking block.content_block_delta— the next incremental update for the block.content_block_stop— the block has finished.message_delta— stop_reason and cumulative usage updates.message_stop— the stream completed successfully.
Full HTTP stream response
For a direct HTTP integration, read SSE lines in order. The event field names the event, while data contains JSON with the matching type.
event: message_start
data: {"type":"message_start","message":{"id":"msg_...","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_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":8}}
event: message_stop
data: {"type":"message_stop"}Main delta types
| Delta | Carries | How to handle it |
|---|---|---|
| text_delta | A fragment of generated text | Append delta.text to the displayed output. |
| input_json_delta | Partial JSON for tool_use | Accumulate partial_json and parse after content_block_stop. |
| thinking_delta | Streaming thinking content | Handle only when thinking is enabled and available. |
| signature_delta | The thinking block signature | Retain it unchanged before content_block_stop. |
When the response is complete
Do not stop after the first text or content_block_stop event. The complete response is ready only after message_stop. Usage values in message_delta are cumulative.
- Accumulate blocks by index; it matches the block's position in the final content array.
- Ignore ping and unknown future event types without crashing the parser.
- For a final Message object, use finalMessage() in TypeScript or get_final_message() in Python.
Errors inside a streaming response
A request can start with HTTP 200 and later receive event: error. For example, overloaded_error inside a stream corresponds to HTTP 529 outside streaming. Partial text is not a complete response.
event: error
data: {
"type": "error",
"error": {
"type": "overloaded_error",
"message": "Overloaded"
}
}