Working Python examples for Claude Opus 4 (claude-opus-4-7), Anthropic's most capable model. Learn when Opus beats Sonnet, how to use extended thinking with Opus, and how to manage cost at Opus pricing.
Claude Opus 4 (claude-opus-4-7) is Anthropic's most capable model. Use it when task difficulty justifies the higher cost — most production traffic routes to Sonnet 4.6.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7", # ← Opus 4
max_tokens=2048,
messages=[{
"role": "user",
"content": "Design a microservices architecture for a real-time bidding system handling 500K requests/second."
}]
)
print(response.content[0].text)
print(f"Input tokens: {response.usage.input_tokens}")
print(f"Output tokens: {response.usage.output_tokens}")
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=16000,
thinking={
"type": "enabled",
"budget_tokens": 10000 # Opus reasons for up to 10K tokens before answering
},
messages=[{
"role": "user",
"content": """
A snail climbs a 20-foot pole. Each day it climbs 3 feet and slides back 2 feet at night.
Except on the last day, when it reaches the top, it doesn't slide back.
How many days does it take?
Show your full reasoning.
"""
}]
)
for block in response.content:
if block.type == "thinking":
print(f"[Opus thinking — {len(block.thinking)} chars]")
elif block.type == "text":
print(block.text)
def route_to_model(prompt: str, complexity: str) -> str:
"""Route to the right model based on task complexity."""
model_map = {
"simple": "claude-haiku-4-5-20251001", # classification, extraction, short tasks
"medium": "claude-sonnet-4-6", # most tasks — best price/quality
"complex": "claude-opus-4-7", # deep reasoning, architecture design
}
model = model_map.get(complexity, "claude-sonnet-4-6")
response = client.messages.create(
model=model,
max_tokens=4096,
messages=[{"role": "user", "content": prompt}]
)
return response.content[0].text
# Simple: Haiku (fast, cheap)
result = route_to_model("Classify this as spam or not: 'You won a prize!'", "simple")
# Medium: Sonnet (most production use cases)
result = route_to_model("Summarize this 10-page technical document.", "medium")
# Complex: Opus (hard reasoning)
result = route_to_model("Design an optimistic concurrency control system for a distributed database.", "complex")
long_document = "..." * 50000 # 50K+ token document
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=2048,
system=[{
"type": "text",
"text": long_document,
"cache_control": {"type": "ephemeral"} # cache the document
}],
messages=[{"role": "user", "content": "What are the key risk factors in this document?"}]
)
# First call: full input cost. Subsequent calls: 90% discount on cached tokens.
print(f"Cache write tokens: {response.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {response.usage.cache_read_input_tokens}")
| Task type | Recommended model | Reason |
|---|---|---|
| Simple classification / extraction | Haiku 4.5 | Overkill for Opus — cost savings are massive |
| Code generation (most tasks) | Sonnet 4.6 | 95% of Opus quality at 5x lower cost |
| Long document Q&A | Sonnet 4.6 | Handles 200K context reliably |
| Complex multi-step reasoning | Opus 4.7 | Significantly better on hard logic chains |
| Math proofs / olympiad problems | Opus 4.7 + thinking | Extended thinking essential for hard math |
| System architecture design | Opus 4.7 | Deeper consideration of tradeoffs |
| High-stakes code review | Opus 4.7 | Catches subtle bugs Sonnet misses |
For cost modeling across models, use the Claude API Cost Calculator. For extended thinking deep-dive, see Extended Thinking with Claude. For migration from OpenAI's o1/o3 to Claude Opus extended thinking, see the Claude API Cookbook.