Streaming sends tokens to the client as they are generated — instead of waiting for the full response. For long responses, this cuts perceived latency from 5 seconds to <1 second. Every production chat interface uses streaming.
Direct Python scripts, CLI tools, or backend jobs where you want to print tokens as they arrive.
pip install anthropic — no extra dependencies.
import anthropic
client = anthropic.Anthropic()
# Method 1: stream context manager (recommended)
with client.messages.stream(
model="claude-haiku-4-5-20251001",
max_tokens=500,
messages=[{"role": "user", "content": "Write a 3-paragraph essay about neural networks."}],
) as stream:
for text_chunk in stream.text_stream:
print(text_chunk, end="", flush=True) # Print each token as it arrives
final_message = stream.get_final_message()
print(f"\nTotal tokens: {final_message.usage.input_tokens + final_message.usage.output_tokens}")
# Method 2: raw event stream (for custom event handling)
with client.messages.stream(
model="claude-haiku-4-5-20251001",
max_tokens=500,
messages=[{"role": "user", "content": "Explain transformers."}],
) as stream:
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="", flush=True)
elif event.type == "message_stop":
print("\n[Done]")Set read timeout, not connect timeout, for streaming.A streaming response can take 30s+ to complete while receiving data continuously. Set httpx.Timeout(connect=5.0, read=60.0) — a short connect timeout to detect dead servers, but a long read timeout to allow the stream to complete.
Lesson 3 covers retry and fallback patterns for structured outputs — what to do when validation fails or the API returns an error mid-stream.