How to build an AI agent?
Building an AI agent takes three parts and about 50 lines of code: a model, a set of tools it can call, and a loop that runs the tool and feeds the result back until the task is done. Start with the simplest thing that works — Anthropic recommends agents only when you can't hardcode the path.
Why — the first-principles explanation
The mechanics are far simpler than the hype suggests. You describe some functions to the model in a structured format — name, description, parameters. The model replies with either a final answer or a request to call one of those functions. Your code, not the model, executes it. You append the result to the conversation and call the model again. Repeat until it stops asking for tools. That `while` loop is the agent. Every framework on the market — the OpenAI Agents SDK, LangGraph, CrewAI — is a convenience wrapper around exactly this. Write it once by hand before you pick one, or you'll be debugging abstractions instead of behavior.
The hard part isn't the loop, it's the tool descriptions. The model decides what to call based entirely on the text you wrote describing each tool. A tool named `get_data` with the description "gets data" will be called at random. `search_orders(customer_email)` described as "Returns the last 10 orders for a customer. Use only when the user asks about order history; returns empty if the email is unknown" gets called correctly. You are not programming the agent — you are writing documentation for a colleague who reads only the docs. Most agent failures are documentation failures.
Then there's the part that separates a demo from a system: the loop must be able to stop. An unbounded agent burns tokens forever, retries a broken tool 40 times, and confidently builds twenty steps on a wrong assumption from step two — Anthropic's warning about "compounding errors" is not theoretical. So you cap iterations, set a token budget, add a timeout, and make failures loud rather than silent. And every tool that changes the real world — sending, deleting, paying, deploying — gets a human approval gate or gets scoped read-only. OpenAI's guidance points at the same needs: guardrails and "resumable approval flows."
Finally, the decision most people skip. Anthropic is explicit that agents suit "open-ended problems where it's difficult or impossible to predict the required number of steps," and that they bring "higher costs, and the potential for compounding errors." If you already know the steps, write the steps — a prompt chain or a router is cheaper, faster, and testable. Build an agent when the path genuinely can't be known in advance.
An example that makes it click
Building an agent is like teaching someone to cook in your kitchen over the phone. You can't move their hands. All you can do is (1) tell them what's in the kitchen, (2) let them ask for one thing at a time, and (3) tell them what happened after each try.
So you say: "There's a pot, a knife, a stove with a dial from 1 to 9, and a fridge." That's your tool list. They say "turn the dial to 7." You do it and report back: "done, it's heating." That's the loop. Now here's the whole lesson in one detail: if you'd described the stove as just "a stove," they'd have said "turn on the stove" and you'd have had no idea what to do. The quality of your description — dial, 1 to 9 — is what makes them competent. And one more thing: you set a rule before you started. "If you ask me to do something more than ten times, we stop and talk." Without that, you'd both be on the phone at 3 a.m. still adjusting the dial.
How to do it
- Write down the job in one sentence and ask whether the steps are predictable. If you can list them in order, build a prompt chain or a router instead — Anthropic recommends agents only for open-ended problems where you can't hardcode the path.
- Pick a model with reliable tool calling. Start with the strongest one available; prove the behavior works before you optimize for cost by dropping to a smaller model.
- Write the instructions (system prompt): who the agent is, what it's for, what it must never do, and what to do when it's stuck or uncertain. Say 'ask the human' explicitly — models won't do it by default.
- Define 3-5 tools, no more. For each, write a description precise enough that a new hire could use it correctly with no other context: what it returns, when to use it, when NOT to use it, and what happens on failure.
- Write the loop by hand first: call model → if it requests a tool, run it → append the result → call model again → stop when it returns a final answer. Roughly 50 lines. Use a framework only after this works.
- Add hard stops: a maximum iteration count, a token budget, and a wall-clock timeout. An agent without a ceiling is a bill without a ceiling.
- Scope permissions to the minimum. Make every tool read-only unless it truly must write, and put a human approval gate in front of anything irreversible — sending, deleting, paying, deploying.
- Test on 20 real cases, not 2. Include the messy ones: missing data, ambiguous requests, and tools that fail. Watch the full trace of every tool call, since a good final answer can hide a bad path.
- Log every step in production — prompt, tool call, result, decision. When an agent misbehaves, the trace is the only way to find out which step went wrong.
Key facts
- Anthropic's engineering guidance (December 2024) recommends agents for "open-ended problems where it's difficult or impossible to predict the required number of steps, and where you can't hardcode a fixed path," and warns of "higher costs, and the potential for compounding errors."
- Anthropic lists five simpler patterns to try before a full agent: prompt chaining, routing, parallelization, orchestrator-workers, and evaluator-optimizer.
- OpenAI's Agents guide defines agents as applications that "plan, call tools, collaborate across specialists, and keep enough state to complete multi-step work," and points to "guardrails, or resumable approval flows" as reasons to use an SDK.
- The model never executes a tool: it emits a structured tool-call request, the surrounding program runs it, and the output is appended to the conversation before the next model call.
- Gartner (June 25, 2025) predicts over 40% of agentic AI projects will be canceled by end of 2027, citing escalating costs, unclear business value, and inadequate risk controls.
▶ The 60-second explainer (script)
Building an AI agent is three parts and about fifty lines of code. A model, a set of tools, and a loop. Here's the loop. You describe some functions to the model — name, description, parameters. The model replies with either an answer or a request to call one of them. Your code runs it, you paste the result back into the conversation, and you call the model again. Repeat until it stops asking. That while-loop is the agent. Every framework out there is a wrapper around exactly that, so write it by hand once before you pick one. Now the part nobody tells you: the hard work isn't the loop, it's the tool descriptions. The model decides what to call based only on the text you wrote. A tool called get_data described as 'gets data' will get called at random. The same tool described as 'returns the last ten orders for a customer, use only for order history, returns empty if the email is unknown' gets called correctly. You're not programming the agent — you're writing documentation for a colleague who only reads the docs. And put in the brakes. Cap the iterations, cap the tokens, set a timeout, and make anything irreversible — sending, deleting, paying — require a human to click yes. One last thing. Before you build any of this, ask whether you can just write the steps down. If you can, do that instead. Anthropic's own advice is to use agents only when the path genuinely can't be predicted.
What authoritative sources say
People also ask
Do I need a framework to build an AI agent?
No. The core loop is about 50 lines against any model's tool-calling API. Frameworks help with retries, tracing, and handoffs once you're past a prototype — but they hide the loop, which makes early debugging harder.
How many tools should an agent have?
Start with 3-5. Model accuracy in picking the right tool drops as the list grows and the descriptions start overlapping. If you need twenty, that's a sign you want several specialized agents with a router.
Why does my agent keep calling the wrong tool?
Almost always the descriptions. The model chooses using only the text you wrote. Add when-to-use and when-NOT-to-use lines, state what the tool returns, and make sure no two tools sound like they do the same thing.
How do I stop an agent from looping forever?
Hard caps, not prompts. Set a maximum iteration count, a token budget, and a timeout in your code. Asking the model nicely in the system prompt to be efficient is not a control.
How much does it cost to run an agent?
More than you'd guess, because each loop re-sends the whole growing conversation to the model. Cost scales roughly with the square of the steps, which is why iteration caps are a budget control, not a nicety.
The same question, asked other ways
- How to create an AI agent?1,600/mo
- How to build AI agents?1,000/mo
- How to create AI agents?720/mo