The Agent Loop: How AI Agents Actually Work

If you understand one thing about how AI agents work, make it this: the agent loop. It is the single mechanism that turns a language model from a thing that answers questions into a thing that gets work done. Once you see the loop, agents stop being mysterious.

The loop in one paragraph #

An agent runs in a cycle. The model looks at the current situation and the tools available to it. It either produces a final answer, in which case the loop ends, or it decides to use a tool. If it calls a tool, your program runs that tool for real, captures the result, and hands the result back to the model. The model looks at the new situation, including what the tool just returned, and decides again. This repeats until the model has nothing left to do and gives its final answer.

That is it. Look at the world, decide on an action, take the action, look again. Observe, think, act, repeat.

A turn, step by step #

Walk through a single pass of the loop for the request “how many open bugs are assigned to me?”

  1. The model receives the context. That includes your question and a list of tools it is allowed to use, for example a tool called search_issues.
  2. The model decides. It cannot answer from memory, so instead of replying with text it emits a structured request: call search_issues with assignee = "me" and status = "open".
  3. Your program executes the tool. This is the crucial part: the model does not run anything itself. It only asks. Your code catches the request, actually queries the issue tracker, and gets back, say, seven issues.
  4. The result goes back to the model. Your program feeds the seven results into the context as the answer to the tool call.
  5. The model decides again. Now it has what it needs. It produces a final reply: “You have seven open bugs,” and the loop ends.

Simple requests finish in one or two passes. Hard ones might loop dozens of times, each pass adding one more action and one more observation.

Who runs what #

The most common misconception is that the AI “does” everything. It does not. The loop has a strict division of labor:

  • The model proposes. It decides what should happen next: which tool, with which inputs, or whether it is finished. It produces only text and structured requests. It never touches your files, your network, or your database directly.
  • Your program disposes. The code around the model (often called the harness) is what actually executes tool calls, enforces permissions, and feeds results back. It is the muscle to the model’s judgment.

This split is not an accident, it is the safety model. Because your code sits between the model’s decision and any real action, you get to inspect, log, gate, or refuse anything before it happens. The model can ask to delete a file; your code decides whether to let it.

How the loop knows to stop #

A loop that never ends is a bug, so termination matters. An agent stops for one of a few reasons:

  • The model signals it is done. When it produces a final answer instead of another tool call, the loop exits naturally. This is the normal case.
  • A limit is hit. Sensible agents cap the number of passes. If the loop runs, say, twenty-five times without finishing, the harness stops it rather than letting it spin forever (and run up a bill).
  • It gets stuck waiting. Some steps need something from outside, like a human approving an action or a slow tool returning. The loop pauses there until the input arrives, then continues.

Why it has to be a loop #

You could ask why the model does not just plan everything up front and execute in one go. The answer is that it cannot know the future. It does not know how many bugs are assigned to you until it looks. It does not know whether the test passes until it runs it. It does not know what the search returns until it searches.

The loop exists precisely because each action produces information the model did not have before, and the next decision depends on it. Acting one step at a time, and reacting to what each step reveals, is what lets an agent handle messy, open-ended tasks instead of only the ones that can be scripted in advance. That reactivity is the entire point.

Every agent you will ever use is some version of this loop. A coding agent runs it with tools for reading and editing files; a research agent runs it with tools for searching and reading. The tools change. The loop does not. And the mechanism that lets the model call those tools in the first place is worth understanding on its own: tool use.