← Course | Module 02 - The API Lesson 10
MODULE 02

Response Streaming: Real-Time Output

🕑 10 min read 🎯 Beginner

Why Streaming Matters

Without streaming, users wait for the ENTIRE response before seeing anything. With streaming, text appears word by word - making Claude feel faster even though total time is the same.

# Streaming response with client.messages.stream( model="claude-sonnet-4-20250514", max_tokens=1024, messages=[{"role": "user", "content": "Explain quantum computing"}] ) as stream: for text in stream.text_stream: print(text, end="", flush=True)

When to Stream vs Not

StreamDon't Stream
Chatbots / real-time UIBackground processing
Long responsesShort, structured responses
User-facing appsBatch jobs / pipelines

Key Takeaways

🖨 Download PDF 🐦 Share on X
← Previous