Case Studies Book a 30-minute discovery call

How to build an AI agent: the loop, the tools, and the hard parts

An AI agent is a language model that uses tools in a loop to finish a goal, and most of the difficulty lives in the loop rather than the model. This guide gives you the correct mental model, a step-by-step build path, the 2026 framework options, and an honest account of why agents fail in production.

Kanika Mathur
By Kanika Mathur, Head of Service Delivery
Reviewed by Resourcifi engineeringPublished May 4, 2026Updated May 4, 202612 min read
AI
Bright workshop desk flat lay with colorful modular electronic components, a small circuit board and neat tools on a light surface
Key takeaways

How to build an AI agent: the short version

  • An AI agent is a language model that uses tools in a loop: it plans, takes an action (usually a tool call), observes the result, and repeats until the goal is met or a stop condition is hit. Anthropic defines an agent as an LLM that dynamically directs its own process and tool usage.
  • The most-missed distinction is agent versus workflow. A workflow runs LLMs and tools through predefined code paths; an agent lets the model decide the path at runtime. Most production systems are workflows, and that is often the more reliable choice.
  • Every agent has six parts: a model, tools defined by JSON schema, short-term memory (the context window), long-term memory via retrieval, planning, and a loop controller with guardrails and a hard iteration cap.
  • Build it in order: scope one narrow use case, pick a capable model, define tools, add retrieval, write the loop, add evals, add guardrails, then deploy with tracing. LangChain reports about 94% of organizations running agents in production already have observability.
  • Agentic systems trade cost and latency for capability. Anthropic measured that agents use roughly 4x more tokens than chat, and multi-agent systems about 15x more, so default to a single agent and add complexity only when evals justify it.

What an AI agent actually is, and what it is not

An AI agent is a large language model that uses tools in a loop to accomplish a goal: it plans, takes an action (usually a tool or function call), observes the result, and repeats until the task is done or a stop condition is hit. Anthropic's working definition is exactly this, where an agent is an LLM that dynamically directs its own process and tool usage. The building block underneath is the augmented LLM, a model equipped with retrieval, tools, and memory that generates its own queries and decides what to keep.1

The single most useful distinction here is agent versus workflow, and most teams get it wrong. In a workflow, the LLM and its tools are orchestrated through predefined code paths that the developer fixes in advance. In an agent, the model decides its own path at runtime. Many systems shipped as "AI agents" are actually workflows, and for well-understood tasks a workflow is usually the more reliable and cheaper choice. Reach for a true agent only when the path cannot be known ahead of time. For the full definitional treatment of what agents are and where they fit, see our pillar on what AI agents are.

An agent also differs from a plain chatbot. A chatbot is largely a stateless request and response: it answers one query, holds no memory across turns, calls no tools, and takes no action in external systems. An agent is multi-step and stateful, and it can act, writing to a database, calling an API, or sending a message, rather than only talking. The contrast below sets the three apart.

Chatbot, workflow, and agent compared
The line that matters is who controls the path. A chatbot has none, a workflow fixes it in code, and an agent lets the model decide at runtime.
How a chatbot, a workflow, and an agent differ
PropertyChatbotWorkflowAgent
Controls the pathNo path; single turnDeveloper, fixed in codeThe model, at runtime
Uses toolsNoYes, predefined stepsYes, model-selected
StateMostly statelessCarried between stepsCarried across the loop
Can take actionsNo, it answersYes, on railsYes, decides which
Best fitFAQ and simple Q and AKnown, repeatable tasksOpen-ended tasks
Source: Anthropic, Building Effective Agents (2024), agent versus workflow distinction.

The core architecture: the agent loop and its six parts

Every agent runs the same loop: the model plans, decides whether to call a tool or finish, executes the chosen tool, observes the result, appends it to the context, and repeats under a hard iteration cap. Underneath that loop sit six parts: the model, the tools, short-term memory, long-term memory, planning, and the loop controller with its guardrails. Get the loop and the tool interface right and the model is rarely the limiting factor.

The mechanical loop with a tool-using model is concrete. You pass the model a set of tools; it returns a tool-use request with the function name and arguments; your code runs the function and returns the result; you call the model again with that result appended to the message history. The model can request several tools in parallel, and when tool choice is automatic it decides each turn whether to act or answer. Anthropic's guidance is to design this agent-computer interface with the same care as a human interface: clear tool names, precise descriptions, example usage, and edge cases. On real benchmarks they spent more time optimizing the tools than the prompt.2

