Skip to content

Commit f463248

Browse files
committed
docs: add thinking examples
1 parent 599f2b9 commit f463248

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

examples/thinking.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import anthropic
2+
3+
client = anthropic.Anthropic()
4+
5+
response = client.messages.create(
6+
model="claude-3-7-sonnet-20250219",
7+
max_tokens=3200,
8+
thinking={"type": "enabled", "budget_tokens": 1600},
9+
messages=[{"role": "user", "content": "Create a haiku about Anthropic."}],
10+
)
11+
12+
for block in response.content:
13+
if block.type == "thinking":
14+
print(f"Thinking: {block.thinking}")
15+
elif block.type == "text":
16+
print(f"Text: {block.text}")

examples/thinking_stream.py

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

0 commit comments

Comments
 (0)