ML//agent//agent loop

The agent loop is the control cycle that drives an AI agent, a loop that repeatedly assembles context, asks a model for the next action, executes or rejects that action, records the observation and decides whether another iteration is warranted.


The agent loop is the control cycle that drives an AI agent, a loop that repeatedly assembles context, asks a model for the next action, executes or rejects that action, records the observation and decides whether another iteration is warranted.

Its source code can be tiny (the agent harness implements it and owns the durable state rebuilt between model calls). Its policy surface is not. Timeouts, retry rules, budgets, validation, compaction, permissions and termination conditions all change which trajectories remain possible; iteration limits and stopping rules are where agent autonomy becomes a concrete execution budget. A loop that continues after every error creates a different agent from one that returns control immediately, even with the same prompt and model.

Stripped to its skeleton it is an ordinary program: a while loop that sends the context to the model over an API, receives either a final answer or a tool request written as JSON (function calling), validates and runs the request, appends the result to the context and goes round again. The model executes nothing; the loop does. That is why an agent is a Python loop is a fair first description and a poor production design, since everything that makes it safe lives in what the skeleton leaves out:

Limits on iterations, tokens, money and wall time, so a confused model cannot loop forever, and a clear rule for handing control back to a person.

Care with actions that change the world. A retried read is harmless; a retried payment or valve command is not, so operations with side effects are made idempotent (repeating them changes nothing) or are confirmed before they run.

State kept by the harness, because the model's API remembers nothing between calls (agent memory); and since most of the loop's time is spent waiting on the network and on tools, one process can run many such loops concurrently.

The loop is the heartbeat. The surrounding rules decide what kind of organism gets a pulse.