The agent loop
Four lines of logic, and three places where everything goes wrong
A test-fixing agent is set loose on a repository. Turn 1: it reads the test file. Turn 2: it modifies the module. Turn 3: it reruns the suite, and one test still fails. Turn 4: it reverts its change. Turn 5: it reapplies the exact same one. Turn 12: the bill for this single task has reached $2.80, the history weighs 90,000 tokens, and the process is still running. At no point did the code crash: the loop does exactly what it was told to do, which is to keep going as long as the model requests a tool.
The body of an agent loop fits in about fifteen lines and there is nothing subtle about it. The difficulty lies elsewhere, in three decisions you make by default because you never spelled them out: under what condition do you stop, how many turns do you allow before giving up, and what do you do with the history once it becomes the main expense. Getting any one of the three wrong gives you a loop that works in a demo and becomes impossible to operate on the first difficult task.
The Anatomy of a Turn
An agent turn has four moments. Two belong to you, two belong to the model.
- Reasoning: the model receives the history and decides. It produces either final text or one or more tool call requests.
- Execution: your code runs those calls. The model touches nothing itself — it only emits intentions in JSON format.
- Observation: you feed the result of each call back into the history, associated with the call's identifier.
- Iteration: you call the model again with the augmented history.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion