Agent loop
The agent loop is the core pattern behind an AI agent. It’s the cycle that lets a model take actions in the world rather than just answer once. The cycle is:
- Observe. The model receives context — the user’s goal, plus the results of anything it has done so far.
- Reason/decide. It figures out the next step toward the goal.
- Act. It calls a tool — run code, search the web, edit a file, hit an API.
- Get results. The tool’s output is fed back into the model’s context.
- Repeat. The model looks at the new information and decides the next action, until it judges the task done, or it hits a limit such as a max-steps or budget cap.
The key difference from a plain chatbot is that the loop lets the model iterate: try something, see what happened, adjust. That is what makes things like coding agents work — write code, run tests, see failures, fix, run again. A single request-response turn cannot do this. The loop is what turns a model into something that can pursue a goal rather than merely respond to one.
In practice, three concerns dominate the difficulty of running an agent loop well:
- Context management. The loop accumulates a lot of tokens as tool results pile up turn over turn, which is the subject of context engineering.
- Knowing when to stop. Judging that the task is actually done, rather than looping past completion or giving up early.
- Error handling. Recovering gracefully from failed tool calls and dead ends instead of compounding them into further bad actions.
The infrastructure that wraps the loop — providing tools, managing the context window, enforcing guardrails — is the harness. Running many such loops unattended, on a recurring cadence rather than a single session, is the concern of loop engineering.