The Agent Loop for Code — Read, Edit, Run, Observe

Every coding agent, under the hood, runs the same loop: understand the situation, take an action, observe what happened, and decide the next action — repeating until the task is done. This read-edit-run-observe loop is the engine of a coding agent, and it works for code precisely because each action produces concrete, verifiable feedback. This post builds that loop and shows why closing it against real results is what separates an agent from a code generator.

Post 1 said agency means acting and closing the loop. This post is the loop itself — the core execution model of a coding agent. Understanding it clarifies everything: why grounding (post 3) and tools (post 4) matter, why verification (post 6) is central, and why coding agents can self-correct. It’s the beating heart of the series.

The loop: observe, act, observe again

A coding agent operates as a cycle, the same shape as any agent (perceive → decide → act → perceive) specialized for software:

  1. Understand / observe the state — read the relevant code, the task, the current situation (what files exist, what the error is, what the tests say). The agent builds a picture of where things stand.
  2. Decide the next action — reason about what to do next given the goal and the current state: which file to read, what edit to make, what command to run.
  3. Act — take a concrete action through a tool (post 4): read a file, edit code, run a command, execute tests, search the codebase.
  4. Observe the result — see what the action produced: the file’s contents, the edit’s effect, the command’s output, the test results, the error message.
  5. Repeat — feed that observation back into understanding, and loop — deciding and taking the next action — until the task is complete (tests pass, the feature works) or the agent determines it’s done or stuck.

The critical element is step 4→5: the agent observes the concrete result of each action and uses it to decide the next one. This is closing the loop, and it’s what makes the agent agentic rather than a one-shot generator. A code generator produces code and stops; an agent makes a change, runs it, sees the error, and fixes it — iterating toward a working result. The loop is why a coding agent can start with a wrong first attempt and converge to a correct solution, which is how real engineering actually works (nobody writes correct code first try; they iterate against feedback).

Why feedback makes the loop work for code

The loop is powerful for coding specifically because code gives concrete feedback at every step (post 1’s verifiability), and this is worth seeing in detail:

The deep point: the agent loop plus code’s concrete feedback equals reliable self-correction. The agent isn’t expected to be right first try; it’s expected to iterate against real results toward being right, which is both more achievable and more robust. This is why the loop, not the model’s raw code-generation ability, is the essence of a coding agent — the model provides the reasoning at each step, but the loop against feedback is what gets to a working solution.

What the loop needs, and where it struggles

The loop frames the rest of the series by revealing what it depends on and where it breaks:

The synthesis: the coding agent is, at its core, a read-edit-run-observe loop that closes against code’s concrete feedback — observe the state, decide an action, act through a tool, observe the result, repeat — converging to a working solution by iterating the way a human engineer debugs. It works because code gives objective feedback at every step (verifiability from post 1), it needs good grounding (post 3), reliable tools (post 4), and a clear success signal (post 6), and it struggles exactly where that feedback is weak. Hold the loop as the engine of everything that follows: the model reasons, but the loop-against-feedback is what makes a coding agent reliably get to working code.

The throughline: every coding agent runs a read-edit-run-observe loop — observe the state (read code, the task, the error), decide the next action, act through a tool (read/edit/run/test/search), observe the concrete result, and repeat until done. The critical element is observing each action’s result and using it to decide the next — closing the loop, which makes it an agent (iterates to a working solution) rather than a one-shot generator (produces code and stops). It works for code specifically because code gives concrete feedback at every step: every action has an observable result, errors are rich signals not dead ends (read the stack trace, fix the cause — the loop’s superpower), enabling convergence through iteration (attempt → observe failure → refine, like human debugging — turning “write correct code” into the tractable “generate, test, fix, repeat”). The loop needs good grounding (post 3, to observe well), reliable tools (post 4, to act), and a clear success signal/verification (post 6, to know it’s done) — and it struggles or thrashes exactly where feedback is weak (no tests, fuzzy success), so the loop is only as reliable as the feedback that drives it.

Key takeaways

Further reading

Sources & References