Skip to main content
Tutorial AI Tools

How to Run Agentic Workflows With Claude Sonnet 5

Claude Sonnet 5 makes agents cheaper on paper, but token math, effort levels, and one tokenizer change decide whether you actually save money.

AnIntent Editorial

9 min read
How to Run Agentic Workflows With Claude Sonnet 5

Photo by Pankaj Patel on Unsplash

By the end of this walkthrough you will have a working Claude Sonnet 5 agentic loop that calls tools, uses adaptive thinking at the right effort level, and stays inside a token budget you can defend to finance. This is the setup to use when you are moving a Sonnet 4.6 agent to the new model, or building a fresh browser/terminal agent that needs to run autonomously for more than a handful of turns.

The model ID is claude-sonnet-5, and Anthropic launched it on June 30, 2026 with introductory pricing of $2 per million input tokens and $10 per million output tokens through August 31, 2026, moving to $3/$15 after that. The 1-million-token context window is large enough to load a full codebase in a single prompt, and adaptive thinking is on by default with effort set to high on both the API and Claude Code.

Why This Model Is Worth Rewiring Your Agent For

Sonnet 5's most interesting number is not the price. It is the +20.7-point jump on Terminal-Bench, from Sonnet 4.6's 55.4% to 76.1%, which DataCamp flagged as the largest single-benchmark improvement at launch. That benchmark measures multi-step agentic coding inside real terminal environments, which is the workload most production agents actually run.

On agentic search, Anthropic's benchmark set shows Sonnet 5 hitting 84.7% on BrowseComp and 80.4% on Terminal-Bench 2.1, the latter beating Opus 4.8's 74.6%. On SWE-bench Pro it lands at 63.2% against Opus 4.8's 69.2%, and on OSWorld-Verified it scores 81.2% versus Opus's 83.4%. The pattern is consistent: Sonnet 5 wins pure agent-loop benchmarks and loses complex coding by roughly six points.

That gap matters for how you route work. Use Sonnet 5 for the outer agent loop, tool orchestration, and browser tasks. Reserve Opus 4.8 for the deep-reasoning inner calls where six points on SWE-bench Pro pay for themselves.

Set Up the Claude Sonnet 5 API Call

This is the minimum viable Claude Sonnet 5 API tutorial you actually need. Install the SDK, set your key, and target the model ID directly.

from anthropic import Anthropic

client = Anthropic()

response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=8192,
    thinking={"type": "enabled", "effort": "high"},
    tools=[browser_tool, terminal_tool],
    messages=[{"role": "user", "content": task_prompt}],
)

The effort parameter is the single most important knob. Anthropic documents four levels, mapping to the same scale as Claude Code's /effort command, with high as the API default. Higher effort spends more thinking tokens per turn and takes more turns before returning.

For output limits, standard max_tokens caps at 128k. If you need longer synthesis passes, add the beta header output-300k-2026-03-24 to raise the ceiling to 300k tokens through the batch API, per Anthropic's launch notes. Do not enable it by default. The 300k ceiling exists for report generation, not for chat turns.

The Tokenizer Change Almost Nobody Budgets For

Sonnet 5 ships with the updated tokenizer first introduced with Opus 4.7. DataCamp reports the same input text can map to roughly 1.0 to 1.35× more tokens depending on content type, which erases a chunk of the headline price cut before you write a single tool call. Code-heavy inputs and JSON payloads tend to sit at the higher end of that range.

Anthropic chose the $2/$10 introductory price so switching from Sonnet 4.6 is roughly cost-neutral, not a flat discount. Audit your actual token counts on a representative traffic sample before you tell anyone this migration saves money.

There is a second, worse trap. Artificial Analysis measurements cited by BuildFastWithAI show Sonnet 5 burns about 40% more output tokens per Intelligence Index task than Sonnet 4.6, and uses roughly 3× the agentic turns on knowledge-work evals, with max effort using around 6× the turns of low effort on GDPval-AA. At standard $3/$15 pricing, real per-task cost works out to about $2.29 per Intelligence Index task, which is roughly 15% more than Opus 4.8 despite Opus's higher per-token price.

That is the number to remember. Cheaper per token does not mean cheaper per task.

Pick the Right Effort Level for the Job

Effort is where most teams over- or under-spend. The API default is high, which is correct for agents that need to reason across many tool calls, but wrong for classification, extraction, or single-turn rewrites.

  • Low effort: single-turn generation, formatting, extraction, cheap classification.
  • Medium effort: retrieval-augmented answers, short chained tool calls, code review of a single file.
  • High effort: multi-step browser or terminal agents, cross-file refactors, research tasks with more than five tool calls.
  • Max effort: rare. Reserve for tasks where a wrong answer costs more than roughly $10 of extra tokens.

Because max effort uses about 6× the turns of low effort on GDPval-AA per the BuildFastWithAI breakdown, setting it globally is the fastest way to blow through a monthly budget. Route per-task, not per-deployment.

Build the Agent Loop

A working agent loop for Claude Sonnet 5 has four moving parts: a system prompt that names the tools and the stopping condition, a tool schema, a while loop that keeps calling the API until the model returns a stop_reason of end_turn, and a hard cap on total turns.

MAX_TURNS = 25
messages = [{"role": "user", "content": task_prompt}]

for turn in range(MAX_TURNS):
    resp = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=8192,
        thinking={"type": "enabled", "effort": "high"},
        tools=TOOLS,
        messages=messages,
    )

    messages.append({"role": "assistant", "content": resp.content})

    if resp.stop_reason == "end_turn":
        break

    if resp.stop_reason == "tool_use":
        tool_results = run_tools(resp.content)
        messages.append({"role": "user", "content": tool_results})

