Module 037 min

The loop: think, act, look

Every agent you will ever build is the same eight-step cycle, repeated until something says stop. Once you can see the cycle, you can debug any agent.

After this module
  • You can trace any agent step by step and point at the exact iteration where it went wrong.
  • You know the seven ways a loop is allowed to end — and why "the model decided it was done" is the weakest of them.

Here is the whole thing, in one sentence: build the best context you can, ask the model what to do next, do it, look at what happened, write it down, repeat. The model supplies judgement; the loop supplies persistence, safety and the ability to stop.

THE AGENT LOOP Goal done = checklist Build context history · docs · tools Decide next step or answer Permission allow · ask · deny Execute tool sandbox · timeout Observe result or error Update state messages · task list · budget · retries Stop done · blocked · out if act repeat if answer / limit
The harness owns the arrows. The model only ever fills in "what next". Everything that decides whether a step is allowed, how long it may take, and when the loop ends lives in the surrounding code.

3.1Walk one real task

Step through a booking task. Watch what changes at each step — and notice how much of the work is the harness, not the model.

Try it · loop walkthrough8 steps

"Book the cheapest direct flight to Chicago on Thursday that gets me there before 10am, then put it on my calendar."

The harness turns prose into a checklist: direct, Thursday, before 10:00, cheapest, calendar entry. That checklist is what "done" will be verified against.

3.2How a loop is allowed to end

Agents fail in two directions: they stop too early and declare victory, or they never stop. A serious harness does not rely on the model's opinion for either. It enforces limits and reports them.

Stop conditionWhy it existsWhat "good" looks like
Verified completionThe goal checklist is satisfied by evidence, not by the model's summary"Tests pass" is a tool result; "I believe the tests pass" is not
Max turnsLoops can oscillate between two wrong ideas foreverStop at N, return partial work plus a clear "I got stuck here"
BudgetTokens are money; a runaway loop can cost hundreds of dollars in minutesHard cap, warn at 80%, record spend per task
Wall-clock timeoutSome tools hang; some tasks are no longer worth finishingPer-tool and end-to-end deadlines, propagated to sub-tasks
Repeated failureThree identical failures mean the approach is wrongEscalate to a human or replan, never silently retry a fourth time
Human interruptA person may simply want it to stopCancellation is cooperative, fast, and leaves state consistent
Needs inputAmbiguity or a high-stakes actionAsk one precise question with the options you already know

3.3Three shapes the loop takes

ReAct — interleaved

Think, act, look, think. Maximum flexibility, minimum predictability. Best for open-ended research and exploration where the next step genuinely depends on the last result.

flexible · chatty · can wander

Plan-then-execute

One planning pass produces an ordered list; an executor works through it and only replans on failure. Cheaper, auditable, easier to approve — the plan itself becomes an artifact.

auditable · brittle to surprises

Thought → tool → thought

Several tool calls issued in one turn, all results returned together, then one decision. Fewer round-trips and lower latency, ideal when calls are independent (read five files at once).

fast · needs parallel-safe tools

Whichever shape you choose, the same three things must be true: the model's output is parsed into an action, the action is checked before it runs, and the result is written into state in a form the next iteration can use. If any of those is missing, you have a demo, not an agent.

The trap · the silent success

The most common production failure isn't a crash. It's an agent that finishes, reports success in confident prose, and did nothing — or did the wrong thing — because the loop trusted the model's summary instead of a verifiable result. Make the last step of every task a check, not a claim.

Check yourselfQuestion 1 of 1

An agent keeps retrying the same failing API call. Which fix belongs in the harness?

60-second recap

  • Eight steps, endlessly repeated: goal → context → decide → permission → execute → observe → state → stop.
  • The harness owns the arrows. Permissions, retries, budgets and termination are code, not instructions.
  • Every iteration resends a bigger context — that's where cost and latency live.
  • Stopping is a design decision. Verified completion beats "the model said it was done", every time.