The six parts break down as follows.

  • Model. The reasoning engine that plans and decides. Start with the most capable model to set a quality ceiling, then trade down to cheaper or faster models per task where evals show they hold up.3
  • Tools and function calling. Functions you define with a name, a description, and a JSON schema for inputs. Prefer a few well-described tools over many overlapping ones, and use strict schema conformance where available.2
  • Short-term memory. The running message history, intermediate tool results, and scratchpad reasoning held in the context window. This is what lets the agent reference earlier steps within a single task.
  • Long-term memory. Knowledge that outlives one run, usually through retrieval-augmented generation: documents are chunked, each chunk is turned into an embedding by an embedding model and stored in a vector database, and at query time the most similar chunks are returned as context.4 For the mechanics in depth, see our guide to retrieval-augmented generation.
  • Planning. How the goal becomes steps. In a true agent the model decides the next step from feedback; in a workflow the steps are fixed. Common patterns include prompt chaining, routing, orchestrator-workers, and evaluator-optimizer, where one call generates and a second critiques.1
  • Loop controller and guardrails. The code that runs the observe-and-act cycle, enforces a maximum iteration count and stop conditions, validates inputs and outputs, and can pause for human review at checkpoints. Anthropic recommends a maximum number of iterations to keep control.1
The agent loop as a six-step cycle
A goal enters, the model plans, then it acts and observes in a loop until it decides to finish. Guardrails and a hard iteration cap wrap every pass.
The observe-and-act loop, step by step
StepWhat happens
1. GoalA task and any context enter the loop.
2. PlanThe model decides the next move from the goal and what it has seen.
3. DecideCall a tool, or finish and answer. If finishing, return the result.
4. ActYour code executes the chosen tool with the model's arguments.
5. ObserveThe tool result is appended to the context for the next pass.
6. GuardValidate output, check the iteration cap and stop conditions, then loop to step 2 or stop.
Source: Anthropic, Building Effective Agents (2024); OpenAI Agents SDK, agent loop documentation (2025).

How to build an AI agent, step by step

Build an AI agent in eight ordered steps: pick a narrow, measurable use case; choose a capable model; define your tools with clean JSON schemas; add retrieval and memory; write the observe-and-act loop with a hard iteration cap; add outcome-based evals; add guardrails; then deploy to real users with tracing. The biggest timeline driver is scope clarity and data readiness, not raw engineering effort.

Each step has a clear "what good looks like" marker, and the early steps cost the least but decide the most.

  1. Pick a narrow, valuable use case. Start where the task is well-scoped, repetitive, and measurable, and where a wrong answer is low-risk, such as drafts, summaries, or internal triage, before automating high-stakes actions.3
  2. Choose the model. Begin with the most capable model to set a quality baseline, then trade down per task where evals pass.3
  3. Define the tools. Write each tool with a precise name, description, and JSON schema, and treat the interface as a product. Few clear tools beat many overlapping ones.2
  4. Add retrieval and memory. Wire in retrieval for domain knowledge plus short-term working memory. For knowledge-heavy agents the bottleneck is usually retrieval quality rather than the model.4
  5. Write the loop. Implement observe-and-act with a hard maximum-iteration cap and clear stop conditions, or adopt a framework that provides the loop for you.
  6. Add evals. Build a small eval set from day one and judge final outcomes and state, not just whether the right API was called. Pair automated evals with human review, which catches what automation misses.5
  7. Add guardrails. Layer input and output validation: relevance and safety filters, PII screening, and tool-risk gates that route high-impact actions through human approval.3
  8. Deploy and monitor. Ship to a small set of real users, then add tracing over the full multi-step trajectory of tool calls, latency, cost, and failures, and iterate.6

Observability is now table stakes rather than a nicety. LangChain's 2025 survey found that about 94% of organizations already running agents in production have some form of observability in place.6 If you would rather have a team build and run this end to end, see how our AI agent development pods scope, build, and operate agents.

Frameworks and build-vs-buy in 2026

You have three broad paths: hand-code the loop for maximum control, adopt a framework that gives you the loop, tools, and orchestration out of the box, or use a low-code platform for speed at the cost of control. The main framework options in 2026 are the OpenAI Agents SDK, native tool use on Anthropic's Claude, LlamaIndex for data-heavy agents, and graph or role-based frameworks such as LangGraph and CrewAI. Frameworks move fast, so treat the positioning below as a 2026 snapshot.

