| name | continuous-agent-loop |
| description | Patterns for continuous autonomous agent loops with quality gates, evals, and recovery controls. Use when setting up autonomous workflows, choosing loop architecture, or building CI/CD-style development pipelines. |
| origin | MCC |
Continuous Agent Loop
Patterns, architectures, and selection guidance for running Claude Code autonomously in loops — from simple sequential pipelines to full RFC-driven multi-agent DAG orchestration.
When to Use
- Setting up autonomous development workflows without human intervention
- Choosing the right loop architecture for your problem
- Building CI/CD-style continuous development pipelines
- Running parallel agents with merge coordination
- Adding quality gates and recovery controls to autonomous workflows
Loop Selection Flow
Start
|
+-- Need strict CI/PR control? -- yes --> continuous-pr
|
+-- Need RFC decomposition? -- yes --> rfc-dag
|
+-- Need exploratory parallel generation? -- yes --> infinite
|
+-- default --> sequential
Loop Pattern Spectrum
From simplest to most sophisticated:
| Pattern | Complexity | Best For |
|---|
| Sequential | Low | Simple scripts, single-file changes |
| Continuous PR | Medium | Feature branches with CI validation |
| Infinite Loop | Medium | Exploratory/creative generation in parallel |
| RFC-DAG | High | Large features decomposed into independent work units |
Sequential Loop
Run claude -p in a loop with a task list. Simplest pattern — one agent, one task at a time:
while read -r task; do
claude -p "Complete this task: $task" --allowedTools Edit,Write,Bash
done < tasks.txt
When it works: Small changes, scripting, batch file modifications.
When it fails: Tasks that depend on each other, large features, anything requiring parallel work.
Continuous PR Loop
Agent works on a branch, opens PRs, and waits for CI to pass before continuing:
claude -p "Implement the feature described in TASK.md. Create a PR when done. If CI fails, fix the issues and push again."
Quality gates: CI must pass, linter must pass, tests must pass before merge.
Recovery: On CI failure, the agent reads the CI output and self-corrects.
Infinite Loop (Parallel)
Multiple agents run in parallel on isolated worktrees, each exploring different approaches:
for i in $(seq 1 3); do
git worktree add "/tmp/wt-$i" -b "attempt-$i"
claude -p "Implement the feature in /tmp/wt-$i" &
done
wait
When to use: Creative exploration, benchmark comparisons, searching for the best approach.
Coordination: A merge agent reviews all worktrees and picks the best result.
RFC-DAG Loop
Decompose a large feature into an RFC with independent work units, then execute as a directed acyclic graph:
- Decompose: Use
ralphinho-rfc-pipeline to create an RFC from a one-liner
- Plan: Build a dependency DAG of implementation tasks
- Execute: Run agents in parallel on independent tasks
- Merge: Coordinate merges in dependency order
- Verify: Run the eval harness after each merge
Combined Production Stack
For maximum reliability, combine these skills:
- RFC decomposition (
ralphinho-rfc-pipeline) — break work into verifiable units
- Quality gates (
plankton-code-quality + /quality-gate) — enforce standards on every change
- Eval loop (
eval-harness) — measure whether each iteration actually improved things
- Session persistence (
nanoclaw-repl) — survive restarts and context resets
Quality Gates
Every loop iteration should pass these gates before proceeding:
- Build passes —
npm run build / cargo build / go build returns 0
- Tests pass — no regressions from the change
- Linter passes — no new warnings introduced
- Coverage maintained — coverage does not drop below threshold
- Eval score improves — the change measurably improves the target metric
If any gate fails, the loop should:
- Read the failure output
- Attempt a fix (max 3 retries)
- If still failing, freeze and report
Failure Modes
| Failure | Symptom | Recovery |
|---|
| Loop churn | Many commits, no measurable progress | Freeze, narrow scope, add explicit acceptance criteria |
| Same root cause | Repeated retries with identical errors | Stop retrying, diagnose root cause, fix manually |
| Merge queue stalls | Parallel branches can't merge cleanly | Reduce parallelism, serialize dependent tasks |
| Cost drift | Token usage growing without bound | Set per-iteration budget caps, use cheaper models for simple tasks |
| Context exhaustion | Agent loses track of goals after compaction | Use session persistence, add goal reminders to loop prompt |
Recovery Protocol
When a loop gets stuck:
- Freeze the loop immediately
- Audit — run
/harness-audit to inspect what went wrong
- Reduce scope to the smallest failing unit
- Replay with explicit acceptance criteria and a fresh context
- Verify the fix passes all quality gates before resuming the full loop
Anti-Patterns
- Running loops without quality gates — leads to drift and broken code
- Unbounded retries — set a maximum retry count per task
- Ignoring cost — set budget caps and model routing (haiku for simple, sonnet for complex)
- Fire-and-forget parallel agents — always coordinate merges
- Skipping evals — without measurement, you cannot tell if the loop is making progress