Build an AI agent with Claude in 2026: model selection (Opus 5, Sonnet 5, Fable 5), agent loop, memory, and security. Practical guide with code.

Building an AI agent with Claude comes down to three core pieces: a model capable of calling tools, a loop that executes those calls, and a tier choice that won't blow your bill. The Claude API exposes all of that natively. What trips people up most often isn't the architecture, it's the model selection.
This guide starts from scratch and covers five concrete steps, from model selection through production deployment, with the pitfalls we see repeatedly with our clients. If you come from the no-code side, it's still useful: understanding what happens under the hood saves a lot of billing surprises down the line.
1. Choose the Right Claude Model for Your Agent
Anthropic shifted its lineup this year. The single-flagship approach is gone: the Claude 5 line rests on three purpose-built tiers, Opus 5, Sonnet 5, and Fable 5, all sharing a 1-million-token context window at the API level.
tech-insider.org, Claude Opus 5 vs Sonnet 5 vs Fable 5, August 2026 "Anthropic's Claude 5 lineup replaced the old single-flagship approach with three purpose-built tiers, all sharing a 1-million-token context window at the API level."
According to this article, Opus 5 launched on July 24, 2026. It's the model to reserve for complex reasoning tasks: multi-step planning, orchestration across agents. Sonnet 5 covers most standard agent use cases, customer support, data extraction, ticket summarization. Fable 5 targets speed and cost for high-volume repetitive tasks.
Most teams start with the most expensive model out of habit. It's rarely necessary. On cost itself: Fable 5.1 charges $10 per million input tokens and $50 per million output tokens, excluding cache.
VentureBeat, September 1, 2026 "Fable 5.1 retains Fable 5's headline API rates: $10 per 1 million input tokens and $50 per million output."
According to VentureBeat, cache reads cost 75% less with this version. Concretely: if your agent rereads the same system prompt on each call, enabling cache changes your month-end invoice.
Pitfall: Going straight to Opus 5 "because it's the most powerful" for an agent that just does simple classification. Test on the cheapest tier first. Scale up only if output quality truly demands it.
2. Define the Tools Your Agent Can Call
An agent without tools is just a chatbot. Nothing more. The difference comes down to this: you describe each available function with a JSON schema, and the model decides on its own when to call it.
{
"name": "get_order_status",
"description": "Fetches the status of an order by its ID",
"input_schema": {
"type": "object",
"properties": {
"order_id": { "type": "string" }
},
"required": ["order_id"]
}
}
The model returns a tool_use block when it judges a call necessary. Your code executes the actual function, then returns the result in the next conversation turn. That's the entire principle behind Claude API's native function calling, no third-party framework needed to get started.
An agent can also request two or three tools in the same turn, in parallel. That's useful for an agent that needs to cross-reference a Shopify order and a support ticket before responding, for example.
3. Build the Agent Loop
This is where most tutorials stop too short. An agent isn't a single API call. It's a loop that runs as long as the model requests tools.
The typical flow:
- Send the user message and the list of available tools
- If the response contains a
tool_use, execute the corresponding function - Return the result to the model in a new message
- Repeat until the model returns a final response with no tool calls
Pitfall: Forgetting a turn limit. A poorly bounded agent can loop on a tool that always returns an error, burning tokens with each iteration and never stopping. A ceiling of ten turns is enough for most cases we handle at our clients.
We went deeper into this kind of drift in our article on the cost of AI agents in production: the unbounded loop is one of the most common causes of exploding invoices.
4. Add Memory and Persistent Context
An agent that forgets everything between sessions isn't very useful for business workflows. Two options: manage memory yourself by summarizing and reinserting conversation history, or go through an MCP server that exposes an external data source as persistent memory.
We covered MCP protocol details in our MCP server definition. Quick summary: MCP standardizes how an agent connects to a database, CRM, or file system, without rewriting a connector for every tool and every client.
With prompt caching 75% cheaper on Fable 5.1, keeping long context becomes far less financially painful than it was a few months ago. That changes the math for agents that need to remember an entire conversation history.
5. Secure and Monitor the Agent Before Production
Before hooking an agent up to real data, three checks are essential. Limit each tool's permissions to what's strictly necessary. Log every tool call with its token cost. Plan a degraded mode if the API returns an error. These three checks form the foundation of any production AI agent, regardless of the model behind it.
We saw this scenario with one e-commerce support client: a tool that consistently returned errors, no turn limit, and a session that ran all night before anyone noticed.
Anthropic itself documents this security side. Its teams published research on how Claude agents, in a controlled environment, helped identify and fix alignment flaws, a sign that the reliability of autonomous agents goes well beyond the scope of a technical tutorial.
If your team has fewer than five developers, don't build an observability system from scratch. A Postgres table with cost per call and tool name is enough to start. You can refine it later.
Conclusion
Three things to remember. The choice of model, Opus 5, Sonnet 5, or Fable 5, weighs more heavily on your budget than the agent loop architecture itself. The agent loop must always have a turn ceiling, no exceptions. And persistent memory should be planned from the start, not rushed through after the first production bug.
If you want us to look together at how to hook an AI agent to your existing business tools, the fstck.co team supports this kind of project end-to-end.
Frequently Asked Questions
Which Claude model should I choose for an agent calling many tools in parallel?
Sonnet 5 covers most tool orchestration use cases. Reserve Opus 5 for scenarios requiring complex multi-step reasoning, and test Fable 5 if latency and cost outweigh precision.
How much does a Claude-based AI agent cost in production?
It depends on the model and cache token volume. Fable 5.1 charges $10 per million input tokens and $50 per million output tokens on non-cached tokens, with a 75% reduction on cache reads.
Should I use MCP or Claude API's native function calling?
Native function calling is enough for an agent with a few fixed tools. MCP becomes interesting when you want to expose the same data source to a Slack agent and a support agent without duplicating connection code.
Can a Claude AI agent work without a framework like LangChain?
Yes. The basic agent loop fits in about fifty lines of code around the Claude API. A framework becomes useful mainly when you're orchestrating three or more agents together, not for a standalone agent.
What's the main limitation of this approach?
It doesn't handle coordination between three or more agents working on the same task in parallel. For that, you need an additional orchestration layer, which goes beyond the scope of a first working agent.


