Claude API · Streaming
Stream text (Python)
Print text as it is generated. Use streaming for long outputs and high max_tokens, which avoids request timeouts.
with client.messages.stream(
model="claude-opus-5-5",
max_tokens=64000,
messages=[{"role": "user", "content": "Write a story"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Official Claude API documentation