Agents and tool use
An agent is an LLM running in a loop. It decides an action (usually a tool call), sees the result, and decides again, until the task is done or a limit is reached.
Why it matters
Many tasks need several steps whose order isn't known in advance: look something up, calculate, check, try again. A single prompt can't do that. Agents let a model plan and adapt as results come in. They are powerful, but they fail in new ways — loops, wrong tools, compounding errors — so they need limits, logging and evaluation.
The idea
An agent is a model in a loop. It chooses an action, such as calling a search tool or a calculator, sees the result, and chooses again until the task is done.
A new assistant with a phone and a laptop, working through a task step by step.
A human assistant notices when they are going in circles. Agents can loop, call the wrong tool, or trust a bad result, so they need step limits, logging and evaluation.
How it works
- Give the model a goal, a list of tools with clear descriptions, and instructions on when to stop.
- Think: the model decides what to do next and outputs a tool call (or a final answer).
- Act: your code runs the tool, with checks and permissions.
- Observe: the tool's result is added to the context.
- Loop until the model gives a final answer, or a step, time or cost limit is hit.
- Handle errors: return tool errors to the model so it can recover, and stop if it repeats itself.
- Log every step, and evaluate both the final answers and the paths taken.
Key points
- Agent = model + tools + loop + stopping rules.
- Start with the simplest workflow that works; use open-ended agents only when the steps can't be fixed in advance.
- Step limits, timeouts and budgets are essential.
- Errors compound over steps: a 95%-reliable step repeated 10 times succeeds about 60% of the time.
- Tool descriptions and good error messages strongly affect behaviour.
- Risky actions, such as sending an email or spending money, need a person to confirm them first.
Common mistakes
- No step limit, so a confused agent loops forever.
- Hiding tool errors instead of returning them, so the agent can't recover.
- Too many overlapping tools, so it can't choose.
- Using an agent where a fixed pipeline would be simpler and more reliable.
Not true: Hiding tool errors from the model helps an agent stay on track.
In fact: An agent can only recover from an error it can see. Return the error message as the tool's result, so that it can try something else.
Not true: An agent needs no step limit: it stops by itself when the task is done.
In fact: A confused agent can loop for ever, spending money on every step. Step limits, timeouts and budgets are essential.
Part of Unit 5: AI engineering in the free ML Foundations course.