Claude streaming: events and recovery

When creating a Message, you can set stream to true to incrementally stream the response using server-sent events (SSE).

Verified

On this page

Definition

Set stream to true when creating a Claude Message to receive the response incrementally through server-sent events (SSE). Each SSE event has a named type and associated JSON data. If you do not need to process text as it arrives, the SDK can stream internally and return the complete Message object.

For requests with large max_tokens values, the SDK requires streaming to avoid HTTP timeouts. The Python SDK allows both sync and async streams. The PHP SDK provides streaming through createStream.

Amazon Bedrock's bedrock-runtime endpoint does not stream plain server-sent events. A direct API integration needs to handle the events in the stream itself.

At a glance

The stream may also contain ping events alongside its main flow.

Diagram: message_start contains a Message object with empty content. > A series of content blocks follows, each with a content_block_start, any number of content_block_delta events, and a content_block_stop event. > One or more message_delta events indicate top-level changes to the final Message object. > The flow ends with a final message_stop event.1message_start contains a Message object withempty content.2A series of content blocks follows, each with acontent_block_start, any number ofcontent_block_delta events, and acontent_block_stop event.3One or more message_delta events indicatetop-level changes to the final Message object.4The flow ends with a final message_stop event.

How it works

The delta type in content_block_delta determines how to update the content block at that index. Each content block has an index that corresponds to its index in the final Message content array. Some blocks arrive whole, with no deltas: redacted_thinking blocks, server tool results such as web_search_tool_result, and the fallback block at each model boundary of a server-side fallback.

When thinking is used with streaming enabled, thinking content arrives through thinking_delta events. For thinking content, a special signature_delta event is sent just before the content_block_stop event. This signature is used to verify the integrity of the thinking block.

When display is set to omitted on the thinking configuration, no thinking text is streamed. One documented request enables thinking with streaming, and its summarized display setting streams a condensed summary of Claude's reasoning rather than the full chain of thought. The stream keeps the HTTP connection alive through SSE. The SDK’s message-accumulation helper collects the events into a complete Message object.

The API might occasionally send errors in the event stream. During periods of high usage, an overloaded_error might be received, which would normally correspond to an HTTP 529 in a non-streaming context. The versioning policy allows new event types, so code should handle unknown events gracefully.

Example

The official weather-tool request and SSE response show how tool input arrives incrementally. Deltas for a tool_use block update its input field. The deltas are partial JSON strings; the final tool_use.input is always an object.

By default, the API buffers each key and value in input until it is complete. Once the API has a complete input key and value, it emits them as multiple content_block_delta events with chunked partial JSON. With tools, there might be delays between streaming events while the model generates a value.

Accumulate the string deltas as they arrive, then parse the complete JSON after content_block_stop. The documented options include partial JSON parsing with Pydantic or the SDK streaming helpers. Another documented request asks Claude to search the web for current weather information. For Claude 4.5 models and earlier, a streaming request interrupted because of network issues, timeouts, or other errors can be recovered by resuming from where the stream was interrupted.

Before recovery, save all content successfully received before the error. Then create a new API request with the partial assistant response at the start of a new assistant message. For Claude 4.6 or later models, the same capture-and-resume strategy applies, but the partial response goes in a user message with an instruction to continue from the interruption.

Tool use and thinking blocks cannot be partially recovered.

From the official source
python
client = anthropic.Anthropic()

tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                }
            },
            "required": ["location"],
        },
    }
]

with client.messages.stream(
    model="claude-opus-5",
    max_tokens=1024,
    tools=tools,
    tool_choice={"type": "any"},
    messages=[
        {"role": "user", "content": "What is the weather like in San Francisco?"}
    ],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
From the official source
Plain text
event: message_start
data: {"type":"message_start","message":{"id":"msg_014p7gG3wDgGV9EUtLvnow3U","type":"message","role":"assistant","model":"claude-opus-5","stop_sequence":null,"usage":{"input_tokens":472,"output_tokens":2},"content":[],"stop_reason":null}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: ping
data: {"type": "ping"}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01T1x1fJ34qAmk2tNTrN7Up6","name":"get_weather","input":{}}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"location\":"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"San"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Francisc"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"o,"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" CA\"}"}}

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

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":89}}

event: message_stop
data: {"type":"message_stop"}
From the official source
Plain text
Your previous response was interrupted and ended with [previous_response]. Continue from where you left off.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.