The table compares the primary options. Vendor SDKs are the most accurate to cite; positioning for the broader ecosystem changes release to release, so verify the current docs before committing.

Agent framework and SDK options in 2026
Framework or SDKWhat it isBest for
OpenAI Agents SDKLightweight, code-first SDK that provides the agent loop, tools, handoffs between agents, guardrails, sessions, and tracing as primitives. Optimized for OpenAI models.Teams on OpenAI models wanting a thin, code-first harness with built-in handoffs.
Anthropic tool use and Claude Agent SDKNative tool-use API with a tool-call and tool-result loop, plus the Agent SDK extracted from Claude Code. Integrates with the Model Context Protocol and supports parallel tool calls.Teams on Claude wanting native tool use, MCP, and Claude Code style agents.
LlamaIndexData and retrieval-first framework that now ships agent primitives such as function and ReAct agents and tool retrievers.Agents whose main job is reasoning over your private, indexed data.
LangGraphGraph-based, state-machine framework built for explicit control, durable state, and human-in-the-loop checkpoints. Model-agnostic.Production systems needing auditability, persistence, and tight control.
CrewAIRole-based multi-agent framework where you define agents as roles with tasks. Low learning curve.Fast multi-agent prototypes that split into clear specialist roles.

For production, the boring choice is often the right one. A workflow or a single agent on a thin SDK tends to beat a heavy multi-agent framework on reliability and cost, so start simple and add framework machinery only when a concrete need shows up. The IBM framing of code versus framework versus platform is a useful map: hand-coding gives the most control and the most manual work, frameworks give you scaffolding, and platforms trade control for speed.7

Why agents fail in production, and how to harden them

Agents fail in production for five recurring reasons: cascading errors across multi-step runs, genuinely hard evaluation, hallucinated tool calls, cost and latency, and a new class of security risks from autonomy plus tools. None are reasons to avoid agents; they are the work that separates a demo from a system. Hard iteration caps, strict tool schemas, outcome-based evals, least-privilege tools, and human approval on risky actions cover most of it.

Take them in turn. Cascading failure is the structural one: because an agent runs many steps, a single bad tool selection or misread result can compound through the rest of the run, and systems get more brittle as the step count grows. Evaluation is hard because agents can take completely different valid paths to the same goal, so you usually cannot check whether they followed the "correct" steps; judge the final outcome and state, and add human review. Hallucinated tool calls and fabricated outputs are a leading failure mode and can even fool an automated evaluator that reads only the trajectory, so validate tool outputs, use strict schemas with retries, and ground the agent with retrieval.5

Cost and latency are a deliberate trade. Anthropic measured that agents use roughly 4x more tokens than a chat interaction, and multi-agent systems about 15x more, which is why a single agent should be the default and multi-agent reserved for genuinely high-value, parallelizable work.5 The same research used an orchestrator-worker shape, where a lead agent spawns specialized subagents, but only because the task justified the token cost; for the full single-versus-multi treatment see our guide to multi-agent systems.

Token cost grows fast as you add autonomy
Relative token use against a single chat interaction, from Anthropic's engineering measurements. The jump to multi-agent is the reason to default to one agent.
Relative token use by system type Anthropic measured that a single agent uses about 4 times the tokens of a chat interaction, and a multi-agent system about 15 times. 0x5x10x15x ChatSingle agentMulti-agent 1x~4x~15x
Data behind this chart
System typeRelative token use
Chat interaction1x (baseline)
Single agentAbout 4x
Multi-agent systemAbout 15x
Source: Anthropic, How we built our multi-agent research system (2025).

Security is the newest hard part. Autonomy plus tools plus memory creates attack classes beyond plain model risk. The OWASP Top 10 for Agentic Applications, released in December 2025, names goal hijacking, tool misuse, identity and privilege abuse, and memory poisoning, and prompt injection remains the dominant driver of agentic failures in production.8 Mitigate with least-privilege tool scopes, human approval on high-risk actions, input and output filtering, and sandboxing for any code execution. For the full controls, see our guide to AI security best practices.

Frequently asked

How to build an AI agent: questions

