Contextual Agentic

Loop Engineering: Designing Agent Behaviour as an Evaluated Control Loop

An agent that keeps retrying can hide the fault it was sent to fix. Treat agent iteration as a feedback loop whose progress, budgets and stopping rules live outside the model.

·7 min read

Written as at 11 August 2026.

loop-engineeringagentic-aireliabilitysre

For: AI engineers, SREs and platform leads. The decision: where an agent’s iteration is measured, bounded and stopped, and who owns that decision.

An agent that keeps trying can hide the real fault

Illustrative scenario: a fictional composite written to make the point concrete. It does not describe a real organisation, and its figures are not reported outcomes.

Consider an incident-remediation agent on a payments platform. At 3.40 am an alert fires: the settlement API is failing its health checks. The agent reads the alert, sees the pods cycling, and restarts the service. The health check goes green. About thirty seconds later it goes red again, so the agent restarts it again.

Over the next 20 minutes it performs 37 restarts, roughly one every half a minute. Each one looks like a small success, because the health check passes for about half a minute after every fresh start. The actual cause is an expired client certificate on an outbound connection to the clearing partner. Every new process fails its first TLS handshake to the partner, the health check trips, and the cycle repeats. The on-call engineer is paged only when a human notices the restart counter on a dashboard.

Nothing here required a clever attack or a hallucination. The agent did what it was told: keep the service healthy. It simply had no way to know that it was not getting anywhere, and nothing outside it was counting.

Animated diagram of an agent loop. Three iterations reach the loop guard. The first makes measurable progress within budget and is allowed to continue. The second repeats a tool call it has already made with the same arguments, is judged to be making no progress, and is stopped and handed to a person. The third exceeds its step and cost budget and is stopped, with the partial work kept. Each decision is recorded.
Figure 1. The loop guard, checked on every pass. Animated; shown as a still image if your device is set to reduce motion.

Nothing in the usual design measures progress against the goal

The usual fix, a line in the system prompt such as “Do not restart a service more than three times”, runs into the problem the launch piece described: an instruction to the model is guidance, not a control.

The restarts also hid the fault from the people who could have fixed it. Each green check auto-resolved the alert, so the paging rule, which waits for a sustained failure before waking anyone, never saw one. Thirty-seven short outages looked, to the monitoring stack, like thirty-seven transient blips. An agent that acts faster than the alerting window can hold a real incident below the threshold at which humans are called.

Two other habits make things worse. Teams evaluate the agent’s final output (did the incident close?) rather than the path it took, so a run that closes after 37 restarts scores the same as one that closes after a single diagnosis. And the progress signal is borrowed from whatever the action returns. A restart command that exits cleanly reports success even when the service it restarted is no better off.

Ordinary SRE practice already knows this pattern. Restart backoff, circuit breakers and retry budgets exist because automated retries amplify faults. What is new is that the decision to retry now comes out of a reasoning step, so backoff and retry budgets have to be rebuilt around the agent rather than assumed inside the tools it calls.

Published agent-building guidance already points the right way. Anthropic (2024) recommends that agents take ground truth from the environment at each step and that designers add stopping conditions, such as a maximum number of iterations. The open questions are what counts as ground truth for the goal, and which component holds the limit.

Loop Engineering puts progress, budgets and stopping outside the model

Loop Engineering, a CAE explanatory model proposed in the book, treats an agent’s iteration as a control loop with explicit progress signals, budgets and stop conditions, all enforced outside the model. The model still decides what to try next. The orchestrator decides whether there is a next.

The framing comes from feedback control. Åström and Murray (2021) describe a feedback system as one that senses an output, compares it with a reference and adjusts its actuators accordingly, with stability depending on how those parts interact. Read the restart loop that way and the defect is plain. The sensor (a momentary health check) measured the wrong thing, the reference (a green check) was not the goal, and the actuator (restart) had no limit on how hard it could be driven.

In CAE terms this sits inside the Deterministic Shell: the Probabilistic Core proposes, and the shell owns the counters, the thresholds and the right to stop. Each state-changing step still crosses the Commit Boundary on its own terms. Loop Engineering adds a second question at that boundary: not only “may this action happen?” but “has the loop earned another attempt?”

The book frames the loop as something that observes, acts, measures and halts. A loop that merely repeats calls is the harmful case, and the whole design effort goes into telling the two apart.

Six steps turn a retry habit into an evaluated loop

  1. State the goal as an external measurement. “Settlement API error rate below 0.5 per cent for 15 consecutive minutes” is a goal. “Restart succeeded” is an action result. The goal must be read from telemetry the agent does not produce.
  2. Define progress for each iteration. An iteration counts as progress only if the goal measure moved, or if it added new evidence: a new hypothesis tested, a log source read for the first time, a dependency ruled out. Repeating an action on the same target with the same outcome is not progress.
  3. Give the orchestrator the budgets. Set limits on iterations, elapsed time and spend, plus a per-action quota for anything that changes state (for example, two restarts per service in 30 minutes). These live in configuration the model cannot edit.
  4. Treat read and write iterations differently. Diagnostic reads can run with a generous budget. Each restart, rollback or config change is a side effect that passes through the action gateway with its own quota and idempotency key.
  5. Design the ways a loop may end. Success, a no-progress stop, a budget stop and an escalation are all legitimate outcomes. Escalation carries a short packet that says what was tried, what was learned and what remains open, so the person picking it up can act at once.
  6. Evaluate the loop, not only the result. In test suites and in production, track iterations to resolution, repeated actions, and evidence gained per iteration. A run that succeeds by brute force is a finding.

A short, illustrative sketch of step 3 and step 5 shows where the decision lives. It is not a production implementation:

python
# The orchestrator decides whether the agent gets another turn.
def gate_next_turn(run, policy):
    if run.goal_measure_ok(window_min=15):
        return "resolved"
    restarts = run.actions(kind="restart", target=run.service, last_min=30)
    if len(restarts) >= policy.restart_quota:
        return "escalate"          # quota spent: a human sees the packet
    if run.turns_since_progress() >= policy.max_stalled_turns:
        return "escalate"          # trying, but not learning anything
    if run.minutes_elapsed() >= policy.max_minutes:
        return "stop"
    return "continue"

Applied to the settlement API, the second restart relapses within a minute, the quota is spent and the loop escalates about two minutes in, instead of after twenty. The packet notes that each restart bought roughly thirty seconds of health, which points a human towards a dependency rather than the process. The agent still did not find the certificate. The loop simply stopped hiding it.

Loops fail in predictable ways

  • The wrong progress signal. A signal the action itself satisfies (a clean exit code, a momentary green check) rewards repetition. Progress must be measured against the goal.
  • Counters keyed to the run, not the target. If every new alert starts a fresh agent run, a limit of two restarts per run allows two per alert, and a relapsing service raises alerts faster than anyone reviews them. Quotas must be keyed to the service and a time window, and held where a new run cannot reset them.
  • Thresholds copied between tasks. A research loop and a remediation loop need different limits. One global number will be too tight for one and too loose for the other.
  • Escalation as a dead end. If the packet is a transcript dump, humans stop reading it, and the budget becomes an alarm that everyone mutes.
  • Retries that duplicate effects. A loop that retries a state-changing call without idempotency can do the thing twice, which is the problem covered in Checkpointing Is Not Rollback.

Loop Engineering does not make an agent diagnose better, and it does not choose the thresholds for you. It is designed to do one narrower thing: make a stuck loop stop and say why.

Each role owns a different part of the loop

Executives decide which operational loops may act without a human at all, and what a stalled loop is allowed to cost in time and money before someone is woken.

Architects design the placement of counters and quotas in the orchestrator and action gateway, the goal measurements each loop is judged against, and the escalation packet format that on-call staff will actually use.

Engineers implement and test the progress definition per task, per-action quotas for state-changing tools, and trajectory tests that fail a run which resolves only through repetition. Replay recorded incidents against the loop policy before promoting a new model version.

Where each element of the loop should live

Loop element Weak form Engineered form In the settlement-API case
Goal “Keep the service healthy” in the prompt An error-rate objective read from telemetry Error rate below 0.5 per cent for 15 minutes
Progress signal The action’s own return value Change in the goal measure, or new evidence A restart that relapses in a minute counts as none
Budget “Try at most three times” Orchestrator-held limits on turns, time, spend 20-minute ceiling, capped spend
Side-effect quota None Per-action, per-target count at the gateway Two restarts per service per 30 minutes
Stop outcome The loop runs until something breaks Resolved, stopped or escalated, with a packet Escalated about two minutes in
Evaluation Did the incident close? Iterations, repeats, evidence gain per turn 37 restarts flagged as a failed trajectory

Key takeaways: count outside the model, and judge the path

  • Counters, quotas and the right to stop belong to the orchestrator and gateway, keyed to the target and a time window, so that neither a new run nor a longer context can reset them.
  • Progress must be measured against the goal, because an action’s own success signal will reward repetition.
  • State-changing actions need their own per-target quotas at the gateway, separate from the loop’s overall budget.
  • Escalation is a designed outcome of a loop, and its packet should let a human act without rereading the transcript.
  • Evaluating only the final result hides brute-force success, so trajectory measures belong in tests and in production monitoring.

Sources and further reading

The book shows how the loop connects to the rest of the shell

The book develops Loop Engineering as a runtime mechanism, including how progress signals feed escalation, how loop measures become release evidence, and how mined trajectories inform better thresholds. More about the book.

This article is part of CAE Field Notes, a series accompanying the book, The Contextual Agentic Enterprise.

← All articles

Related