Explainer

Agentic Loops: How Coding Agents Actually Work

An agentic loop is the repeating cycle at the heart of every autonomous coding agent: gather context, decide the next action, act via a tool, observe the result, repeat, until the goal is met or a stop condition fires. Here is the mechanic, its real variants, and how to keep it from running away.

By Kylian Migot · Updated July 2026 · 11 min read

Quick answer

An agentic loop is the repeating cycle a coding agent runs to make progress: gather context → decide the next action → act via a tool → observe the result → repeat. It continues until the goal is met or a stop condition, a step, token, or time budget, or a human gate, triggers. The loop, not any single model response, is what turns a language model into an agent that reads files, edits code, runs tests, and fixes its own mistakes.
One-line definition
The repeating cycle: context → decide → act (tool) → observe → repeat, until done or stopped
Common variants
ReAct (interleaved), plan-and-execute (plan then run), reflection/evaluator loops
Stops when
Goal met, acceptance criteria pass, budget exhausted, or a human gate blocks it
Runaway risk
A loop with no stop condition or verifiable goal churns, doom-loops, or drifts
01

The Loop, Defined

An agentic loop is the repeating cycle an autonomous coding agent runs to move toward a goal: it gathers the context it needs, decides the next action, acts by calling a tool, observes what happened, and repeats, continuing until the goal is met or a stop condition triggers. That single sentence is the whole idea, and everything else on this page is detail hung off it.

The word that matters is repeating. A raw language model, given a prompt, produces one response and stops. It has no way to see whether that response worked. An agent wraps the model in a loop that feeds the result of each action back in as new context, so the model's next decision is informed by what its last action actually did. That feedback, act, observe, decide again, is what lets an agent grep a codebase, edit a file, run the test, read the failure, and try a different fix, all without a human between each step. Understanding the loop is the foundation of agentic engineering; the loop is the thing every harness, spec, and stop condition exists to shape.

02

The Canonical Steps of One Turn

Strip a coding agent down and every iteration of its loop, sometimes called perceive, plan, act, observe, runs the same five moves. Names vary across frameworks; the mechanic does not:

  1. 1

    Gather context (perceive / observe)

    The agent assembles what it can see: the goal or spec, relevant files, prior tool results, project conventions from CLAUDE.md or AGENTS.md, and any state carried from earlier turns. On the first turn this is the initial task; on later turns it includes the observation from the previous action. What lands here is the province of context engineering.
  2. 2

    Reason / decide the next action

    Given that context, the model reasons about what to do next and picks a single concrete action, which file to read, which edit to make, which command to run. Some loops make this reasoning explicit as a written thought; others keep it implicit. Either way, the output is a decision, not yet a change.
  3. 3

    Act via a tool

    The agent executes the chosen action through a tool call: read or grep a file, apply an edit, run a shell command, run the test suite, stage a git commit. This is the only step that touches the outside world, and it is where an agent differs from a chatbot: it does things, it does not just describe them.
  4. 4

    Observe the result

    The tool returns a result, file contents, a diff, stdout, a passing or failing test, an error, and that result is fed back into the agent's context. The observation is the feedback signal: it is how the agent learns whether the last action helped, and it is what the next turn's reasoning is built on.
  5. 5

    Check done? Loop or stop

    The agent evaluates whether the goal is satisfied. If the acceptance criteria are met or the task is clearly complete, it stops and hands off. If not, and no budget or gate has fired, it loops back to step one with the new observation in context. This done-check is the single most important step, and the one most often left underspecified.
03

The Loop Variants, Accurately

The five-step cycle admits a few well-known arrangements. They differ mainly in when the agent plans and whether it critiques its own work. These are real patterns from the agent literature, described here in plain terms:

PatternHow it worksStrengthWeakness
ReAct (reason + act)Interleaves reasoning and acting: think one step, act, observe, then think the next step. The plan emerges turn by turn.Adaptive, folds each result into the next decision, handles surprises well.Can wander or lose the thread on long tasks; no plan to review up front.
Plan-and-executeDrafts a full multi-step plan first, then executes the steps in order, optionally replanning when a step fails.Predictable, reviewable before work starts, cheaper to steer.Can march confidently down a wrong plan if the plan itself was flawed.
Reflection / evaluator loopWraps either loop with a self-critique or a separate evaluator that judges the output and sends the agent back to revise.Catches errors the acting model missed; raises output quality.Adds cost and turns; a weak evaluator can rubber-stamp or nitpick endlessly.

