Claude Tutorials7 min read

How to Use Claude Sonnet 5 to Build AI Agents: A Practical Guide (2026)

Learn how to use Claude Sonnet 5 for agentic workflows — API setup, tool use, browser and terminal automation, and cost-smart agent patterns you can ship in 2026.

Short Answer

To use Claude Sonnet 5 for agents: get an API key, call the Messages endpoint with model claude-sonnet-5, define your tools as JSON schemas, and run a tool-use loop where the model requests a tool, you execute it, you feed the result back, and repeat. Cache your system prompt to keep costs low. Sonnet 5's strength is finishing multi-step tasks autonomously.

Why Sonnet 5 for Agents

An agent is just a model in a loop with tools. What makes an agent useful is whether it can plan, call the right tools, notice when something went wrong, and recover, over many steps, without a human nudging it each turn. Learning how to use Claude Sonnet 5 as an agent means learning that loop.

Sonnet 5 was built for exactly this. Its benchmark gains cluster where agents succeed or fail:

  • Terminal-Bench 2.1 at 80.4%, reliably runs command-line workflows
  • BrowseComp at 84.7%, navigates and researches the web
  • OSWorld-Verified at 81.2%, controls desktop and computer environments

Combined with a 1 million token context, so the agent can hold a lot of state, and low pricing, so you can leave it running, Sonnet 5 is a strong default for building agents in 2026. The full launch context is in everything you need to know about Claude Sonnet 5.

Step 1: Get Access

