Debian Codegraph @

examples/thinking_stream.py

raw
1 import anthropic
2
3 client = anthropic.Anthropic()
4
5 with client.messages.stream(
6 model="claude-sonnet-5",
7 max_tokens=16000,
8 thinking={"type": "adaptive", "display": "summarized"},
9 output_config={"effort": "high"},
10 messages=[
11 {
12 "role": "user",
13 "content": "Create a haiku about Anthropic. Think carefully about syllable counts before answering.",
14 }
15 ],
16 ) as stream:
17 thinking = "not-started"
18
19 for event in stream:
20 if event.type == "thinking":
21 if thinking == "not-started":
22 print("Thinking:\n---------")
23 thinking = "started"
24
25 print(event.thinking, end="", flush=True)
26 elif event.type == "text":
27 if thinking != "finished":
28 print("\n\nText:\n-----")
29 thinking = "finished"
30
31 print(event.text, end="", flush=True)