DEV Community

Cover image for The Agentic Loop Explained: Step Through the Plan, Build, Judge and Repeat Loop Behind AI Coding Agents
DevOps Daily
DevOps Daily

Posted on

The Agentic Loop Explained: Step Through the Plan, Build, Judge and Repeat Loop Behind AI Coding Agents

Ask a chat model for a signup endpoint and you get one answer. A coding agent works differently: it can write the route, run the tests, read the failure, fix it, and keep going until some check says the job is done. The model inside can be the same. The difference is a loop wrapped around it, and that loop is what this post covers: the agentic loop explained one step at a time, with an animated simulator you can pause, step through and break on purpose.

Agentic Loop Simulator on DevOps Daily

Disclosure: I help build the simulator; it is free, runs in the browser, and needs no signup.

One call versus a loop

A single model call reads a prompt and writes a reply. An agentic loop turns that into a cycle: gather context, take one action with a tool, check the result against the goal, and go again until the goal is met or a budget runs out. The model does the thinking inside each step. The harness around it owns the loop, the tools and, most important, the rules for when to stop.

Anthropic's Building effective agents describes a close relative, the evaluator-optimizer workflow: "one LLM call generates a response while another provides evaluation and feedback in a loop." One possible design takes that further and splits the work into three roles.

Plan, build, judge

The Agentic Loop Simulator plays one task through those three roles: "add a working /signup endpoint (hash the password, return 201)". Be clear about what you are watching: it is a scripted, illustrative run. No model, editor or test suite runs in your browser. Every step is written in advance to show the pattern. Three cards light up in turn:

  • Plan (planner agent): decide the single next step toward the goal.
  • Build (builder agent plus tools): do it, here an edit to routes/signup.ts.
  • Judge (a separate judge agent): grade the result against the goal and the spec.

A gate under the cards shows the decision after each judge step: "goal not met ✗ loop back to plan" or "goal met ✓ stop". With the judge turned off, the builder grades itself and the gate says "self-check says done ✓ stop", even though the goal is not met. Press Play (it is slow on purpose, about 2.3 seconds a step at 1x), or step with Prev and Next or the arrow keys. Speed goes from 0.5x to 2x.

With the separate judge on, which is the default, the script takes three loops:

  1. The builder writes the handler. The judge step shows npm test failing and rejects the work: the password is stored in plain text.
  2. The builder adds bcrypt hashing. The tests now pass, and the judge still rejects it, because the endpoint returns 200 and the spec says 201. The caption puts it plainly: "Tests green is not the same as goal met."
  3. The builder returns 201 Created. The judge approves, and the loop stops.

Watch the two panels under the loop while it runs. Loop state tracks the iteration, a token counter with a meter, whether the goal is met, and whether the test suite is passing. The context window panel adds a chip for each plan and edit, so you can see the context get longer every loop.

Turn the judge off

Now flip the "Separate judge agent" toggle off and press Play again. This time the builder grades its own work. Loop one fails its own tests and goes round again. Loop two adds the hashing, the tests go green, and the builder decides it is done. The run stops with the card "Stopped by its own self-check", and one more step changes it to "Shipped a confident bug": the endpoint returns 200, not 201, and nothing in the loop compared the work with the spec.

That is the case for an independent check. A self-check can miss requirements the builder never tested for, and a reviewer with its own instructions and the spec in front of it may catch them.

Real agents also stop on checks that do not measure the goal. In a recorded run, an agent appended two tests below if __name__ == "__main__":, where unittest never collects them, and reported "All 8 tests pass (6 pre-existing + 2 new ones)". The same suite, run again outside the agent, printed Ran 6 tests.

The token counter ends at 91k with the judge and 56k without, but those are authored values in the script, not measurements of what verification costs.

How context can raise loop cost

The hint under the token meter says: "The whole context is re-sent every loop, so each loop costs more than the one before." The script's three judged loops add 27k, 31k and 33k. In a real harness, keeping history makes each request bigger, while compaction, selective context and caching pull the other way. Either way, a loop needs a stop condition the judge can check, plus a hard cap on turns in case it never gets there. Anthropic's guide calls these stopping conditions, "such as a maximum number of iterations". In Claude Code's headless mode the cap is --max-turns, and there is no limit by default.

A toy loop you can run

You can see the harness logic without any model at all. In this Python toy, the planner and builder are stubs that apply a fixed list of edits. The part worth reading is the loop, the two judges and the max_loops cap:

# a toy harness: the "agents" are stubs, the loop is real
SPEC = {"hashed": True, "status": 201}
FIXES = [{"route": True}, {"hashed": True}, {"status": 201}]

def build(code, step):
    return {**code, **FIXES[step]} if step < len(FIXES) else code

def tests_pass(code):          # what the builder checks
    return code.get("route") and code.get("hashed")

def meets_spec(code):          # what a separate judge checks
    return tests_pass(code) and code.get("status") == SPEC["status"]

def run(judge, max_loops=5):
    code = {"status": 200}
    for loop in range(1, max_loops + 1):
        code = build(code, loop - 1)
        if judge(code):
            return f"stopped after {loop} loops, status {code['status']}"
    return "hit max_loops"

print("self-check:     ", run(tests_pass))
print("separate judge: ", run(meets_spec))
Enter fullscreen mode Exit fullscreen mode
self-check:      stopped after 2 loops, status 200
separate judge:  stopped after 3 loops, status 201
Enter fullscreen mode Exit fullscreen mode

Same outcome as the simulator, with one honest caveat: the toy's judge wins because it checks one more requirement, the status code. Add that check to tests_pass and the self-check stops in the right place too. So the toy does not prove you need a second agent. It shows that a loop stops wherever its definition of done lets it stop, and that definition has to cover the spec, not only the tests you already have.

How it maps to a real coding agent

The bottom of the simulator maps each card to Claude Code. Plan is a planner subagent or a plan-mode turn that writes the next step. Build is the main agent using its Read, Edit and Bash tools. Judge is a separate reviewer subagent, ideally on a different model. Treat that last part as an option to test, not a rule: the Claude Code subagents docs show how to define a reviewer with its own system prompt, tools and model, plus a maxTurns cap. Whether a different model helps depends on your task.

For the wider picture, including the fix-until-green, experiment and overnight triage loops and the executor and advisor split for cost, read the companion post, Stop Prompting, Start Looping: Agentic Loops Explained. Then run the simulator twice, once with the judge and once without, and compare what each run counted as done. That definition is the part of the loop you design.

Does your agent setup use a separate judge step, or does one agent check its own work? Tell us in the comments.

Top comments (0)