The 25-turn cap is deliberate. On knowledge-work evals, Sonnet 5 already uses roughly 3× the turns of Sonnet 4.6. Without a ceiling, a runaway browser agent can rack up a hundred turns before you notice, especially on high effort. Log turn counts and token totals to whatever observability stack you use for enterprise AI spend.

How Claude Sonnet 5 vs Opus 4.8 Actually Shakes Out

The claude sonnet 5 vs opus 4.8 question resolves cleanly if you look at the benchmark spread rather than the price sheet. Sonnet 5 wins Terminal-Bench 2.1 (80.4% vs 74.6%), edges GDPval-AA v2 knowledge-work Elo (1,618 vs 1,615), and matches Opus on BrowseComp within noise. Opus 4.8 wins SWE-bench Pro by six points and OSWorld-Verified by 2.2 points, according to the benchmark table published by BuildFastWithAI.

Anthropic's own framing is unusually direct. In its launch post, the company said Opus 4.8 remains the model of choice for higher accuracy, while positioning Sonnet 5 as a lower-priced option of much higher quality than what was previously available, a line reproduced in TechCrunch's launch coverage.

Pricing tells the same story from the other side. Opus 4.8 lists at $5 input and $25 output per million tokens, so Sonnet 5 at standard $3/$15 is roughly 40% of Opus's per-token cost, per the BuildFastWithAI pricing summary. Combined with Sonnet 5's higher token burn per task, the actual all-in gap is much narrower than the sticker price suggests. Route hard reasoning to Opus, orchestration to Sonnet.

One Piece of Context Everyone Missed

BuildFastWithAI notes that Claude Fable 5 and Mythos 5 remain suspended under a US Commerce Department export-control order that took effect June 12, 2026, with no confirmed return date. Sonnet 5 is effectively the best widely available Claude model right now, and the June 30 launch timing was not accidental. If you were planning to wait for Mythos, that plan has no delivery date.

The Failure Mode That Will Bite You First

The most common way a Sonnet 5 agent fails in production is not a bad answer. It is a silent turn-count explosion when a tool returns a large payload the model then re-reads on every subsequent turn.

Because the context window is 1 million tokens, a 200,000-token HTML dump from a browser tool does not throw an error. It sits in the message history, gets re-processed on every turn, and each turn now costs you the same 200,000 input tokens over again. Combined with the roughly 3× turn increase on knowledge-work loops that Artificial Analysis measured, one careless browser call can multiply your bill by ten.

The fix is boring and mandatory. Summarize tool outputs above roughly 4,000 tokens before appending them to the message history, or store the raw output externally and pass only a handle. This one change does more for your cost curve than dropping to medium effort.

Safety Behavior Worth Knowing Before You Ship

Anthropic states that Sonnet 5 shows a lower rate of undesirable behaviors than Sonnet 4.6, is safer in agentic contexts, and has a much lower ability to perform cybersecurity tasks than the current Opus models. Lovable co-founder Fabian Hedin, quoted by TechCrunch, said the model refuses unsafe requests cleanly and consistently in his team's early usage.

The honest caveat is that alignment is not at Opus 4.8 or Mythos Preview levels. For agents that touch payments, production infrastructure, or user data at scale, keep the same review gates you would use for Sonnet 4.6. Read Anthropic's system card alongside your own red-team traces before flipping traffic. For deeper reading on this class of trade-off, our AI Safety archive collects related analysis.

What to Do Next

Migrate one non-critical agent first, log token counts and turn counts per task for a week, and compare the per-task cost to your Sonnet 4.6 baseline before rerouting production traffic. If the numbers land where Artificial Analysis says they will, keep Sonnet 5 for orchestration and browser work, and route SWE-bench-style deep coding to Opus 4.8. For a broader view of where Claude Code sits against alternatives, our comparison of Cursor, Copilot, and Claude Code is the next place to look.

Frequently Asked Questions

What is the model ID for Claude Sonnet 5 on the API?

The model ID is `claude-sonnet-5`. Anthropic launched it on June 30, 2026 as the default model for Free and Pro plans, and it is also available in Claude Code and on the Claude Platform.

How large is Claude Sonnet 5's context window and max output?

Sonnet 5 has a 1 million token context window, large enough to hold a full codebase in one prompt. Standard max output is 128k tokens, extendable to 300k via the batch-API beta header `output-300k-2026-03-24`.

When does Claude Sonnet 5 introductory pricing end?

Introductory pricing of $2 per million input tokens and $10 per million output tokens runs through August 31, 2026. After that, standard pricing is $3 input and $15 output per million tokens.

Is Claude Sonnet 5 actually cheaper than Opus 4.8 per task?

Per token, Sonnet 5 at standard pricing is about 40% of Opus 4.8's cost. But Artificial Analysis found Sonnet 5's real per-task cost on Intelligence Index tasks is roughly $2.29, about 15% more than Opus 4.8, because it burns more output tokens and takes more agentic turns.

What is Claude Sonnet 5's knowledge cutoff?

Anthropic lists Sonnet 5's training cutoff as January 2026. For any topic newer than that, agents should rely on browser or search tools rather than the model's parametric knowledge.

Written by

AnIntent Editorial

AnIntent is an independent technology and automotive publication. Our editorial team researches every article from live primary sources, cross-checks key facts across multiple references, and cites claims inline so readers can verify them directly. We cover smartphones, laptops, EVs, gaming hardware, AI tools, and more — with no sponsored content and no paid placements.

More from AnIntent

Keep reading

All articles