Building with the Claude API · lesson 10 of 17
Streaming
Server-Sent Events (SSE) stream of incremental content deltas. Use for any UI where time-to-first-token matters.
Python
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
final = stream.get_final_message() # full Message after stream ends
Event types
message_start— initial message metadata.content_block_start/content_block_delta/content_block_stop— per content block.message_delta— top-level updates (e.g.,stop_reasonarrives here).message_stop— done.ping— keepalive.error— terminal.
For each content block you'll see deltas of type:
text_delta— text content.input_json_delta— partial JSON for atool_use(streamed as a string).thinking_delta— extended thinking content.signature_delta— extended thinking signature (for redaction proofing).
Streaming + tools
Tool use streams too. You'll see tool_use blocks start with name and id, then input_json_delta events that you concatenate into the JSON string, then a stop. Don't try to parse until the block is fully streamed unless your client does incremental JSON parsing.
Streaming + thinking
When extended thinking is enabled, you'll see thinking blocks streamed before the actual answer. Display these in a collapsed "Reasoning" section.
When to skip streaming
- Batch jobs (use the Message Batches API instead).
- Tool-use loops where you need the full response before deciding next step. (You can stream, but you don't display until done.)
- When latency is dominated by tool execution, not generation.