How AI agents work
A model in a loop with tools: planning, tool calls, context, stop conditions, and when not to use one.
Includes an interactive demo: Agent loop stepper
What an agent is
An AI agent is a model running in a loop with tools. Each turn it looks at the goal and everything that has happened so far, then either calls a tool or decides it is finished. That’s it: no magic, just a loop with good instructions and good tools.
Step through a run
Here is one run of a lead-handling agent. Watch the phase move around the loop and the context grow with every step.
- Read the goal and plan: The model receives the goal, the available tools and their descriptions. It decides the first step.
- Call a tool: It asks to run a tool with arguments. The application, not the model, actually executes it.
- Read the result: The tool result goes back into the context. Nothing found, so this is a new contact.
- Call the next tool: It creates the contact, then asks a calendar tool for open slots.
- Handle a failure: The calendar API times out. The agent reads the error and retries once, as its instructions say.
- Check before acting: Sending an email is an outside action, so a guardrail asks: is this allowed without a human? Here it is.
- Stop and report: The goal is met, so the loop stops and returns a summary. A step limit would also stop it.
Want to drive one yourself, with failures and approvals? Try Agent Lab.
Tools are the whole game
A model can only do what its tools allow. Each tool is a name, a description the model reads, and a JSON schema for its inputs. Clear names and descriptions matter more than clever prompts.
{
"name": "calendar.find_slots",
"description": "Find open 30-60 minute meeting slots on the sales calendar. Use before offering times to a lead.",
"input_schema": {
"type": "object",
"properties": {
"duration_min": { "type": "integer", "enum": [30, 45, 60] },
"within_days": { "type": "integer", "minimum": 1, "maximum": 14 }
},
"required": ["duration_min"]
}
}Context and memory
Every turn, the whole history goes back to the model, so context grows with each step. Long runs need managing:
- Keep tool results shortReturn the 3 fields the agent needs, not the whole API response.
- Summarise old turnsReplace early steps with a short summary once they’re no longer needed.
- Store facts outsidePut durable facts in a database or notes file the agent can read back.
- Set a budgetA hard limit on steps and tokens stops a confused agent from looping forever.
Workflow or agent?
| Aspect | Workflow | Agent |
|---|---|---|
| Path | Fixed, drawn in advance | Decided by the model each turn |
| Best for | Repeatable processes | Open-ended tasks with many possible paths |
| Cost per run | Low and predictable | Higher, varies with steps |
| Debugging | Easy: same path every time | Harder: needs traces and evals |
| My default | ✓ Most business processes | When the path genuinely varies |