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

  1. Give the model a goal, a list of tools with clear descriptions, and instructions on when to stop.
  2. Think: the model decides what to do next and outputs a tool call (or a final answer).
  3. Act: your code runs the tool, with checks and permissions.
  4. Observe: the tool's result is added to the context.
  5. Loop until the model gives a final answer, or a step, time or cost limit is hit.
  6. Handle errors: return tool errors to the model so it can recover, and stop if it repeats itself.
  7. Log every step, and evaluate both the final answers and the paths taken.

Key points

Common mistakes

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.