Agent and Loop Engineering Patterns
An agent can reason, act, observe, and continue, but production work needs more than an open ended loop. The system must decide which tasks deserve agents, which agent configuration is eligible, when to use one agent or several, what happen
A rapid review of the chapter’s existing Quick Read, principles, definitions, lessons, and review material.
Reconstruct and defend this chapter’s architecture.
Reconstruct the architecture, name each boundary, and defend the tradeoffs.
Open the source exercise
Design a workflow that classifies an issue, routes it to a specialist profile, plans, implements, verifies, repairs twice, detects oscillation, and escalates. Show deterministic and model decisions, typed handoffs, budgets, stop conditions, separate Attempts, and the human decision packet.
3. Enduring Principle
Use the least agentic mechanism that solves the problem
Choose among:
- a deterministic function for known transformations and policy;
- a deterministic workflow for stable ordered work;
- one agent loop for bounded ambiguous reasoning and tool use;
- a routed set of specialized agents when task classes differ materially; and
- a multi-agent workflow when independence, parallelism, context isolation, or distinct expertise creates measurable value.
The objective is not to maximize agent count. It is to minimize total error, human attention, cost, and lead time while preserving authority and evidence.
Define a task-specific agent profile
A Task-Specific Agent Profile records the capabilities required for a class of work: reasoning depth, context window, tool use, structured output, repository scale, environment, latency, cost, security, privacy, availability, and historical evaluation. It binds an eligible model route, instructions, skills, tools, context policy, harness capabilities, budgets, and verifier requirements.
Profiles should reflect task roles such as classification, planning, implementation, review, recovery, or summarization. They are eligibility templates, not permanent assignments to one model provider.
Route conditionally under policy
Conditional routing uses observable state to select a permitted next node:
- task type, risk, complexity, repository, and required capability;
- confidence or ambiguity calibrated on representative cases;
- tool, provider, environment, and capacity availability;
- cost, latency, retry, and attention budgets;
- prior failures and changed hypotheses; and
- required independence or human authority.
Deterministic routing should handle known rules. Model classification may propose a route for ambiguous inputs, but the orchestrator filters it through eligibility and records alternatives, reason, uncertainty, and fallback.
Use named orchestration patterns
| Pattern | Use when | Principal risk |
|---|---|---|
| Single agent with tools | One bounded context and authority set is sufficient | Broad context and self-confirmation |
| Router to specialist | Task classes need materially different profiles | Misclassification and hidden fallback |
| Planner then executor | Upfront decomposition reduces implementation ambiguity | Plan becomes stale or invents requirements |
| Generator then independent verifier | Output needs separate assurance | Correlated model, context, or tool failure |
| Parallel fan-out/fan-in | Independent research or candidate generation benefits from parallelism | Cost, duplication, synthesis error |
| Map-reduce | Work divides into uniform independent units | Lost global invariant |
| Supervisor-worker | Dynamic delegation is required | Supervisor becomes unbounded authority bottleneck |
| Debate or adversarial review | Competing hypotheses improve a consequential judgment | Confident argument without external evidence |
| Human escalation | Meaning, risk, authority, or unresolved ambiguity exceeds automation | Poor decision packet and approval fatigue |
Every handoff should use a typed contract containing objective, source lineage, scope, inputs, outputs, unresolved questions, evidence, budget, and completion state. Conversational memory is not a reliable handoff contract.
Engineer the attempt loop explicitly
The canonical loop is Generate → Verify → Diagnose → Repair or Replan → Retry → Escalate or Stop. Verification should produce structured findings linked to criteria. Retry requires a changed hypothesis, input, tool, configuration, or recovery action. Repeating the same conditions is not a strategy.
Define convergence and stop conditions
Track progress through resolved criteria, failing tests, finding count and severity, changed uncertainty, artifact distance, policy state, and consumed budgets. Stop or escalate when:
- the required outcome is independently verified;
- a hard gate fails;
- work requires authority the Attempt does not possess;
- the retry, token, time, tool, compute, or monetary budget is exhausted;
- consecutive iterations produce no material progress;
- the loop oscillates between prior states;
- new work expands the approved scope;
- the environment or dependency is not trustworthy;
- evidence becomes stale or contradictory; or
- a human decision is required.
An iteration limit is a final containment boundary, not the only convergence mechanism.
Separate retry, fallback, replan, and escalation
- Retry repeats a logical operation after a transient or corrected failure.
- Repair changes the artifact or local implementation hypothesis.
- Replan changes the authorized sequence while preserving approved intent and scope; material changes require a new Plan revision.
- Fallback selects a different eligible route, tool, or environment under policy.
- Escalation asks a human or higher authority to resolve a bounded decision.
- Stop contains unsafe, unauthorized, or non-converging work.
Each action creates new history. It must not overwrite the failed Attempt or hide why the strategy changed.
Preserve verifier independence inside multi-agent workflows
Different role prompts on the same model and context may produce correlated errors. Choose independence according to consequence: separate execution, different tools or methods, deterministic checks, blinded context, different model families, or human review. The verifier must not silently edit the candidate it is certifying.
8. Notes and lessons learned
- A retry without a changed hypothesis is usually repeated cost, not recovery.
- Multi-agent architecture is justified by measurable independence, parallelism, specialization, or context isolation.
- Convergence belongs to the runtime contract, not to a model's confidence.
- The best router often filters with deterministic policy before asking a model to rank eligible choices.
9. Interview and discussion questions
- When should a deterministic workflow replace an agent?
- What belongs in a Task-Specific Agent Profile?
- How do retry, repair, replan, fallback, and escalation differ?
- Which signals show that an agent loop is not converging?
- When is a multi-agent system worth its coordination cost?
- How would you establish verifier independence for a high-risk migration?
Review this chapter.
Challenge a claim, boundary, missing failure mode, unclear term, or unsupported evidence statement.
- Claim
- Boundary
- Failure
- Evidence