Streaming Responses
Implementing real-time streaming responses with the Claude API
title: Streaming Responses description: Implementing real-time streaming responses with the Claude API
Streaming allows you to receive Claude's response in real-time as it's generated, providing a better user experience and enabling progressive rendering. This guide covers implementing streaming in various contexts.
Why Use Streaming?
- Better UX: Users see responses immediately instead of waiting
- Progressive rendering: Display partial results as they arrive
- Long responses: Handle lengthy outputs without timeout issues
- Cancellation: Users can stop generation mid-stream
Basic Streaming Implementation
Node.js / TypeScript
Python
Server-Sent Events (SSE)
Next.js API Route
Client-Side Consumption
Stream Events
Understanding the event types:
| Event Type | Description |
|------------|-------------|
| message_start | Stream beginning, contains message metadata |
| content_block_start | New content block starting |
| content_block_delta | Incremental content update |
| content_block_stop | Content block complete |
| message_delta | Message-level updates (stop reason, usage) |
| message_stop | Stream complete |
Processing All Events
Streaming with Tool Use
When Claude uses tools, the stream includes tool calls:
Cancellation
Client-Side Cancellation
Server-Side Cancellation
React Integration
Custom Hook for Streaming
Best Practices
1. Handle Reconnection
2. Progress Indication
Show users that something is happening:
3. Buffer for Word Boundaries
Avoid showing partial words: