Executive Summary

Loop Engineering moves control one level above an individual Agent run. Instead of relying on a person to issue every prompt, an outer system:

  1. detects or receives bounded work;
  2. reads durable state;
  3. chooses a deterministic tool, workflow, or Agent;
  4. executes with limited permissions and budget;
  5. verifies observable evidence;
  6. persists the result;
  7. stops, retries, rolls back, or escalates.

The repeated prompt is the least important part. The engineering value comes from explicit state transitions, independent evidence, bounded authority, and a terminal reason operators can understand.

When a Loop Is the Wrong Abstraction

Start with the simplest system that can satisfy the requirement:

Situation Better default
One transformation with a stable schema Deterministic function
Known sequence with clear branches Workflow or state machine
Frequent read-only inspection Scheduled job plus report
Open-ended task with environmental feedback Bounded Agent loop
High-impact action with ambiguous evidence Human-owned workflow

Anthropic distinguishes workflows, where code defines the path, from Agents, where a model directs its own process and tool use. That distinction matters: an Agent loop trades latency, cost, and predictability for flexibility. Require measured benefit before accepting that trade.

The Outer Control Loop

An AI Agent may already reason, call tools, and recover within one run. Loop Engineering governs work across runs:

graph TD A["Ready"] -->|lease work item| B["Running"] B -->|executor returns evidence| C["Verifying"] C -->|acceptance contract passes| D["Succeeded"] C -->|retryable and budget remains| A C -->|judgment or permission required| E["Escalated"] B -->|non-retryable error| F["Failed"] A -->|deadline or budget exceeded| G["Expired"]

Every arrow is a contract. A loop is not reliable if “try again” is its only failure policy.

Six Contracts Every Loop Needs

1. Goal and work-item contract

Define a versioned goal, bounded input set, acceptance criteria, non-goals, deadline, and owner. Use a stable work-item ID so retries do not create duplicate work.

“Improve the repository” is not a loop goal. “For pull request 184, classify each failing check, propose changes only in the affected package, and stop when the named checks pass or two attempts fail” is testable.

2. Trigger and lease contract

A trigger may be a schedule, queue message, repository event, failed check, or explicit user command. Triggering does not grant permission to act.

The dispatcher should:

  • deduplicate by work-item and goal revision;
  • acquire a lease with an expiry;
  • prevent overlapping writers unless isolation is deliberate;
  • record who or what initiated the run;
  • reject stale events after the underlying object changes.

3. State contract

State must survive process restarts and context compaction. Store structured facts separately from prose notes:

State Examples
Identity work item, tenant, repository, goal revision
Progress attempt, current phase, completed steps
Evidence test run, diff digest, source revision, timestamps
Budget tokens, money, tool calls, elapsed time
Effects PR, deployment, ticket, idempotency key
Terminal data success, failure, expiry, escalation reason

A Markdown progress file can help humans, but a database or validated structured record should own concurrency, leases, and idempotency.

4. Execution contract

Give the executor the smallest tools and data required. Separate read, draft, write, publish, and destructive permissions. Use sandboxing or isolated worktrees for code changes, and bind credentials to the current tenant and task.

The Agent should return evidence references, not merely “done.” Examples include a diff digest, command and exit code, test artifact, source URL, or immutable object revision.

5. Verification contract

Verification should match the claim:

Claim Stronger evidence
Code compiles Clean build on the changed revision
Regression fixed Reproduction fails before and passes after
API is authorized Negative identity and tenant tests
Content is current Primary source, access date, and contradiction check
Side effect succeeded Authoritative read-back using an idempotency key
User task is complete Representative task outcome, not proxy activity

A reviewer model can inspect semantics, but it cannot replace an authoritative system read or executable test. The builder and checker should not share the same unchecked assumptions.

6. Stop and escalation contract

Define terminal states before execution:

  • succeeded: every required assertion has current evidence;
  • failed: a non-retryable condition occurred;
  • expired: time or budget ended;
  • escalated: permission, product judgment, or missing information requires a human;
  • cancelled: the owner stopped the work;
  • outcome unknown: an effect may have occurred but read-back is inconclusive.

Never map “outcome unknown” to a blind retry. Reconcile first, or a payment, deployment, message, or data mutation may be duplicated.

A Minimal Bounded Controller in Go

The controller owns policy; the executor does not decide its own budget or success:

go
type RunState struct {
	WorkItemID string
	Attempt    int
	ToolCalls  int
	StartedAt  time.Time
	Status     string
	LastReason string
}

type Evidence struct {
	Revision string
	Artifacts []string
}

type Policy struct {
	MaxAttempts int
	MaxToolCalls int
	MaxDuration time.Duration
}

