Skip to main content

Anatomy of an Agent Loop

A loop that runs itself is more than "an agent in a while statement." The difference between a loop you can trust overnight and one that burns money on repeat is a handful of specific parts. This guide covers the six that every reliable loop needs.

info

This content was developed with AI assistance and is regularly reviewed for accuracy.

Learning Focus

By the end of this guide, you'll understand:

  • The six components of a dependable agent loop
  • What each part is responsible for
  • Which parts people skip — and what breaks when they do

The six parts

Picture a loop as a small assembly line the agent runs around. Each part has one job.

1. Trigger

What it is: the thing that starts an iteration.

A trigger is a schedule ("every night at 2am"), an event ("a new issue was labeled agent-ready"), or a queue that isn't empty. Without a trigger, you're back to starting each run by hand — which is just prompt engineering with extra steps.

2. Task selection

What it is: how the loop decides what to work on this iteration.

This is usually "take the next item off the queue," but it can include filtering ("skip anything that needs human sign-off") and prioritization ("do the smallest task first"). Good task selection is what keeps a loop from grabbing work it has no business doing autonomously.

3. Action

What it is: the agent actually doing the work — the prompt plus the tools and context it needs.

This is where your prompt-engineering and context-engineering skills live. The action might be one model call or an agent running its own internal sub-steps (reading files, running commands, searching the web). From the loop's point of view, it's a single "do the task" beat that produces a result.

4. Evaluation (the check)

What it is: deciding whether the result is good enough.

This is the part that separates a real loop from a runaway one. Evaluation can be:

  • Automated and objective — tests pass, the build is green, the schema validates, the number is in range.
  • Rubric-based — the agent (or a second "judge" agent) scores the output against explicit criteria.
  • Human — a person approves before anything ships.

If a loop can't tell good output from bad, it can't make good decisions. No evaluation means no trustworthy loop.

5. Stop condition

What it is: the rule that ends the loop.

Every loop needs at least one, and usually several:

  • Success: the goal is met (tests green, queue empty, digest sent).
  • Budget: a cap on iterations, time, or money — "stop after 10 tries or $5, whichever comes first."
  • Failure: repeated failed checks, or the same error twice in a row.
  • Escalation: a condition that hands control to a human instead of continuing.

A loop without a budget stop condition is the single most expensive mistake in loop engineering. Write it before anything else.

6. Memory (state)

What it is: what the loop writes down between iterations.

Memory is how the loop knows what it already tried, what's done, and what failed — so it doesn't repeat itself or lose progress if it restarts. At minimum, log each iteration's task, result, and decision. This record is also your debugging trail and your audit trail when something goes wrong.

A worked example

Here's the same anatomy applied to a simple "fix flaky tests" loop:

PartIn this loop
TriggerRuns every night at 1am
Task selectionPicks the test file with the most failures in CI
ActionClaude Code reads the file, diagnoses the flake, and patches it
EvaluationRe-runs the test 20 times; passes only if it's green every time
Stop conditionStops after 3 files fixed, or after $4 of usage, or on any failed eval
MemoryAppends each attempt (file, diagnosis, pass/fail) to a log and opens one PR

Notice that five of the six parts exist to keep the agent honest and bounded. The "smart" part — the agent action — is only one beat.

What people skip (and what breaks)

  • Skip the stop condition → the loop runs until you notice the bill.
  • Skip evaluation → the loop "succeeds" on broken output and repeats the mistake at scale.
  • Skip memory → the loop re-does finished work or loops on the same failure forever.
  • Skip task selection filtering → the loop grabs a high-stakes task it shouldn't touch unattended.

Key Takeaways

  • A reliable loop has six parts: trigger, task selection, action, evaluation, stop condition, and memory.
  • The agent's "smart" work is only one of the six — the rest exist to keep it bounded and honest.
  • Evaluation is what makes a loop trustworthy; a loop that can't check its own work can't be trusted.
  • A budget-based stop condition is non-negotiable — write it first.

Next Steps

Continue to: Loop Guardrails & Safety before you let any loop run unattended.