Skip to main content

Extended Thinking

Extended thinking lets the model reason deeply before answering. It is suitable for complex math, logic, and analysis tasks.

import anthropic

client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")

response = client.messages.create(
model="claude-opus-4-7",
max_tokens=16000,
thinking={
"type": "enabled",
"budget_tokens": 10000
},
messages=[{"role": "user", "content": "Prove that the square root of 2 is irrational"}]
)

for block in response.content:
if block.type == "thinking":
print("Thinking process:")
print(block.thinking)
elif block.type == "text":
print("Answer:")
print(block.text)

Streaming Extended Thinking

import anthropic

client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")

with client.messages.stream(
model="claude-opus-4-7",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 8000},
messages=[{"role": "user", "content": "Analyze the pros and cons of this business plan: ..."}]
) as stream:
for event in stream:
if hasattr(event, "type"):
if event.type == "content_block_start" and hasattr(event.content_block, "type"):
if event.content_block.type == "thinking":
print("\n[Thinking...]")
elif event.type == "content_block_delta":
if hasattr(event.delta, "thinking"):
print(event.delta.thinking, end="", flush=True)
elif hasattr(event.delta, "text"):
print(event.delta.text, end="", flush=True)

budget_tokens must be at least 1024, and max_tokens must be greater than budget_tokens.