You have two paths.

  • Direct API: create an API key on the Anthropic platform. Model ID is claude-sonnet-5.
  • Cloud platform: use it through AWS Bedrock, Google Vertex AI, or Azure AI Foundry with your existing billing and governance.
  • For interactive coding agents specifically, Claude Code already runs Sonnet 5 and handles the loop for you, so you may not need to build anything.

    Step 2: The Basic Call

    A minimal request to the Messages API looks like this:

    pythonimport anthropic
    
    client = anthropic.Anthropic()
    
    resp = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=2048,
        messages=[
            {"role": "user", "content": "Summarize this repo's architecture."}
        ],
    )
    print(resp.content[0].text)

    That is a single turn. An agent turns this into a loop with tools.

    Step 3: Give It Tools

    Tools are declared as JSON schemas. The model decides when to call them:

    pythontools = [{
        "name": "run_shell",
        "description": "Run a shell command and return stdout.",
        "input_schema": {
            "type": "object",
            "properties": {"cmd": {"type": "string"}},
            "required": ["cmd"],
        },
    }]
    
    resp = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=2048,
        tools=tools,
        messages=[{"role": "user", "content": "Find and fix the failing test."}],
    )

    When Sonnet 5 wants to use a tool, it returns a tool_use block instead of a final answer. Your job is to execute it and return the result.

    Step 4: The Agent Loop

    This loop is the whole game:

    pythonmessages = [{"role": "user", "content": "Find and fix the failing test."}]
    
    while True:
        resp = client.messages.create(
            model="claude-sonnet-5", max_tokens=2048, tools=tools, messages=messages,
        )
        messages.append({"role": "assistant", "content": resp.content})
    
        tool_uses = [b for b in resp.content if b.type == "tool_use"]
        if not tool_uses:
            break  # model returned a final answer
    
        results = []
        for tu in tool_uses:
            output = execute_tool(tu.name, tu.input)  # you implement this
            results.append({
                "type": "tool_result", "tool_use_id": tu.id, "content": output,
            })
        messages.append({"role": "user", "content": results})

    Sonnet 5's error recovery shines here. When a tool returns an error, it typically diagnoses and adjusts rather than repeating the same failing call, which is what lets it complete long tasks unattended.

    Step 5: Add Guardrails

    A loop that can run shell commands or browse the web needs limits. In production, add:

    • A maximum turn count so the agent cannot loop forever.
    • An allowlist for which commands or domains a tool may touch.
    • Human approval for irreversible actions like deleting data or sending emails.
    • Logging of every tool call so you can audit what the agent did.

    These guardrails are what separate a demo from something you can trust with real access. Sonnet 5's improved prompt-injection resistance helps, but it is not a substitute for your own limits.

    Step 6: Keep Costs Under Control

    Agents are chatty, since every step resends the context, so cost discipline matters.

    LeverSavingWhen to use
    Prompt cachingUp to 90% off repeated inputAlways, for the stable system prompt and context
    Route to Haiku 4.5Lower per-token rateSimple sub-tasks like classification
    Batch API50% offNon-real-time work

    Caching is the biggest single saving, often 70 to 90% of input cost for a multi-step agent. Full patterns are in our cost optimization guide, and the raw numbers are in the Sonnet 5 pricing guide.

    Common Agent Patterns to Start With

    PatternToolsLoop goal
    Coding agentShell, file editLoop until tests pass
    Research agentBrowserGather sources into the 1M context, then synthesize
    Ops agentAPI tools (calendar, tickets, CRM)Execute and reconcile a multi-step workflow

    Start narrow with one clear goal and two or three tools, then expand. An agent that does one thing reliably is far more useful than one that attempts everything and fails unpredictably.

    The Bottom Line

    Building an agent on Claude Sonnet 5 comes down to a simple loop, a handful of well-defined tools, sensible guardrails, and caching for cost. The model's strength in planning, tool use, and error recovery does the heavy lifting; your job is to give it the right tools and the right limits. Start with one narrow, valuable workflow, get it reliable, and expand from there.

    Frequently Asked Questions

    How do I access the Sonnet 5 API?

    Create an Anthropic API key and call the Messages endpoint with model claude-sonnet-5, or use it through AWS Bedrock, Google Vertex AI, or Azure AI Foundry with your existing cloud billing. The direct API is fastest to start; the cloud platforms suit teams that need existing governance and procurement. For coding agents specifically, Claude Code already runs Sonnet 5 and manages the loop for you.

    What makes it good for agents?

    Strong multi-step planning, tool use, and error recovery, reflected in high scores on Terminal-Bench (80.4%), BrowseComp (84.7%), and OSWorld (81.2%). Those benchmarks predict whether an agent finishes a long task rather than stalling. Combined with a 1 million token context to hold state and low pricing to leave it running, Sonnet 5 is a strong default foundation for building autonomous agents in 2026.

    How do I give it tools?

    Declare tools as JSON schemas in the tools parameter. The model returns structured tool_use requests when it wants to act; you execute the tool, return the result as a tool_result, and continue the loop. Repeat until the model returns a final answer instead of a tool call. That request-execute-return cycle is the fundamental mechanism behind every agent you build on the Messages API.

    How do I keep costs low?

    Cache your long system prompt and stable context for up to 90% savings on repeated input, route simple sub-tasks to Haiku 4.5, and batch non-urgent work for 50% off. Caching is by far the biggest lever because agents resend the same context every step, so a chatty multi-step agent can see 70 to 90% of its input cost eliminated by making that context cacheable.

    Can it control a browser?

    Yes. Sonnet 5 scores 84.7% on BrowseComp and 81.2% on OSWorld-Verified, so it reliably navigates pages, fills forms, extracts data, and completes multi-step web workflows when paired with a browser tool or computer-use environment. This makes it well suited to research agents and web-automation agents, which are among the most common and valuable patterns to build.

    Do I need to build the loop from scratch?

    Not always. For coding agents, Claude Code runs Sonnet 5 and handles the loop already. For custom agents, the loop is short and simple to build with the Messages API, and the Anthropic Python and TypeScript SDKs cut the boilerplate. Building it once is worthwhile, because it teaches you exactly how tool use, tool results, and error recovery fit together in an agent.

    Ready to Start Practicing?

    300+ scenario-based practice questions covering all 5 CCA domains. Detailed explanations for every answer.

    Free CCA Study Kit

    Get domain cheat sheets, anti-pattern flashcards, and weekly exam tips. No spam, unsubscribe anytime.