# The loop inside every agent

An agent is a model using tools in a loop. Everything else, from memory to multi-agent systems, is built on those few steps.

## One sentence is enough

Strip away the marketing and an agent is simple. Simon Willison's definition has become the useful one: an LLM agent runs tools in a loop to achieve a goal.

Each word in that sentence does work. **Tools** are how the model touches anything outside its own text. **Loop** means it sees the result of what it did and decides again. **Goal** means there is a point at which it stops.

A chatbot answers from what it already has. An agent can be wrong, read the error, and try something else. That one difference is why Claude Code can fix a bug it has never seen.

## The steps, one by one

```flow loop
Take in the goal and the context
Decide: answer, or call a tool
The harness runs the tool
The result joins the context
Check: is the goal reached?
```

The model never runs anything itself. It writes a request such as "read this file" or "run this command", and the program around it does the actual work and hands back the result. The next decision starts from that result, not from what the model hoped would happen.

This is why reading the output matters more than writing the first attempt. Anthropic calls the tool result ground truth: on every step, the world gets to correct the model.

```terminal
$ fix the failing date test
* Reads tests/date.test.ts
* Runs the test: expected "2 Nov", got "1 Nov"
* Reads src/format.ts, sees a timezone conversion
* Edits src/format.ts
* Runs the test again: passes
# Five steps, and each one was chosen after seeing the last.
```

**Question:** What makes step two of the loop different from a normal chat reply?

- The model uses a larger context
- The model can choose to act and then see what happened
- The model is told the answer in advance
- The model runs faster

**Answer:** The model can choose to act and then see what happened. Acting and then reading the result is the whole difference. Without the result coming back, there is no loop, only a single guess.


## The harness owns the loop

There are two parts here, and it pays to keep them apart. The model decides. The **harness** does everything else: it runs the tools, asks you for permission, keeps the log, shortens the conversation when it grows too long, and recovers after a crash.

Claude Code is a harness. So when you set a permission mode or add a hook, you are not persuading the model. You are changing the program that sits between the model and your machine.

> **The brain can crash and nothing is lost**
Anthropic's Managed Agents split the model and harness (the brain) from the sandboxes (the hands), and keep the session log outside both. A crashed harness simply starts again and replays the log.


## Why stopping is the hard part

A loop with no exit runs until the money is gone. Agents stop for one of three reasons: the goal is met, a limit is reached, or a person steps in.

"The goal is met" is the difficult one, because the model is the one saying so, and models are generous about their own work. A good stop condition is something a program can check: the tests pass, the score is above 90, the page loads. A hard cap on turns or cost catches everything else.

> **Say how you will know it worked**
End your request with the check: "fix the date test, then run the whole suite and show me it passes." You have just given the loop an exit it can actually verify.


## What this explains

Most of what follows in this course is this loop under pressure.

- Each step adds to the context, so long runs fill up and get worse. That is the next problem to solve.
- Tools decide what the agent can see and do, so a badly described tool costs more than a bad prompt.
- Several agents are several loops, and they only help when those loops do not need each other's context.
- Loop engineering wraps a second loop around this one, so you stop being the person who types each turn.

> **See the loop yourself**
Ask Claude Code to do something small that can fail, such as making one test pass. Watch the tool calls go by and notice where the plan changes because of a result. That moment is the agent working.


**Question:** Who actually runs a shell command when an agent decides to use one?

- The model, inside its own process
- The harness, which then returns the output to the model
- Anthropic's servers
- Nobody, the model predicts the output

**Answer:** The harness, which then returns the output to the model. The model only writes the request. The harness runs it on your machine under your permission rules and hands back the result.
