Keep Claude Running for Hours
Table of content
Claude Code stops when it finishes a task. You come back, give it the next task, wait, repeat. This loop burns hours of your time.
The fix: structure your work as a todo list and let Claude process it autonomously. Craig Motlin runs sessions that last over two hours without human input. The pattern works for anyone willing to set it up.
The Core Loop
Three commands, repeated until the list is empty:
/todo → /commit → /compact → repeat
| Command | What it does |
|---|---|
/todo | Implement the next incomplete task |
/commit | Stage and commit with a message |
/compact | Summarize conversation, reclaim context |
Each iteration: pick a task, implement it, commit the result, compress the conversation, pick the next task. The loop continues until no tasks remain.
Todo File Structure
Store tasks in a markdown file Claude can read and update:
<!-- .llm/todo.md -->
- [ ] Add user authentication endpoint
- [ ] Implement JWT token generation
- [ ] Add refresh token rotation
- [ ] Write integration tests
- [ ] Update API documentation
Each task should be:
- Independently committable - One task, one commit
- Self-contained - All context needed is in the task description
- Ordered by dependency - Earlier tasks don’t depend on later ones
Bad task: “Implement authentication” (too vague) Good task: “Add POST /auth/login endpoint that accepts email/password and returns JWT”
The /todo Command
Create a custom command that extracts and implements the next task:
<!-- .claude/commands/todo.md -->
Read .llm/todo.md and find the first unchecked task (- [ ]).
Implement that task completely:
1. Write the code
2. Run tests to verify
3. Mark the task complete (- [x])
Stop after completing one task.
Do not proceed to the next task.
The critical constraint: one task per invocation. Without this, Claude tries to implement everything at once, context explodes, and quality drops.
The /compact Command
Long sessions fill the context window. The /compact command summarizes the conversation and clears history:
<!-- .claude/commands/compact.md -->
Summarize the current conversation state:
- What was just completed
- Current project state
- Any blockers or decisions made
Then clear conversation history but keep the summary.
Claude Code’s built-in /compact does this automatically. Run it after every commit to keep context lean.
Hierarchical Subagents
Instead of one agent doing everything, spawn subagents for specialized work:
/todo-all (orchestrator)
└── @do-todo (implements task)
├── @test-runner (runs verification)
├── @commit-handler (stages and commits)
└── @rebaser (handles upstream changes)
Each subagent runs in its own context window. Verbose output (test failures, build logs, merge conflicts) stays in the subagent. The main orchestrator sees only the result.
This separation prevents context pollution. Motlin’s longest autonomous run lasted over two hours because subagents absorbed the bloat.
Orchestrator Pattern
<!-- .claude/commands/todo-all.md -->
Loop until .llm/todo.md has no unchecked tasks:
1. Dispatch @do-todo for the next task
2. On success, continue
3. On failure, log the error and stop
Do not implement tasks directly.
Always delegate to @do-todo.
The orchestrator manages the loop. Workers do the implementation. Clean separation.
Parallel Worktrees
Git worktrees let you run multiple loops simultaneously without file conflicts:
# Create isolated workspace
git worktree add ../project-auth feature/auth
# Launch Claude in the worktree
cd ../project-auth && claude /worktree
Each worktree gets its own:
- Working directory (no file conflicts)
- Branch (changes stay isolated)
- Todo file (separate task list)
See Git Worktrees for Parallel Agents for the full setup.
Spawn Multiple Loops
# Create worktrees for each task stream
git worktree add ../project-tests tests/comprehensive
git worktree add ../project-docs docs/update
# Launch loops in each (separate terminal tabs)
cd ../project-tests && claude /todo-all
cd ../project-docs && claude /todo-all
Three loops running in parallel. Each processes its own todo list. You review and merge when they finish.
Context Isolation Strategy
Subagents prevent context bloat because they run in separate windows:
| Approach | Context usage | Problem |
|---|---|---|
| One agent, all tasks | Grows with each task | Quality degrades after 5-6 tasks |
| Subagents | Resets per task | Stays fresh indefinitely |
When a subagent finishes, its context is discarded. The orchestrator only receives a short result. The main window never sees the 50 lines of test output or the 200-line diff.
Configure your orchestrator to keep minimal state:
<!-- .claude/agents/do-todo/description.md -->
Implements a single task from the todo list.
Returns only:
- Success/failure status
- Brief description of changes
- Any blockers requiring human input
Phone Notifications
Walk away and get notified when Claude needs you:
{
"hooks": {
"Stop": [{
"matcher": "",
"hooks": [{
"type": "command",
"command": "if [ \"$NOTIFICATIONS\" = \"true\" ]; then notify-send \"Claude: $(basename $(pwd))\"; fi"
}]
}]
}
}
Replace notify-send with your notification tool:
- macOS:
osascript -e 'display notification' - Pushover: HTTP POST to API
- Slack: Webhook to a channel
Set NOTIFICATIONS=true in your shell when you want alerts.
The Ralph Loop Plugin
Anthropic’s official ralph-wiggum plugin automates this entire pattern:
/ralph-loop "Migrate all tests from Jest to Vitest" \
--max-iterations 50 \
--completion-promise "All tests migrated"
The plugin:
- Intercepts session exits with a stop hook
- Re-prompts Claude with the original task
- Continues until the completion promise is satisfied or max iterations reached
Install it:
claude /install-plugin ralph-wiggum
Common Mistakes
Tasks too large. Each task should take 5-15 minutes. If Claude struggles, break it down further.
No iteration limit. Always set --max-iterations or equivalent. Without limits, loops burn tokens forever on stuck tasks.
Skipping /compact. Context fills up. Quality drops. /compact after every commit keeps sessions healthy.
Parallel agents on same files. If two agents edit the same file, you get merge conflicts. Split work by file boundaries or run sequentially.
Vague completion criteria. “Implement the feature” is too vague. “All tests pass and endpoint returns 200” is verifiable.
When to Use Autonomous Loops
| Use case | Good fit? |
|---|---|
| Batch migrations | Yes |
| Test coverage campaigns | Yes |
| Documentation updates | Yes |
| Complex feature development | Maybe (break into smaller tasks first) |
| Exploratory coding | No (needs human judgment) |
Autonomous loops work best for well-defined, repetitive tasks. Creative work still needs human steering.
Quick Start
- Create
.llm/todo.mdwith 5-10 small tasks - Add the
/todoand/compactcommands - Run:
/todothen/committhen/compact - Repeat until list is empty
- Graduate to
/todo-allorchestration when comfortable
Start with manual loops. Add automation after you understand the rhythm.
Next: Craig Motlin’s Autonomous Development Loop
Get updates
New guides, workflows, and AI patterns. No spam.
Thank you! You're on the list.