Anthropic Messages API — Streaming
Generated HTML view. Markdown remains canonical.
Anthropic Messages API — Streaming
Type: official-doc Tier: 1 (Official Doc) Author(s): Anthropic Date: Accessed 2026-06-01 URL: https://platform.claude.com/docs/en/docs/build-with-claude/streaming Accessed: 2026-06-01
Why This Source Matters
Defines how a response is delivered token-by-token instead of all at once. Streaming is what makes a chatbot feel responsive: the user sees text appear immediately rather than waiting for the full completion. This source gives the exact event protocol so the learner understands what is actually arriving over the wire, not just what the SDK abstracts away.
Key Claims
Enabling streaming
- Set
"stream": trueon a Messages request. The response is delivered as server-sent events (SSE). - Each SSE has a named event type (
event: message_stop) and a JSONdatapayload whosetypematches.
Event flow (in order)
message_start— aMessageobject with emptycontent.- For each content block: a
content_block_start, one or morecontent_block_deltaevents, then acontent_block_stop. Each block has anindexmatching its position in the finalcontentarray. - One or more
message_deltaevents — top-level changes to the finalMessage(e.g.stop_reason, finalusage). - A final
message_stop.
pingevents may be interleaved at any time;errorevents can also occur.
Content block delta types
- A text delta:
`` event: content_block_delta data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ello frien"}} ``
- A tool-use delta uses
input_json_deltawith chunkedpartial_json.
SDK usage (Python)
The official SDK abstracts the SSE parsing:
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
model="claude-opus-4-8",
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
stream.text_streamyields only the text deltas.stream.get_final_message()returns the complete accumulatedMessage(identical to.create()), useful when you also need finalusage.- For large
max_tokens, the SDKs require streaming to avoid HTTP timeouts.
Relevant To
- concepts: [streaming, server-sent-events, content-block-delta, token-by-token-generation]
- projects: [01-ai-chatbot]
Notes
- Final token
usagearrives in themessage_delta/ final message, not in the first event — so cost can only be finalized after the stream completes. - LiteLLM exposes streaming via
stream=True, yielding OpenAI-style chunks (chunk.choices[0].delta.content) rather than raw Anthropic events.