Streaming
Endpoints
Streaming
Stream Claude’s response word by word using Server-Sent Events for a real-time user experience.
POST
Streaming
Overview
By default, the Claude API waits until the full response is generated before returning it. Streaming changes this — Claude sends the response in chunks as it generates, word by word, exactly like you see in Claude.ai. Streaming is enabled by adding a single field to your request:Why Use Streaming
Almost every production application that displays Claude responses to end users uses streaming. It is the standard approach for chat interfaces, writing tools, and any application where response time affects user experience.
The Event Lifecycle
Streaming responses are delivered as Server-Sent Events (SSE) — a standard web protocol for pushing data from server to client in real time. Each event has atype and a data field.
Here is the complete event sequence from a real tested request:
Understanding Each Event
Extracting the Text
The text you want lives insidecontent_block_delta events, specifically in the delta.text field. Everything else is lifecycle metadata.
For each incoming event:
if event.type === “content_block_delta” → append delta.text to your display
if event.type === “message_stop” → close the stream
if event.type === “ping” → ignore
The full response is the concatenation of all delta.text values across every content_block_delta event.
Chunk Size Is Variable
Claude does not stream one word at a time. Chunks vary in size — sometimes a single word, sometimes several sentences. From a real tested response:
Never assume chunk boundaries align with word or sentence boundaries. Always concatenate and render the full accumulated text.
Token Reporting in Streaming
Token counts are split across two events because output tokens are not known until generation completes:
This matters for real-time cost tracking — you cannot calculate the full cost of a streaming request until the
message_delta event arrives at the end of the stream.
Markdown in Streaming Responses
Claude returns markdown formatting inside streamed text chunks. From a real tested response:**bold** instead of bold if markdown is not rendered.
When Not to Use Streaming
Streaming adds implementation complexity. It is only worth it when real-time display matters:Further Reading
Send a Message
Standard non-streaming endpoint reference
Tokens
How token reporting differs between streaming and standard responses
Tool Use
Streaming and tool use can be combined in production applications
Error Handling
Handle errors that occur mid-stream
Authorizations
Headers
Example:
"2023-06-01"
Body
application/json
The Claude model to use.
Available options:
claude-sonnet-4-6, claude-opus-4-6, claude-haiku-4-5 Example:
"claude-sonnet-4-6"
Maximum number of output tokens to generate.
Required range:
x >= 1Example:
1024
Set to true to enable streaming.
Example:
true
Response
Streaming response — returns Server-Sent Events
A stream of Server-Sent Events. Each event has a type and data field.