Loop Engineer
Loops are the third object of attention in coding: source code → agent → loop.
Your job with this skill is to turn a user's goal into the right loop pattern,
scaffolded with guardrails so it can't "loopmaxx" (run forever against a vague
objective and burn money).
The one rule that prevents most disasters
Refuse to build a loop without a binary, verifiable exit condition. "Improve
the UX" has no pass/fail and produces infinite loops + large API bills. "Make
npm test exit 0" does. If the user's goal isn't binary, your first job is to
help them make it binary — not to scaffold the loop.
Decision tree — pick the pattern
Is the goal one binary check the agent iterates toward (tests pass, lint clean)?
│
├─ YES, one agent is enough .......................... HEADLESS WHILE-LOOP
│ (templates/headless-loop.sh) or first-party /goal
│
├─ Quality matters & "done" is a judgment call ...... EVALUATOR-OPTIMIZER
│ (writing, code that must meet a bar) (templates/evaluator-optimizer.sh)
│ → ALWAYS a separate critic agent
│
├─ You're improving the PROMPT itself, not the output META / PROMPT-REFINEMENT
│ (a loop that rewrites the prompt another (templates/meta-prompt-refine.sh)
│ loop runs, scored on a test set) → needs holdout + anchor set
│
├─ Subtasks can't be predicted up front ............. ORCHESTRATOR FAN-OUT
│ (delegate dynamically, then verify each) (templates/fanout-orchestrator.sh
│ or the Workflow tool)
│
└─ Just run something on a schedule / interval ...... /loop or cloud Routines
(poll, babysit PRs, recurring checks) (reference/primitives.md)
Nest these: an orchestrator loop can spawn evaluator-optimizer inner loops;
a meta loop wraps a headless loop and rewrites its prompt between runs.
Non-negotiable guardrail checklist
Before scaffolding ANY loop, confirm all five. See reference/guardrails.md for the why.
- Verifiable exit condition — a command/check that returns binary done/not-done.
- Max-iteration cap — a hard
for bound, enforced in code, not in the prompt.
- Budget cap in code — sum
total_cost_usd from --output-format json; stop
before the next call when over budget. Alerts are not enforcement.
- Sandbox — loops that edit files/run commands run in a worktree, container,
or branch — never unattended on
main.
- Human checkpoint — for anything outward-facing (push, deploy, send), the
loop stops and asks, or only proposes.
A loop missing #1 or #2 is a bug, not a loop. Don't ship it.
How to use this skill
- Read the user's goal; map it to a pattern via the decision tree.
- If the goal isn't binary/verifiable, fix that first (with the user).
- Copy the matching template from
templates/, fill the config block, and wire
the real exit check. Keep the guardrails.
- Walk the user through the five-point checklist for their specific loop.
- Tell them how to run it, how to stop it, and what it costs per iteration.
Reference (load as needed)
reference/primitives.md — every Claude Code loop primitive: claude -p,
--continue/--resume, --output-format json (cost), /loop, /goal,
Tasks (~/.claude/tasks, CLAUDE_CODE_TASK_LIST_ID), cloud Routines.
reference/taxonomy.md — the five patterns in depth + when to use each.
reference/guardrails.md — failure modes (loopmaxxing, cost blowups, evaluator
collusion/drift) and the mitigations, with the cautionary numbers.
templates/*.sh, templates/goal-loop.md — runnable scaffolds.
What NOT to claim
Anthropic's Building Effective Agents is the canonical taxonomy source, but it
does not officially endorse "shell fan-out loops" or "headless-in-CI with Task
tracking" — those are community patterns. Attribute them as such.
1---2name: loop-engineer3description: Set up, scaffold, and administer agentic loops in Claude Code — headless while-loops around `claude -p`, evaluator-optimizer (generator/critic) loops, meta / prompt-refinement loops (a loop that refines the prompts another loop runs), and orchestrator fan-out. Every loop ships with non-negotiable guardrails: a verifiable exit condition, a max-iteration cap, and in-code budget enforcement. Use whenever the user wants to build, design, run, wire, or supervise loops / agent loops / "loop engineering" / nested sub-loops, or asks how to make Claude prompt Claude on a schedule or until a goal is met.4---56# Loop Engineer78Loops are the third object of attention in coding: **source code → agent → loop.**9Your job with this skill is to turn a user's goal into the *right* loop pattern,10scaffolded with guardrails so it can't "loopmaxx" (run forever against a vague11objective and burn money).1213## The one rule that prevents most disasters1415**Refuse to build a loop without a binary, verifiable exit condition.** "Improve16the UX" has no pass/fail and produces infinite loops + large API bills. "Make17`npm test` exit 0" does. If the user's goal isn't binary, your first job is to18help them make it binary — not to scaffold the loop.1920## Decision tree — pick the pattern2122```23Is the goal one binary check the agent iterates toward (tests pass, lint clean)?24│25├─ YES, one agent is enough .......................... HEADLESS WHILE-LOOP26│ (templates/headless-loop.sh) or first-party /goal27│28├─ Quality matters & "done" is a judgment call ...... EVALUATOR-OPTIMIZER29│ (writing, code that must meet a bar) (templates/evaluator-optimizer.sh)30│ → ALWAYS a separate critic agent31│32├─ You're improving the PROMPT itself, not the output META / PROMPT-REFINEMENT33│ (a loop that rewrites the prompt another (templates/meta-prompt-refine.sh)34│ loop runs, scored on a test set) → needs holdout + anchor set35│36├─ Subtasks can't be predicted up front ............. ORCHESTRATOR FAN-OUT37│ (delegate dynamically, then verify each) (templates/fanout-orchestrator.sh38│ or the Workflow tool)39│40└─ Just run something on a schedule / interval ...... /loop or cloud Routines41 (poll, babysit PRs, recurring checks) (reference/primitives.md)42```4344Nest these: an **orchestrator** loop can spawn **evaluator-optimizer** inner loops;45a **meta** loop wraps a **headless** loop and rewrites its prompt between runs.4647## Non-negotiable guardrail checklist4849Before scaffolding ANY loop, confirm all five. See `reference/guardrails.md` for the why.50511. **Verifiable exit condition** — a command/check that returns binary done/not-done.522. **Max-iteration cap** — a hard `for` bound, enforced in code, not in the prompt.533. **Budget cap in code** — sum `total_cost_usd` from `--output-format json`; stop54 *before* the next call when over budget. Alerts are not enforcement.554. **Sandbox** — loops that edit files/run commands run in a worktree, container,56 or branch — never unattended on `main`.575. **Human checkpoint** — for anything outward-facing (push, deploy, send), the58 loop stops and asks, or only proposes.5960A loop missing #1 or #2 is a bug, not a loop. Don't ship it.6162## How to use this skill63641. Read the user's goal; map it to a pattern via the decision tree.652. If the goal isn't binary/verifiable, fix that first (with the user).663. Copy the matching template from `templates/`, fill the config block, and wire67 the real exit check. Keep the guardrails.684. Walk the user through the five-point checklist for their specific loop.695. Tell them how to run it, how to stop it, and what it costs per iteration.7071## Reference (load as needed)7273- `reference/primitives.md` — every Claude Code loop primitive: `claude -p`,74 `--continue`/`--resume`, `--output-format json` (cost), `/loop`, `/goal`,75 Tasks (`~/.claude/tasks`, `CLAUDE_CODE_TASK_LIST_ID`), cloud Routines.76- `reference/taxonomy.md` — the five patterns in depth + when to use each.77- `reference/guardrails.md` — failure modes (loopmaxxing, cost blowups, evaluator78 collusion/drift) and the mitigations, with the cautionary numbers.79- `templates/*.sh`, `templates/goal-loop.md` — runnable scaffolds.8081## What NOT to claim8283Anthropic's *Building Effective Agents* is the canonical taxonomy source, but it84does **not** officially endorse "shell fan-out loops" or "headless-in-CI with Task85tracking" — those are community patterns. Attribute them as such.