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 a type 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 inside content_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.
Always check event.type before extracting data. The ping event carries no delta.text field — attempting to extract it without checking the type first will throw errors in your implementation.

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:
Streaming is the continuous delivery of data If you are displaying streamed responses in a UI, render markdown.. do not display raw text. Users will see **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

x-api-key
string
header
required

Headers

anthropic-version
string
required
Example:

"2023-06-01"

Body

application/json
model
enum<string>
required

The Claude model to use.

Available options:
claude-sonnet-4-6,
claude-opus-4-6,
claude-haiku-4-5
Example:

"claude-sonnet-4-6"

max_tokens
integer
required

Maximum number of output tokens to generate.

Required range: x >= 1
Example:

1024

messages
object[]
required
stream
boolean

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.