What is an AI agent?
An AI agent is a large language model connected to tools and memory that runs in a loop: it plans, takes an action such as a tool or function call, observes the result, and repeats until the goal is met or a stop condition is hit. Unlike a chatbot, it is stateful, multi-step, and can take real actions in external systems. Anthropic defines it as an LLM that dynamically directs its own process and tool usage.
How do you build an AI agent?
Pick a narrow, measurable use case, choose a capable model, define your tools with clear JSON schemas, add retrieval and memory, implement the observe-and-act loop with a hard iteration cap, add outcome-based evals and guardrails, then deploy to real users with tracing. Start with a single agent and add complexity only when evals justify it. Scope clarity and data readiness usually drive the timeline more than raw engineering effort.
What is the difference between an AI agent and a chatbot?
A chatbot answers one message at a time and is largely stateless, with no tool use and no ability to act. An AI agent is multi-step and stateful: it can call tools, query data, and take actions, and it decides its own next step from the results it sees. In short, a chatbot talks and an agent does.
What tools do you need to build an AI agent?
At minimum you need a language model with function or tool calling, a set of tools defined by JSON schema, and a loop that executes them. Most production builds add a vector database for retrieval, an orchestration layer or framework such as the OpenAI Agents SDK, the Claude Agent SDK, LlamaIndex, or LangGraph, and observability tooling for tracing, cost, and reliability.
How long does it take to build an AI agent?
As an industry range, a narrow pilot on clean data can ship in roughly 2 to 4 weeks, a mid-complexity custom agent typically takes about 8 to 16 weeks to production, and an enterprise multi-workflow program runs 3 to 6 months. Scope clarity and data readiness move the timeline more than raw engineering effort does, so treat these as planning benchmarks rather than a guarantee.
Kanika Mathur

Kanika Mathur

Head of Service Delivery, Resourcifi

Kanika Mathur is Head of Service Delivery at Resourcifi, where her engineering pods build tool-using agents and ship the unglamorous parts that decide whether one survives contact with production: tight tool schemas, hard iteration caps, outcome-based evals, and human approval on risky actions. She has watched more agents fail on a bad tool description than on a weak model, which is the bias this guide carries throughout.

Resourcifi on LinkedIn →

Sources

  1. Anthropic, Building Effective Agents (2024).
  2. Anthropic, Tool use with Claude (2025).
  3. OpenAI, A Practical Guide to Building Agents (2025).
  4. LlamaIndex, Introduction to RAG (2025).
  5. Anthropic, How we built our multi-agent research system (2025).
  6. LangChain, State of Agent Engineering (2025).
  7. IBM, How to Build an AI Agent (2025).
  8. OWASP Gen AI Security Project, Top 10 Risks and Mitigations for Agentic AI Security (2025).
Keep reading
Related guides worth your time
Agents & RAG Agentic RAG: When to Use It and How to Build It Agentic RAG explained: how it differs from naive and advanced RAG, the key patterns like corrective RAG and self-RAG, the... Read guide Agents & RAG AI Agent for Fintech: Risk, Compliance, Ops, Customer AI agents in finance: fraud, AML, KYC and servicing use cases, how to build with money-movement guardrails and human appr... Read guide Agents & RAG AI Agent for Healthcare: Use Cases, Governance & Implementation AI agents in healthcare: the use cases that pay off first, how to build one HIPAA-safe on FHIR with clinician review, and... Read guide Agents & RAG AI Agent for HR: Recruiting, Onboarding, People Ops AI agents for HR: screening, employee Q and A and onboarding use cases, how to build them, and the bias, EEOC and Local L... Read guide Agents & RAG AI Agent for Legal: Intake, Discovery, Contracts, Research AI for legal research: real use cases, how accurate the tools are, the documented sanctions risk, and why attorney verifi... Read guide Agents & RAG AI Agent for SaaS: How to Embed Autonomous Agents in Your Product AI agents' disruptive impact on the SaaS industry in 2025: Gartner sees agentic AI at 30% of app-software revenue by 2035... Read guide Strategy, architecture & ops AI Architecture Patterns Agentic design patterns explained: reflection, tool use, planning, and multi-agent collaboration, with a framework to pic... Read guide Strategy, architecture & ops AI Architecture Patterns for SaaS: A Technical Guide Generative AI architecture for SaaS: layered design, multi-tenant isolation, LLM gateway, RAG, and security. Built by Res... Read guide Building AI AI Copilots for SaaS: Build vs Buy Guide AI copilot vs AI agent for SaaS: a copilot assists, an agent acts. How an in-app copilot works, the RAG and multi-tenant... Read guide
The loop is the hard part, not the demo

Want a team to build the agent you just scoped?