Loop Command
Theloop command runs Claude Code in autonomous loops, spawning fresh agent sessions for each iteration. This approach, known as the “Ralph Wiggum pattern” (credited to Jeffrey Huntley, popularized by Matt Pocock), maintains context quality by avoiding long-running sessions that accumulate context fatigue.
Overview
Instead of a single long-running agent session that degrades over time, the loop command:- Spawns fresh sessions - Each iteration starts with a clean context
- Uses preset prompts - Built-in or custom prompts guide each iteration
- Tracks progress - A progress file persists notes across sessions
- Detects completion - Special markers signal when work is done or blocked
Basic Usage
Work through the backlog with the default task-completion preset, one iteration per pending task:taskgraph next picks. It runs one iteration unless you pass -n:
--id works with the default and tdd presets and with custom prompt files. The other presets pick their own work, so loop refuses --id with them. A task ID that doesn’t exist fails before Claude Code starts.
Watch Claude work as it goes, instead of seeing each iteration’s output when it ends:
Permission prompts
Each iteration runsclaude -p with permission prompts off (--dangerously-skip-permissions), so Claude can read, edit and run anything in the project without asking. The first run in a project asks you to allow that and remembers your answer in ~/.taskgraph/loop-trusted.json. In a script or CI, where nothing can answer, pass --trust. To keep Claude off your machine, pass --sandbox to run each iteration in a Docker sandbox (docker sandbox run claude) instead; the first sandbox run asks you to sign in.
How It Works
Each loop iteration:- Builds a prompt from a short header (the progress file and
CLAUDE.mdas@references, the iteration number, the tag and any--idtask) followed by the preset or custom prompt - Spawns
claude -p <prompt>as a fresh subprocess in the project root - Captures output and checks for completion markers
- Waits 5 seconds before the next iteration
- An agent outputs
<loop-complete>REASON</loop-complete>- all work done - An agent outputs
<loop-blocked>REASON</loop-blocked>- cannot proceed - Maximum iterations reached
CLI Options
Option Details
--prompt accepts two types of values:
- Preset names: Use built-in presets like
default,tdd,test-coverage,linting,duplication, orentropy - File paths: Provide a path to a custom prompt file (e.g.,
./my-custom-loop.md)
Run tasks in parallel
taskgraph loop --parallel N runs up to N independent tasks side by side, each in its own git worktree and branch. See Run tasks in parallel.
Built-in Presets
Six built-in presets cover common workflows:default (the TaskGraph backlog), tdd (the backlog, test-first), test-coverage, linting, duplication and entropy. See Loop Presets for what each one does.
Custom Prompts
You can create custom prompt files for specialized workflows that aren’t covered by the built-in presets.Specifying a Custom Prompt
Pass the file path to the--prompt option:
Required Structure
Custom prompts should follow this structure to work effectively with the loop system:File References with @ Syntax
The@ prefix makes files available to the agent. Claude Code will read these files when processing the prompt:
@ prefix, file paths are just text. With @, the agent can actually read and reference the file contents.
Completion Markers
The loop watches for two special markers in agent output:
When either marker is detected, the loop exits early. The reason text is captured in the final result.
Example usage in a custom prompt:
Best Practices
- Be specific: Clearly define what “done” means for your workflow
- One task per iteration: Instruct agents to complete exactly one unit of work
- Use the progress file: It is attached to every prompt; tell agents to append a note to it so the next iteration sees what was done
- Include quality checks: Add steps for running tests or type checks before completing
- Define completion criteria: Tell agents exactly when to output
<loop-complete> - Handle edge cases: Define when to output
<loop-blocked>to avoid infinite loops
Progress File
The progress file is a shared log that persists notes across loop iterations. Since each iteration spawns a fresh agent session, this file provides continuity.Default Location
--progress-file option:
How It Works
- Start: The loop appends a header to the file, creating it if needed. Earlier runs’ notes are kept.
- Each iteration: The prompt attaches the file with an
@reference, and the agent appends notes about its work - End: The loop appends a summary of the run
File Format
The loop writes the header and the summary; the lines in between are whatever the agents append:# Tag: line appears only with --tag.
Using Progress in Custom Prompts
The loop attaches the progress file to every prompt, so a custom prompt doesn’t need to reference it. Tell agents to append notes:Real-World Examples
Here are practical scenarios for using the loop command in your development workflow.Weekend Automation
Run a loop overnight or over the weekend to clear your task backlog while you’re away:--trust skips the permission question so nothing waits for you, and && sends a desktop notification only if the loop didn’t end blocked or on an error.
Test Coverage Sprint
Push your test coverage from 60% to 80% systematically:Tech Debt Day
Dedicate a day to cleaning up lint errors across your codebase:Code Review Prep
Reduce code duplication before opening a PR:Multi-Tag Workflow
Process different parts of your codebase in sequence:Custom Migration Script
For complex migrations, create a custom prompt and run it:Related documentation:
- CLI Root Commands - Full command reference
- Task Structure - Understanding task organization

