Appearance
Text size
Typeface
Line spacing
1
Chapter 1 · 4 min

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

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?
Back to the top until the work is done

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.

$ 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.
Check yourself

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

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.

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.

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.
Check yourself

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

Read further