All posts

Loop engineering: designing the loop around an AI agent

Prompt engineering shaped one answer. Context engineering shaped what the model sees. Agents that run for an hour need a third discipline: deciding how each step is checked, when the loop stops, where progress is kept and when a person is called.

4 min read

The first wave of LLM work was about the prompt: how to phrase one request to get one good answer. The second was about context: which documents, tools and history the model sees on each turn. Agents that work for minutes or hours, such as fixing a failing test suite, migrating a codebase or working through a queue of tickets, add a third layer. Most of their quality now comes from the loop around the model:

How is each step checked? When does the loop stop? Where is progress kept? When does a person get called?

We call designing those answers loop engineering. A strong model in a weak loop wanders, repeats itself and declares victory early. An average model in a well-designed loop can grind through surprisingly hard work.

The anatomy of a loop

Every agent loop, however it is implemented, has the same five parts:

  1. Goal and definition of done. This is something a program can check, not a feeling.
  2. One step. The agent makes one change or takes one action.
  3. Verify. Something outside the model judges the step.
  4. Record. The result is written somewhere that survives the context window.
  5. Decide. Continue, roll back, stop or escalate.

Most failures we see trace back to a missing or weak part: no real definition of done, verification by the model's own say-so, or progress kept only in the conversation.

1. Verifiers, ranked by how much to trust them

The verifier is the most important design decision in a loop. Not all checks are equal:

Verifier Trust Use for
Deterministic tests, type checks, schema validation High Anything with a right answer
Measured metrics (latency, cost, accuracy on a fixed eval set) High Optimisation loops
Rubric graded by a model, with examples Medium Writing, summaries, tone
A model asked "is this good?" Low Rarely on its own
The agent's own report that it is done Very low Never as the only check

The rule of thumb is to give the loop the strongest verifier the task allows, and to write one before writing the agent. If there is no test, the first job of the loop is often to create one, for example a failing test that reproduces the bug, so that every later step has something firm to push against.

2. Stop conditions

An agent without explicit stop conditions stops when it runs out of context or money. Define them up front:

  • Success: the definition of done passed.
  • Budget: maximum steps, tokens, wall-clock time or spend.
  • No progress: the verifier's score hasn't improved in N iterations.
  • Repetition: the same error, or the same edit, has appeared twice.
  • Boundary: the next step needs something outside the agent's scope, such as a production write, a payment or a message to a customer.

The last three matter most in practice. "Stop when stuck" is what keeps an overnight run from turning into a large bill and a mess to clean up.

3. Keep progress outside the context window

Long loops outlive their context. Conversations get compacted, sessions restart and subagents start empty. A loop that keeps its state only in the chat forgets what it tried and tries it again.

Give the loop a memory it can read at the start of every step:

  • A progress file: what the goal is, what has been tried, what worked, what is next.
  • Checkpoints: in code, a commit per accepted step, so a bad step is one revert away.
  • A keep/discard log: for optimisation loops, every experiment with its score and whether it was kept.
while (!budget.exhausted()) {
  const state = await readProgress();             // survives restarts
  const step = await agent.propose(state);        // one change, not ten
  const result = await verify(step);              // tests, evals, metrics
 
  if (result.better) await checkpoint(step);      // keep: commit
  else await rollback(step);                      // discard: revert
 
  await appendProgress(step, result);
  if (result.done) return report("success");
  if (stalled(state, result)) return escalate("no progress", state);
  if (step.needsApproval) return escalate("approval", step);
}
return escalate("budget", await readProgress());

One change per step is a deliberate constraint. When an agent changes ten things and the score drops, nobody, the agent included, knows which change caused it.

4. Budgets are part of the design

Budgets are not just a safety net; they shape behaviour. Set them per run and per step:

  • Per step: a cap on tool calls and tokens, so one confused step cannot consume the whole run.
  • Per run: a ceiling on spend and time, with a report when it is reached rather than a silent stop.
  • Per experiment: in optimisation loops, a fixed time box per attempt, so results are comparable.

5. Decide where the human sits

A loop does not mean "no people". It means people are placed where their judgement is worth the most:

  • Before: agreeing the definition of done and the boundaries.
  • At boundaries: approving anything irreversible or customer-facing.
  • After: reviewing the final diff or report, with the progress log as evidence.

What a loop removes is the person babysitting each step. That is where most of the time in "AI-assisted" work goes today.

A checklist for your next agent loop

  • A definition of done a program can check.
  • The strongest verifier the task allows, written before the agent.
  • One change per step, with a checkpoint or rollback after each.
  • Progress kept in a file the loop reads at every step.
  • Stop conditions for success, budget, no progress, repetition and boundaries.
  • A clear escalation path, and a report at the end that a person can review in minutes.

Prompts and context still matter. But once agents run for longer than a single exchange, the loop is where reliability comes from, and it is engineering work like any other.

Found this useful? Pass it to someone building with AI.

Zeuxo Templates

Start from a complete product, not a blank page.

Kiln, Tidings, Marrow — React 19 + TypeScript dashboards with every screen built. From $79.

Browse templates

Keep reading

Claude Skills & Harness · · 5 min read

How to write a Claude skill that actually gets used

A skill is a folder with a SKILL.md, but most skills fail before their first line is read: the description never triggers, or the body is too long to follow. How to write the trigger, keep the body lean, and know when a hook or plugin is the better tool.

  • #Claude Code
  • #Skills
  • #AI agents

AI Agents · · 4 min read

How to monitor AI agents running in parallel

Running five AI agents at once is easy. Knowing which are working, which are stuck and what each produced is not. What to track, and how to build a view you can trust.

  • #AI agents
  • #Agent Studio
  • #Engineering