1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
model="claude-sonnet-4-5-20250929",
max_tokens=3200,
thinking={"type": "enabled", "budget_tokens": 1600},
messages=[{"role": "user", "content": "Create a haiku about Anthropic."}],
) as stream:
thinking = "not-started"
for event in stream:
if event.type == "thinking":
if thinking == "not-started":
print("Thinking:\n---------")
thinking = "started"
print(event.thinking, end="", flush=True)
elif event.type == "text":
if thinking != "finished":
print("\n\nText:\n-----")
thinking = "finished"
print(event.text, end="", flush=True)
|