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
CommandWhat it does
/todoImplement the next incomplete task
/commitStage and commit with a message
/compactSummarize 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:

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:

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:

ApproachContext usageProblem
One agent, all tasksGrows with each taskQuality degrades after 5-6 tasks
SubagentsResets per taskStays 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:

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:

  1. Intercepts session exits with a stop hook
  2. Re-prompts Claude with the original task
  3. 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 caseGood fit?
Batch migrationsYes
Test coverage campaignsYes
Documentation updatesYes
Complex feature developmentMaybe (break into smaller tasks first)
Exploratory codingNo (needs human judgment)

Autonomous loops work best for well-defined, repetitive tasks. Creative work still needs human steering.

Quick Start

  1. Create .llm/todo.md with 5-10 small tasks
  2. Add the /todo and /compact commands
  3. Run: /todo then /commit then /compact
  4. Repeat until list is empty
  5. Graduate to /todo-all orchestration when comfortable

Start with manual loops. Add automation after you understand the rhythm.


Next: Craig Motlin’s Autonomous Development Loop

Topics: ai-agents automation workflow