← Home

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.

Request body
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 512,
  "stream": true,
  "messages": [
    {"role": "user", "content": "Explain SSE in one paragraph"}
  ]
}

Anthropic and OpenAI-compatible

Both formats support stream: true. The Anthropic /v1/messages endpoint sends named SSE events, while the OpenAI-compatible /v1/chat/completions endpoint sends data chunks followed by [DONE].

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_starta Message object with an empty content array.
  • content_block_startthe start of a text, tool_use, or thinking block.
  • content_block_deltathe next incremental update for the block.
  • content_block_stopthe block has finished.
  • message_deltastop_reason and cumulative usage updates.
  • message_stopthe 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.

text/event-stream
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

DeltaCarriesHow to handle it
text_deltaA fragment of generated textAppend delta.text to the displayed output.
input_json_deltaPartial JSON for tool_useAccumulate partial_json and parse after content_block_stop.
thinking_deltaStreaming thinking contentHandle only when thinking is enabled and available.
signature_deltaThe thinking block signatureRetain 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.

SSE
event: error
data: {
  "type": "error",
  "error": {
    "type": "overloaded_error",
    "message": "Overloaded"
  }
}
Claude API error reference