What is the agent loop?
A single model call answers a question. An agent does a job. The difference is a loop: the model asks for a tool, the result comes back, and it decides what to do next from what it just learned. Then again, and again.
Nobody writes the steps in advance. A script follows a fixed list. An agent looks at the result of each step and chooses the next one, which is why it can cope with surprises like a fix that only half works.
The loop ends when the model replies with plain text and no tool request. That is its way of saying the job is finished. Sensible systems also set a maximum number of rounds, in case it never says so.
Why does the agent loop matter?
A single model call answers once. Real work takes many steps, and each step depends on what the last one found. The agent loop is the small piece of code that lets a model keep going: look, act, look again. It is the defining mechanism of an agent, and it is only a few lines long.
How does the agent loop work?
- The harness sends the model the task and the list of tools.
- If the model's reply asks for a tool, the harness runs it.
- The result is added to the conversation.
- The model is called again with everything so far.
- When a reply asks for no tool, the loop ends and that reply is the answer.
When should you use the agent loop?
Use it when
- The steps cannot be known in advance
- Each step depends on what the previous one found
- The work involves several tools used in combination
Think twice when
- The steps are always the same. A fixed sequence is cheaper and more predictable
- One model call is enough
Where does the agent loop sit in a real system?
The loop is the core of the harness. Everything else in a harness exists to feed it or restrain it: the prompt builder, the tools, the permission checks, the limits.
What goes wrong with the agent loop?
- Running a loop with no limit on rounds, time or money.
- Hiding errors from the model. An error is a result it can react to.
- Using a loop where a plain sequence of steps would do.
What does the agent loop look like in practice?
Told to fix a failing test, an agent lists the files, reads two, edits one, runs the tests, sees one still failing, edits another and runs them again. Eleven rounds, each chosen from the result of the last. Nobody scripted the order.