Papa Programmer
Lesson 02 of 20

The agent loop

8 min read

Optional

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.

  1. Look. The program gives the model the goal, the history so far and the result of the last action.
  2. Decide. The model picks the next action. It can call a tool or declare that the goal is met.
  3. Act. The program runs the chosen tool. The model never runs anything itself.
  4. Read. The result goes back into the history, and the cycle starts again.
  1. LookThe program gives the model the goal, the history and the last result.
  2. DecideThe model picks the next action, or says the goal is met.
  3. ActThe program runs the chosen tool.
  4. ReadThe result is added to the history.

Then the cycle starts again at Look

The agent loop: the program runs every move, and the model only decides

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.

python
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.

Look, decide, act, readThe four moves repeat until the goal is met or a limit stops the agent.
The program owns the loopThe model only decides. Your code runs the tools, so your code can limit them.
Always set an exitGive every loop a step limit and a failure exit, not only a success condition.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools