The agent loop
8 min read
One cycle, four moves
The Agent loopThe cycle an agent repeats: look at the goal and history, decide the next action, run it and read the result. The program runs the loop. The model only makes the decision. has four moves, and the agent repeats them in order.
- Look. The program gives the model the goal, the history so far and the result of the last action.
- Decide. The model picks the next action. It can call a tool or declare that the goal is met.
- Act. The program runs the chosen tool. The model never runs anything itself.
- Read. The result goes back into the history, and the cycle starts again.
- LookThe program gives the model the goal, the history and the last result.
- DecideThe model picks the next action, or says the goal is met.
- ActThe program runs the chosen tool.
- ReadThe result is added to the history.
Then the cycle starts again at Look
The program owns the loop. The model owns only the decision in step 2. This split matters because the program is where you place limits, logs and safety checks.
The loop in plain Python
The sketch below has no library and no real model. The decide function stands in for the model so that you can run the loop and see each move.
def decide(goal, history):
if not history:
return {"tool": "add", "args": [2, 3]}
if len(history) == 1:
return {"tool": "add", "args": [history[-1], 10]}
return {"done": history[-1]}
tools = {"add": lambda a, b: a + b}
goal = "Add 2 and 3, then add 10"
history = []
for step in range(1, 6):
action = decide(goal, history)
if "done" in action:
print(f"Step {step}: goal met, answer {action['done']}")
break
result = tools[action["tool"]](*action["args"])
history.append(result)
print(f"Step {step}: {action['tool']}{tuple(action['args'])} gave {result}")Run it and you see three steps. The first adds 2 and 3. The second adds 10 to that result. The third finds nothing left to do and stops. A real agent has the same shape. Only decide changes, because it becomes a call to a language model.
Three ways the loop ends
A loop that cannot end is a bug that costs money. Build in all three exits.
- Success. The model says the goal is met, and the program checks the claim when it can. A model can say it is done when it is not.
- A step limit. The sketch above runs at most five steps. Pick a number that fits the task, and stop at it even if the agent still wants to continue.
- A failure exit. If the same action fails three times, stop and report what happened. Do not let the agent retry forever.
When the limit is hit, return an honest message such as "I could not finish. Here is what I did and where I stopped". A clear failure is more useful than a long silent loop.
Why the history matters
Each pass through the loop adds to the history, and the model reads all of it again on the next pass. The history is how the agent knows what it already tried. It is also why a long task slows down and costs more as it runs. The next module covers how to keep that history small.
Who runs the tool?
The model writes {"tool": "add", "args": [2, 3]}.
The model only decides. Your program owns the loop and runs every tool, which is where you can add limits.
What should the loop do?
An agent has called the same search tool four times and got the same empty result each time.
A loop needs a failure exit. An honest stop is more useful than a long loop that burns money.
Which exit is missing?
A loop ends only when the model says the goal is met. Once, the model never says it.
A loop that cannot end is a bug. Set a maximum number of steps that applies even when the agent wants to continue.
Your reading position is saved
0 of 20 complete
Lesson complete
The agent loop
Up next