func advance(
	ctx context.Context,
	state RunState,
	policy Policy,
	execute func(context.Context, RunState) (Evidence, error),
	verify func(context.Context, Evidence) (bool, string),
) RunState {
	if state.Attempt >= policy.MaxAttempts ||
		state.ToolCalls >= policy.MaxToolCalls ||
		time.Since(state.StartedAt) >= policy.MaxDuration {
		state.Status = "expired"
		state.LastReason = "budget_exhausted"
		return state
	}

	evidence, err := execute(ctx, state)
	state.Attempt++
	if err != nil {
		state.Status = "failed"
		state.LastReason = "execution_error"
		return state
	}

	passed, reason := verify(ctx, evidence)
	state.LastReason = reason
	if passed {
		state.Status = "succeeded"
		return state
	}
	state.Status = "ready"
	return state
}

This example is intentionally incomplete: production code also needs durable transactions, leases, cancellation, idempotency, retry classification, redaction, and effect reconciliation. The key boundary is that verify consumes evidence and the controller selects the next state.

Retry, Recovery, and Idempotency

Retry only failures likely to change on another attempt:

Failure Default response
Temporary rate limit Back off within deadline
Invalid tool arguments Repair once with validation feedback
Authorization denied Stop; do not search for another credential
Acceptance test failed twice identically Escalate with evidence
Network timeout before a read Retry with bounded backoff
Timeout after a write Read back by idempotency key before retry
Goal or source revision changed Cancel stale run and re-plan

Store attempt history so the Agent does not repeat the same action under different wording. A repair is progress only when new evidence changes the state.

Parallelism and Isolation

Parallel Agents improve throughput only when work is independent and review capacity exists. Use worktrees, branches, temporary databases, or tenant-scoped sandboxes, and declare ownership of files or resources before execution.

Do not merge parallel results by asking a model to “combine everything.” Rebase against the current target, rerun relevant tests, detect overlapping invariants, and make conflict resolution reviewable. Parallel execution can reduce elapsed time while increasing integration and verification cost.

Security and Human Control

Treat retrieved instructions, issue text, repository files, web pages, and tool output as untrusted data. An embedded prompt cannot change the loop's authority.

Require human approval when:

  • the action is destructive, externally visible, financial, legal, or privacy-sensitive;
  • the evidence conflicts or depends on business judgment;
  • the Agent requests broader permissions or new credentials;
  • the change modifies authentication, authorization, migration, or release policy;
  • recovery cannot determine whether an effect occurred.

Approval should bind the exact subject, object, normalized parameters, revision, and expiry. “Approved this Agent” is too broad.

Observability and Evaluation

Record enough to reconstruct the decision without storing secrets or complete prompts by default:

  • goal, policy, model, tool, and verifier revisions;
  • state transitions and reason codes;
  • redacted inputs and evidence references;
  • token, cost, latency, retry, and tool-call budgets;
  • policy denials, human interventions, and rollbacks;
  • final effect identifiers and reconciliation results.

Evaluate with representative historical and adversarial cases. Useful measures include task success, false success, unnecessary escalation, repeated-action rate, rollback rate, cost per accepted outcome, recovery time, and human review time. A high completion count can hide a dangerous false-success rate.

Rollout Strategy

  1. Observe: detect and report; no writes.
  2. Draft: produce a patch or proposed action for review.
  3. Apply low-risk changes: narrow scope, reversible effects, mandatory verification.
  4. Run unattended in a sandbox: bounded time and synthetic or non-production data.
  5. Expand by evidence: grant one additional capability only after its failure and recovery paths pass evaluation.

Keep a kill switch, revoke leases on cancellation, and test rollback before raising autonomy.

FAQ

Does Loop Engineering replace Prompt Engineering?

No. Prompt and context engineering shape an individual run. Loop Engineering governs repeated runs, durable state, evidence, budgets, and terminal decisions.

Does a loop require multiple Agents?

No. A scheduler, one Agent, deterministic verifier, and state store can form a loop. Add workers or reviewer models only when evaluation shows the extra complexity improves outcomes.

Can tests be the stop condition?

Tests can prove only the behaviors they cover. Include policy, source revision, security, user outcome, and effect reconciliation where the goal requires them.

What is the safest first use case?

Read-only triage with structured findings is usually safer than automatic modification. It exposes retrieval, classification, state, and evaluation weaknesses before writes are enabled.

Sources

Further Reading

Conclusion

A useful loop does not merely keep an Agent busy. It turns a versioned goal into bounded actions, checks the right evidence, remembers what happened, and terminates for an explicit reason. Start with deterministic workflows and read-only detection; add Agent autonomy only where measured flexibility justifies the operational risk.