How-To/How to Build an AI Agent With Claude

How to Build an AI Agent With Claude

An AI agent is a model that takes a goal, decides what to do, calls tools, and loops until the job is done. Claude is the strongest model in its class for this, with first-class tool use, the Model Context Protocol it created, and the best agentic coding on the market. This guide walks you from model choice to a working tool-using agent you can run in production.

Last updated: Jun 20, 2026

What an AI agent actually is

A chatbot answers. An agent acts. The difference is the loop: the model receives a goal, decides on an action, calls a tool, reads the result, and decides again. It keeps going until the task is complete or it hits a stop condition. Your job as the builder is to give Claude good tools, a clear goal, and a loop that runs those tool calls and feeds the results back.

Claude is well suited to this for three concrete reasons. Tool use is native, not bolted on. Anthropic created the Model Context Protocol (MCP) and supports it first-class, so connecting external tools is a solved problem. And agentic coding is the strongest in its class, which matters because most useful agents end up reading code, querying data, or manipulating files.

Step 1: Pick the right model

Match the model to the work. You do not need the flagship for every step, and over-spending on a simple agent adds up fast.

  • Sonnet 5: the current default on Claude Free and Pro, and available in Claude Code and the API. Its introductory API price is $2 in / $10 out per million tokens through August 31, 2026, then $3/$15. Start here for most agents and benchmark against your own tool-use cases.
  • Opus 4.8 (May 28 2026): the flagship, best agentic coding, plans before it edits, and holds up over long autonomous runs. 1M token context. $5 in / $25 out, or $10 / $50 in Fast Mode. Use it for complex multi-step agents and big-codebase work.
  • Haiku 4.5: fastest and cheapest at $1 in / $5 out, 200K context. Use it for high-volume, low-complexity steps like classification or routing inside a larger agent.

Common mistake: selecting a premium model before measuring the workload. Start agent development on Sonnet 5, then change models only when your evaluations show a quality, latency, or cost reason. Fable 5 was restored globally on July 1, 2026. Mythos 5 is limited to approved Project Glasswing organizations, so do not make it a dependency unless your organization has that access.

Step 2: Define the goal and the stop condition

An agent without a stop condition either quits too early or loops forever. Write the goal as a single sentence the model can check itself against, and define what 'done' looks like. Put both in the system prompt.

A workable system prompt skeleton: 'You are an agent that resolves customer support tickets. For each ticket, look up the order, check the policy, and either issue the fix or escalate. You are done when the ticket has a resolution or an escalation note. Do not invent order details. If a tool returns nothing, say so and escalate.' Specific, checkable, and it tells the model how to behave when a tool fails.

Step 3: Give Claude its tools

Tools are where the agent gets its power. Each tool is a function with a name, a description, and an input schema. Claude reads the description to decide when to call it, so the description is doing real work. Write it like you are briefing a competent new hire: what it does, when to use it, what it returns.

  • Name tools by what they do: get_order_status, search_docs, create_ticket. Avoid vague names like 'helper' or 'tool1'.
  • Write descriptions for the model, not for yourself. State the trigger condition: 'Use this when the user references an order number.'
  • Keep input schemas tight. Required fields required, enums where the values are fixed, no free-text where a structured value will do.
  • Make tools return clean, structured results. Garbage in the tool result becomes garbage in the agent's next decision.

Step 4: Connect external systems with MCP

Hand-writing a tool wrapper for every system you want the agent to touch does not scale. The Model Context Protocol is Anthropic's open standard for connecting models to external tools and data sources, and Claude supports it first-class. Instead of bespoke glue per integration, you point Claude at MCP servers and the tools show up.

Practically: use an existing MCP server for things like databases, file systems, search, or your SaaS tools, or stand up your own for an internal system. The agent then calls those tools the same way it calls any other tool. This is the single biggest leverage point in agent building, because integration is usually the expensive part.

Step 5: Build the agent loop

The loop is the engine. It is the same shape whether you write it by hand against the API or use a framework.

  • Send the goal plus the available tools to Claude.
  • If Claude returns a tool call, execute that tool in your code.
  • Feed the tool result back to Claude as the next message.
  • Repeat until Claude returns a final answer with no tool call, or you hit the stop condition.
  • Cap the number of iterations so a stuck agent cannot run unbounded.

Pro tip: log every tool call, its input, and its result. When an agent does something wrong, the failure is almost always a bad tool result or a tool description the model misread, not the model 'being dumb.' Your logs make that obvious in seconds. Build logging in from the first run, not after the first incident.

Step 6: Use Claude Code if you are building a coding agent

If your agent's job is to read, write, or refactor code across a real codebase, you do not have to build the harness from scratch. Claude Code is Anthropic's dedicated CLI, built around agentic coding with Claude. It already handles the loop, file access, and tool use over a codebase. Reach for it before reinventing the file-editing and command-running machinery yourself.

Step 7: Cut cost and latency before you ship

Agents are token-hungry because every loop resends context. A few levers cut the bill hard without changing your logic.

  • Prompt caching: up to 90% off on cached input. Cache the stable parts of your prompt (system prompt, tool definitions, long reference docs) so you stop paying full price to resend them each loop.
  • Batch processing: 50% cheaper for work that does not need to be real-time, like processing a queue of tickets overnight.
  • Right-size the model per step: route simple sub-steps to Haiku 4.5 and reserve Sonnet or Opus for the calls that need the reasoning.
  • Lean on the 1M context on Opus 4.8 and Sonnet 4.6, at flat rates with no surcharge, when the agent genuinely needs to reason over a large codebase or document set.

What Claude-based agents are best at

Play to the model's strengths. Claude agents are strongest at agentic coding, long-context analysis over big codebases and document sets, production assistants, retrieval-augmented generation, and general tool-using workflows. Claude also handles web search and image input, which is useful when your agent needs to look things up or read a screenshot. Note that Claude does not generate images; it is text and code focused. Build around that and your agent will be reliable rather than fighting the tool.

THE CLAUDE INSIDER

Get the Claude playbook in your inbox.

One weekly email for Claude and Claude Code users. Real workflows, no hype. Subscribe and we send you The Claude Power-User Cheatsheet.

Frequently Asked Questions

RELATED