apps/docs/capabilities/loop.mdx
The loop 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.
Instead of a single long-running agent session that degrades over time, the loop command:
This pattern is ideal for batch operations like clearing task backlogs, improving test coverage, or fixing lint errors across a codebase.
Run 10 iterations with the default task-completion preset:
task-master loop -n 10
Run 5 iterations with the test-coverage preset:
task-master loop -n 5 --prompt test-coverage
Run iterations with a custom prompt file:
task-master loop -n 10 --prompt ./my-custom-prompt.md
Filter to specific task tag:
task-master loop -n 10 --tag backend
Each loop iteration:
claude -p <prompt> as a fresh subprocessThe loop exits early when:
<loop-complete>REASON</loop-complete> - all work done<loop-blocked>REASON</loop-blocked> - cannot proceedEach iteration's agent has access to a shared progress file (default: .taskmaster/loop-progress.txt) via the @ file reference syntax. Agents append notes about what they completed, allowing subsequent iterations to build on previous work.
Example progress file content:
# Loop Progress - Started 2025-01-09 10:30:00
# Preset: test-coverage | Iterations: 10
[10:30:45] Iteration 1 (Task 5.2): Added unit tests for UserService
[10:35:22] Iteration 2 (Task 5.3): Added integration tests for auth flow
[10:40:18] Iteration 3: All pending test tasks complete
| Option | Type | Default | Description |
|---|---|---|---|
-n, --iterations | number | 10 | Maximum number of iterations to run. |
-p, --prompt | string | default | Preset name or path to a custom prompt file. Accepts: default, test-coverage, linting, duplication, entropy, or a file path like ./my-prompt.md. |
--progress-file | string | .taskmaster/loop-progress.txt | Path to the progress log file where iteration notes are appended. |
--sleep | number | 5 | Seconds to wait between iterations. Gives the system time to settle before spawning the next agent. |
--on-complete | string | - | Shell command to run when all tasks complete (status becomes all_complete). Example: --on-complete 'notify-send "Done!"' |
-t, --tag | string | - | Filter to only work on tasks with this tag. |
--status | string | pending | Filter to only work on tasks with this status. |
--project | string | - | Project root directory. Auto-detected if not provided. |
--json | flag | - | Output results as JSON instead of formatted text. |
--prompt accepts two types of values:
default, test-coverage, linting, duplication, or entropy./my-custom-loop.md)--on-complete runs only on success:
The shell command only executes when finalStatus is all_complete, meaning an agent output the <loop-complete> marker. It does not run on max_iterations, blocked, or error outcomes.
The loop command includes five built-in presets for common development workflows. Each preset guides agents to complete ONE focused task per iteration.
| Preset | Purpose | Completion Marker |
|---|---|---|
default | Complete tasks from Task Master backlog | ALL_TASKS_DONE |
test-coverage | Write meaningful tests for uncovered code | COVERAGE_TARGET |
linting | Fix lint and type errors one by one | ZERO_ERRORS |
duplication | Refactor duplicated code into shared utilities | LOW_DUPLICATION |
entropy | Clean up code smells (long functions, deep nesting) | LOW_ENTROPY |
The standard task completion workflow. Agents work through your Task Master backlog, completing one task per iteration.
task-master loop -n 10 --prompt default
What it does:
task-master next to get the highest priority available tasktask-master show <id>task-master set-status --id=<id> --status=doneCompletion criteria: All pending tasks are complete.
Improves test coverage by writing meaningful tests for user-facing behavior - not just chasing coverage numbers.
task-master loop -n 10 --prompt test-coverage
What it does:
npm run coverage, etc.)test(<file>): <describe user behavior tested>Philosophy: Don't write tests just to increase coverage. Use coverage as a guide to find untested user-facing behavior. Prioritize error handling, CLI commands, API endpoints, and file parsing over internal utilities.
Completion criteria: Coverage reaches target or 100%.
Cleans up lint errors and type errors systematically, one fix per iteration.
task-master loop -n 20 --prompt linting
What it does:
npm run lint, eslint ., etc.)tsc --noEmit, etc.)fix(<file>): <describe error fixed>Completion criteria: Zero lint errors and zero type errors.
Finds duplicated code using detection tools and refactors into shared utilities.
task-master loop -n 10 --prompt duplication
What it does:
npx jscpd . or similar)Completion criteria: Duplication below threshold (e.g., <3%).
Targets code smells that increase cognitive load and maintenance burden.
task-master loop -n 10 --prompt entropy
Code smells targeted:
What it does:
complexity-report)Completion criteria: No significant code smells remain.
You can create custom prompt files for specialized workflows that aren't covered by the built-in presets.
Pass the file path to the --prompt option:
task-master loop -n 10 --prompt ./my-custom-loop.md
The path can be absolute or relative to the current directory.
Custom prompts should follow this structure to work effectively with the loop system:
# Your Loop Title
Brief description of what this loop does.
## Files Available
- @.taskmaster/tasks/tasks.json - Your task backlog
- @.taskmaster/loop-progress.txt - Progress log from previous iterations
- @path/to/other/file.ts - Any other files the agent needs
## Process
1. Step one of the workflow
2. Step two of the workflow
3. ... more steps
## Important
- Complete ONLY ONE task per session
- Keep changes small and focused
- Do NOT start another task after completing one
- If work is complete, output: <loop-complete>REASON</loop-complete>
- If blocked, output: <loop-blocked>REASON</loop-blocked>
The @ prefix makes files available to the agent. Claude Code will read these files when processing the prompt:
## Files Available
- @src/index.ts - Main entry point
- @package.json - Dependencies and scripts
- @.taskmaster/loop-progress.txt - Progress from previous iterations
Without the @ prefix, file paths are just text. With @, the agent can actually read and reference the file contents.
The loop watches for two special markers in agent output:
| Marker | Purpose | Example |
|---|---|---|
<loop-complete>REASON</loop-complete> | Signal that all work is done | <loop-complete>ALL_TESTS_PASSING</loop-complete> |
<loop-blocked>REASON</loop-blocked> | Signal that work cannot proceed | <loop-blocked>MISSING_API_KEY</loop-blocked> |
When either marker is detected, the loop exits early. The reason text is captured in the final result.
Example usage in a custom prompt:
## Important
- If all migrations are complete, output: <loop-complete>MIGRATIONS_DONE</loop-complete>
- If a migration fails with an unrecoverable error, output: <loop-blocked>MIGRATION_FAILED: describe error</loop-blocked>
@.taskmaster/loop-progress.txt so agents can see what previous iterations accomplished<loop-complete><loop-blocked> to avoid infinite loopsThe progress file is a shared log that persists notes across loop iterations. Since each iteration spawns a fresh agent session, this file provides continuity.
.taskmaster/loop-progress.txt
Override with the --progress-file option:
task-master loop -n 10 --progress-file ./my-progress.txt
@ reference and append notes about their workThe progress file uses a simple timestamped format:
# Loop Progress - Started 2025-01-09 10:30:00
# Preset: default | Iterations: 10 | Tag: backend
[10:30:45] Iteration 1 (Task 5.2): Implemented user authentication endpoint
[10:35:22] Iteration 2 (Task 5.3): Added input validation for login form
[10:40:18] Iteration 3 (Task 5.4): Fixed edge case in token refresh logic
[10:45:01] Iteration 4: All pending backend tasks complete
Format breakdown:
[HH:MM:SS] Iteration N (Task ID): DescriptionAlways include the progress file in your ## Files Available section:
## Files Available
- @.taskmaster/loop-progress.txt - Progress log from previous iterations
And instruct agents to append notes:
## Process
...
7. Append a brief note to the progress file about what was done
This creates a chain of context that helps each iteration build on previous work.
Here are practical scenarios for using the loop command in your development workflow.
Run a loop overnight or over the weekend to clear your task backlog while you're away:
task-master loop -n 50 --prompt default --on-complete 'notify-send "Loop complete!"'
This runs up to 50 iterations, completing tasks one by one. When all tasks are done, you'll get a desktop notification.
Push your test coverage from 60% to 80% systematically:
task-master loop -n 20 --prompt test-coverage --on-complete 'echo "Coverage target reached!"'
Each iteration identifies the most important untested user-facing behavior and writes a meaningful test. The agent prioritizes error handling, API endpoints, and CLI commands over internal utilities.
Dedicate a day to cleaning up lint errors across your codebase:
task-master loop -n 30 --prompt linting --sleep 3
With --sleep 3, iterations run faster for quick fixes. The agent systematically addresses type errors first (they break builds), then security-related lint errors, then others.
Reduce code duplication before opening a PR:
task-master loop -n 10 --prompt duplication --tag backend
The --tag backend flag focuses the loop on backend code only. Each iteration finds and refactors one instance of duplicated code into a shared utility.
Process different parts of your codebase in sequence:
# First, handle critical backend tasks
task-master loop -n 10 --tag backend --status pending
# Then, address frontend tasks
task-master loop -n 10 --tag frontend --status pending
For complex migrations, create a custom prompt and run it:
# Create custom-migration.md with your specific steps
task-master loop -n 20 --prompt ./custom-migration.md --progress-file ./migration-progress.txt
Use a separate progress file to track migration-specific notes.