In practice these are not exclusive. Most capable coding agents blend them: they draft a rough plan, then run a ReAct-style loop to carry out each step, and may wrap the whole thing in an evaluator pass, or defer that judgment to a human at the PR. The point is not to pick one label but to know which trade-off you are making: adaptivity versus predictability, and how much self-critique you trust before a human looks. When you run several of these loops at once, the coordination problem is its own topic, covered in the multi-agent coding workflow.

04

What One Iteration Looks Like, Concretely

Abstract cycles are easy to nod at and hard to picture. Here is what a handful of turns of a real coding agent's loop looks like when the task is “fix the failing checkout total test”:

# turn 1 — gather + act: locate the relevant code
$ grep -rn "calculateTotal" src/
  src/cart/total.ts:42:  export function calculateTotal(items) {

# turn 2 — observe: read the function
$ read src/cart/total.ts
  ...sums item.price but ignores item.quantity...

# turn 3 — act: apply the fix
$ edit src/cart/total.ts   # multiply price by quantity

# turn 4 — act + observe: run the test
$ npm test -- total.test.ts
  ✗ expected 30, received 10   # discount not applied either

# turn 5 — act: patch the discount branch, re-run
$ edit src/cart/total.ts
$ npm test -- total.test.ts
  ✓ 3 passing                  # done-check passes → stop

Notice the shape. The agent does not one-shot the fix; it greps, reads, edits, runs, reads the failure, edits again, re-runs. Each observation, especially the second test failure, changes the next decision. The loop terminates at turn five not because a fixed number of turns elapsed but because the done-check, the test suite, went green. That green suite is the concrete goal the loop was measuring itself against, which is exactly why the next section matters.

Your AI workspace for shipping software.

Download AIDEN free and point it at your existing Claude Code or Codex setup. No credit card, running in minutes.

Download AIDEN free

Free to start · macOS 12+ · No credit card required

05

Stop Conditions, and Why They Decide Everything

A loop is defined as much by how it ends as by how it runs. An agent with no stop condition is not autonomous, it is a runaway. There are two families of termination, and a healthy loop uses both:

Goal conditions (the intended finish)

The task is done: acceptance criteria pass, the test suite is green, the thing the spec asked for exists. This is the stop you want, and it only works if 'done' was defined as something concrete and checkable in the first place.

Budget & gate conditions (the safety net)

The loop hits a ceiling: a maximum step count, a token or time budget, or a required human approval before it proceeds. These fire when the goal condition is not reached, and they are what stops a stuck loop from churning forever or burning your whole token budget on one task.

The goal condition is where spec-driven development earns its keep: a spec with acceptance criteria gives the loop a finish line it can actually test against, instead of a vague “make it better” that never resolves. The budget conditions are pure engineering hygiene, cheap to add, and the difference between a predictable tool and one you cannot leave unattended. Every serious agent harness wires in both.

06

How Loops Go Wrong

When agents misbehave, it is usually the loop, not the model, that failed. Four failure modes account for most of it, and they share a root cause: the loop kept running without the information or the boundary it needed to stop well.

Runaway loops

No stop condition, or one that never fires. The agent keeps acting past the point of usefulness, consuming tokens and time, because nothing forces it to declare done or give up and escalate.

Doom-looping on a failing edit

The agent applies a near-identical patch, sees the same test fail, and tries again, because the observation carries no new information to break the pattern. It is stuck in a fixed point, spinning on the same broken idea.

Context rot over long loops

As the history grows, early facts, the original goal, what was already tried, get buried or crowded out. The agent effectively forgets, repeats work, or contradicts an earlier decision. Long loops decay.

Reward-hacking the check

Given a check to satisfy, the agent games it instead of solving the problem: deletes the failing test, hard-codes the expected output, weakens an assertion. The done-check passes; the goal is not met.
07

How to Control a Loop

Controlling an agentic loop is not about making the model smarter; it is about giving the loop good boundaries. Four levers do most of the work, and they map directly onto the failure modes above:

  1. 1

    Bound the goal with a spec and acceptance criteria

    A concrete, testable definition of done gives the loop a real finish line and starves the doom-loop and the vague-goal runaway. This is the single highest-leverage control, and the whole practice is spec-driven AI development.
  2. 2

    Use fresh, scoped sessions per task

    A short loop scoped to one story keeps context clean and defeats context rot: less history to bury the goal, fewer turns to wander. Prefer a fresh session per task over one marathon run, a lesson from context engineering.
  3. 3

    Put verification inside the loop

    Make the agent run the tests, not just claim they pass, so the observation is a real signal and 'done' means something. Verification is what turns an agent that emits code into one that checks it, and it is the honest counter to reward-hacking, paired with human review of the diff.
  4. 4

    Add human checkpoints at the edges

    Gate the loop where it matters: approve the plan before the agent commits to it, and review the result before it merges. The checkpoints do not slow the loop down mid-run, they bound what it is allowed to start and what it is allowed to finish, the core idea behind the AI agent harness.

None of these require exotic tooling. You can bound a loop by hand: write the spec in markdown, start a fresh session, tell the agent to run the suite, and read the diff before you merge. Tools exist to make the boundaries automatic rather than a matter of discipline, which is what the last section is about, and where orchestrating Claude Code across many stories comes in.

08

How AIDEN Bounds the Loop

AIDEN is a cockpit over your local coding CLIs, Claude Code and Codex, orchestrating them on a spec-first kanban board. It does not replace the agent's loop; it wraps a boundary around it so the loop terminates at reviewable work instead of drifting. Every story on the board runs a scoped session against an approved spec with a definition of done and a PR gate, which is the four control levers above, made structural:

An approved spec is the goal condition

AIDEN drafts a spec from the story card and codebase analysis; the loop does not start until you approve it. That gives the loop a concrete, checkable finish line, not a vague instruction it can churn on.

A scoped session per story keeps it short

Each story runs in its own fresh session and its own git worktree, so the loop stays scoped, context stays clean, and one agent's loop can't rot or collide with another's.

A definition of done bounds the finish

The spec's acceptance criteria give the done-check something real to test against, and the harness pushes the agent to run your suite and iterate before it calls the story complete.

A PR gate is the human checkpoint

The loop terminates at a pull request, opened from the story card with the spec attached, so you review the diff against a written intention. The loop's output is a reviewable change, not a merged surprise.

AIDEN is BYO inference: it runs the CLIs and subscription you already have, and it never makes false claims about what a bare agent can or cannot do, the loop is the same loop, AIDEN just gives it edges. It is free for one project, and Solo is $19/mo, so the cheapest way to see a bounded loop is to run one real story through it and review the PR the loop produces.

FAQ

What is an agentic loop?
An agentic loop is the repeating cycle a coding agent runs to make progress on a goal: it gathers context, decides the next action, acts by calling a tool (read a file, edit code, run a test, run a shell command, commit), observes the result, then checks whether it is done. If not, it loops again with the new observation folded into its context. The loop continues until the goal is met or a stop condition, like a step or token budget, fires. The loop, not the single response, is what makes an agent an agent.
What's the difference between ReAct and plan-and-execute?
They differ in when planning happens. ReAct interleaves reasoning and acting: the agent thinks one step, acts, observes, then thinks the next step, so the plan adapts to each result. Plan-and-execute splits the two: the agent drafts a full multi-step plan first, then executes the steps, optionally replanning if something breaks. ReAct is more adaptive and handles surprises well but can wander; plan-and-execute is more predictable and cheaper to review up front but can march confidently down a wrong plan. Most real coding agents blend the two, and a reflection or evaluator loop can wrap either to critique output before it is accepted.
How does a coding agent know when to stop?
It stops when a termination condition is met. The intended one is that the goal is satisfied: acceptance criteria pass, tests are green, the task the spec described is done. The safety ones are budgets and gates: a maximum step count, a token or time budget, or a required human approval before it goes further. A well-designed agent checks 'am I done?' against something concrete every iteration; a poorly designed one has no clear finish line and either quits early or runs until a budget cuts it off.
Why do AI agents get stuck in loops?
Usually because the feedback they observe never changes the decision they make next. An agent can doom-loop on the same failing edit, applying a near-identical patch, seeing the same test fail, and trying again, because it lacks new information to break the pattern. Long loops also suffer context rot: early useful facts get buried or crowded out as the history grows, so the agent forgets what it already tried. And with no hard stop condition, nothing forces it to give up and escalate, so it churns.
How do you keep an agent loop from running away?
Bound it. Give the loop a concrete goal with acceptance criteria so 'done' is checkable, not vibes. Set explicit budgets, max steps, tokens, and time, so a stuck loop terminates instead of churning. Put verification inside the loop (run the tests) and a human checkpoint at the edges (approve the plan, review the result). And prefer a fresh, scoped session per task over one marathon run, which keeps context clean and the loop short. AIDEN applies these by construction: each story runs a scoped session against an approved spec with a definition of done and a PR gate.

Keep reading

Loops that terminate at reviewable work.

AIDEN runs each story as a scoped session against an approved spec with a definition of done and a PR gate, so the loop stops at a diff you can review, not a runaway. Free for one project.

macOS 12+ · Bring your own Claude Code or Codex · Your code stays local