Claude API

How do you stream a Claude API response?

QUICK ANSWER

To stream Claude API responses, set the request parameter 'stream' to true. The server will emit Server-Sent Events containing JSON-RPC event chunks for message start, content delta updates, and completion.

Python SDK Streaming Example

import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short poem."}],
    model="claude-3-5-sonnet-20241022",
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
Verified against: Anthropic Python SDK v0.39.0