Claude Opus 4 API Example (claude-opus-4-7)

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.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

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.

Basic Opus 4 call

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}")

Extended thinking with Opus 4

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)

Model routing: Haiku → Sonnet → Opus

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")

Opus 4 with prompt caching (reduce cost)

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}")

When Opus 4 vs Sonnet 4.6

Task typeRecommended modelReason
Simple classification / extractionHaiku 4.5Overkill for Opus — cost savings are massive
Code generation (most tasks)Sonnet 4.695% of Opus quality at 5x lower cost
Long document Q&ASonnet 4.6Handles 200K context reliably
Complex multi-step reasoningOpus 4.7Significantly better on hard logic chains
Math proofs / olympiad problemsOpus 4.7 + thinkingExtended thinking essential for hard math
System architecture designOpus 4.7Deeper consideration of tradeoffs
High-stakes code reviewOpus 4.7Catches 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.

Frequently asked questions

What is the model ID for Claude Opus 4?
The model ID is `claude-opus-4-7`. Pass this string to the `model` parameter in any Anthropic SDK call. Opus 4 is the most capable model in the Claude 4 family, best for complex reasoning, long-document analysis, and difficult coding tasks.
When should I use Claude Opus 4 instead of Sonnet?
Use Opus 4 when: (1) the task requires deep multi-step reasoning (math proofs, architecture design, strategic analysis), (2) you're processing documents longer than 50K tokens where comprehension quality matters, (3) you need the highest-accuracy code generation for complex systems, or (4) extended thinking mode is needed for hard reasoning problems. For most API tasks, Sonnet 4.6 delivers 90%+ of Opus quality at ~5x lower cost.
How much does Claude Opus 4 cost vs Sonnet?
Opus 4 costs roughly 5x more than Sonnet 4.6 per token. At high volume this difference is significant. Use the Claude API Cost Calculator to model your specific workload. A common pattern: route 90% of traffic to Sonnet, 10% (hard tasks) to Opus.
Does Claude Opus 4 support extended thinking?
Yes. Opus 4 supports extended thinking (budget_tokens parameter), which gives the model extra reasoning tokens it uses internally before answering. Extended thinking dramatically improves performance on math, logic puzzles, and complex coding. Use thinking_budget of 5000–16000 for most problems.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript