The Claude AI Community Discord is open.Join us

Streaming Responses

7 min readYour First App

For a better user experience, stream Claude's responses token by token instead of waiting for the full response.

Why Stream?

  • Faster perceived latency — users see text appear immediately
  • Better UX — feels like a real conversation
  • Memory efficient — process text as it arrives

Python Streaming

import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a poem about coding"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

TypeScript Streaming

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

const stream = await client.messages.stream({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Write a poem about coding' }],
});

for await (const event of stream) {
  if (event.type === 'content_block_delta' &&
      event.delta.type === 'text_delta') {
    process.stdout.write(event.delta.text);
  }
}

Summary

Streaming is recommended for any user-facing application. The API sends Server-Sent Events (SSE) that you can process incrementally.