Learn › Agent Harness, the big picture › Stop 4

Stop 4 of 9 · Simulation

Simulation: the agent loop

One request, one move at a time: what our code sends to the model, what comes back, the tool we run, and round again until the goal or the limit.

Nothing here calls a real model. The replies are scripted so that every run is the same. The shapes of the request and the response are the real ones.

How to read this page.One turn is one request from our code to the model and one response back. The page lays the turns out one under the other, so you can count them and read what crossed in each. Blue rows go to the model. Lavender rows come back from it. Yellow rows are our own code running a tool.

What the person seesReplies are scripted, so pick one of these messages

What happens behind it

A personasks1 · Send the requestall messages + the tool list2 · The model repliesan answer, or "run this tool"3 · Run the toolour code runs it, not the model4 · Add the resultto the messagesGoal reachedno tool asked for:that is the answerLimit reachedstop, andhand overit asked for a toolgo againthe agent loopone turn = one model call

The line of code that is running


This is the loop from agent.py in stop 3, with the same line numbers.

Things to try

  1. Send the first message, then press "Next turn" three times. Count them: three requests and three responses for one sentence from the person. Nobody told the model the order of the steps.
  2. Compare request 1 with request 3. Request 3 carries five messages, because the model remembers nothing between turns and our code sends the whole conversation again. More turns means more text sent each time, which is what you pay for.
  3. Send "What does my policy P-1001 cover?". Same loop, same tools, two turns. No new code was written for it.
  4. Send "Where is my claim C-777?". The claims system is busy. The model would retry for ever. The only thing that stops it is our limit.
  5. Set the limit to 2 and send the first message again. The loop stops before the claim is created. A limit protects you, and a limit that is too tight breaks real work. Choosing it is a design decision.
  6. Untick "Show the exact JSON" when you want to explain the idea first and the detail later.

What the simulation shows

What you sawWhy it matters
The model never runs a tool. It only asks.Our code runs every tool. So our code is the place to put every check.
A reply with no tool request is the answer.That is how the loop knows the goal is reached. There is no separate "done" signal.
The model has no memory. We send everything, every turn.What the model knows is exactly what we choose to send. That is a control, and a cost.
The loop stops for one of two reasons.The model says it is done, or our code says "enough". Only the second one is under our control.

Next: the loop above trusts the model completely. Stop 5 shows what happens when someone takes advantage of that.