BoilerPrompt

Agent loop

The agent loop is the repeating cycle an AI agent runs: decide what to do next, take an action such as reading a file or running a command, observe the result, and repeat. It ends when the task is complete, a limit is reached, or the agent asks for input.

Everything an agent does happens inside this loop. A ten-minute Claude Code session is dozens of iterations of exactly the same four steps, with the observation from one becoming input to the next.

Understanding it explains most agent behaviour. Agents drift when nothing in the loop corrects them — no test run, no type check, no failing output to react to. They loop forever when the observation never changes. And they get expensive when each iteration reads more context than it needs, because every iteration pays for everything it looks at.

What this means in practice

This is why a verification step at the end of a prompt matters so much. "Run the tests" gives the loop a signal to terminate on and something concrete to react to. Without it, the agent's only stopping condition is its own judgement that the work looks finished.

Prompts for the tools this applies to

Related terms

  • Agentic coding

    Agentic coding is using an AI tool that plans and executes a whole task in your codebase rather than completing one line at a time.

  • Coding agent

    A coding agent is an AI system that carries out programming tasks end to end: reading a codebase, editing multiple files, running commands, and reacting to the output.

  • Tool calling

    Tool calling is the mechanism that lets a model do things beyond producing text.

  • Agent mode

    Agent mode is the setting in an AI coding tool where the model can act rather than only answer: creating and editing files, running terminal commands, and iterating on the result.

All glossary terms