Ralph Loops
Runs autonomous iterative AI loops for requirements, planning, or building phases using structured prompts and fresh context per iteration.
Runs autonomous iterative AI loops for requirements, planning, or building phases using structured prompts and fresh context per iteration.
Real data. Real impact.
Emerging
Developers
Per week
Open source
Skills give you superpowers. Install in 30 seconds.
First time? Read SETUP.md first to install dependencies and verify your setup.
Autonomous AI agent loops for iterative development. Based on Geoffrey Huntley's Ralph Wiggum technique, as documented by Clayton Farr.
Script:
skills/ralph-loops/scripts/ralph-loop.mjs
Dashboard: skills/ralph-loops/dashboard/ (run with node server.mjs)
Templates: skills/ralph-loops/templates/
Archive: ~/clawd/logs/ralph-archive/
Claude Code 2.1.29 has a critical bug that spawns orphaned sub-agents consuming 99% CPU. Iterations fail with "exit code null" on first run.
Fix: Downgrade to 2.1.25:
npm install -g @anthropic-ai/claude-code@2.1.25
Verify:
claude --version # Should show 2.1.25
This was discovered 2026-02-01. Check if newer versions fix the issue before upgrading.
When running a Ralph loop, don't monitor it synchronously. The loop runs as a separate Claude CLI process — you can keep chatting.
❌ Wrong (blocks conversation):
Start loop → sleep 60 → poll → sleep 60 → poll → ... (6 minutes of silence)
✅ Right (stays responsive):
Start loop → "It's running, I'll check periodically" → keep chatting → check on heartbeats
How to monitor without blocking:
node ralph-loop.mjs ... (runs in background)process poll <sessionId> when asked or during heartbeatsThe loop is autonomous — that's the whole point. Don't babysit it at the cost of ignoring your human.
When human says:
| Phrase | Action |
|---|---|
| "Interview me about system X" | Start Phase 1 requirements interview |
| "Start planning system X" | Run (needs specs first) |
| "Start building system X" | Run (needs plan first) |
| "Ralph loop over X" | ASK which phase (see below) |
Don't assume which phase. Ask:
"Which type of Ralph loop are we doing?
1️⃣ Interview — I'll ask you questions to build specs (Phase 1) 2️⃣ Planning — I'll iterate on an implementation plan (Phase 2)
3️⃣ Building — I'll implement from a plan, one task per iteration (Phase 3) 4️⃣ Generic — Simple iterative refinement on a single topic"
Then proceed based on their answer:
| Choice | Action |
|---|---|
| Interview | Use protocol |
| Planning | Need specs first → run planning loop with |
| Building | Need plan first → run build loop with |
| Generic | Create prompt file, run directly |
For simple iterative refinement (not full system builds):
/tmp/ralph-prompt-<task>.mdnode skills/ralph-loops/scripts/ralph-loop.mjs \ --prompt "/tmp/ralph-prompt-<task>.md" \ --model opus \ --max 10 \ --done "RALPH_DONE"
"Human roles shift from 'telling the agent what to do' to 'engineering conditions where good outcomes emerge naturally through iteration." — Clayton Farr
Three principles drive everything:
┌─────────────────────────────────────────────────────────────────────┐ │ Phase 1: REQUIREMENTS │ │ Human + LLM conversation → JTBD → Topics → specs/*.md │ ├─────────────────────────────────────────────────────────────────────┤ │ Phase 2: PLANNING │ │ Gap analysis (specs vs code) → IMPLEMENTATION_PLAN.md │ ├─────────────────────────────────────────────────────────────────────┤ │ Phase 3: BUILDING │ │ One task per iteration → fresh context → backpressure → commit │ └─────────────────────────────────────────────────────────────────────┘
Goal: Understand what to build BEFORE building it.
This is the most important phase. Use structured conversation to:
Identify Jobs to Be Done (JTBD)
Break JTBD into Topics of Concern
Create Specs for Each Topic
specs/Template:
templates/requirements-interview.md
Goal: Create a prioritized task list without implementing anything.
Uses
PROMPT_plan.md in the loop:
IMPLEMENTATION_PLAN.md with prioritized tasksUsually completes in 1-2 iterations.
Goal: Implement tasks one at a time with fresh context.
Uses
PROMPT_build.md in the loop:
IMPLEMENTATION_PLAN.mdKey insight: One task per iteration keeps context lean. The agent stays in the "smart zone" instead of accumulating cruft.
Why fresh context matters:
project/ ├── loop.sh # Ralph loop script ├── PROMPT_plan.md # Planning mode instructions ├── PROMPT_build.md # Building mode instructions ├── AGENTS.md # Operational guide (~60 lines max) ├── IMPLEMENTATION_PLAN.md # Prioritized task list (generated) └── specs/ # Requirement specs ├── topic-a.md ├── topic-b.md └── ...
| File | Purpose | Who Creates |
|---|---|---|
| Source of truth for requirements | Human + Phase 1 |
| Instructions for planning mode | Copy from template |
| Instructions for building mode | Copy from template |
| Build/test/lint commands | Human + Ralph |
| Task list with priorities | Ralph (Phase 2) |
For Clawdbot systems, each Ralph project lives in
<workspace>/systems/<name>/:
systems/ ├── health-tracker/ # Example system │ ├── specs/ │ │ ├── daily-tracking.md │ │ └── test-scheduling.md │ ├── PROMPT_plan.md │ ├── PROMPT_build.md │ ├── AGENTS.md │ ├── IMPLEMENTATION_PLAN.md # ← exists = past Phase 1 │ └── src/ └── activity-planner/ ├── specs/ # ← empty = still in Phase 1 └── ...
Detect current phase by checking what files exist:
| What Exists | Current Phase | Next Action |
|---|---|---|
Nothing / empty | Phase 1: Requirements | Run requirements interview |
but no | Ready for Phase 2 | Run |
+ | Phase 2 or 3 | Review plan, run |
| Plan shows all tasks complete | Done | Archive or iterate |
Quick check:
# What phase are we in? [ -z "$(ls specs/ 2>/dev/null)" ] && echo "Phase 1: Need specs" && exit [ ! -f IMPLEMENTATION_PLAN.md ] && echo "Phase 2: Need plan" && exit echo "Phase 3: Ready to build (or done)"
The hierarchy matters:
JTBD (Job to Be Done) └── Topic of Concern (1 per spec file) └── Tasks (many per topic, in IMPLEMENTATION_PLAN.md)
Example:
specs/image-collection.mdspecs/color-extraction.mdspecs/layout-system.mdspecs/sharing.mdCan you describe the topic in one sentence without "and"?
If you need "and" or "also", it's probably multiple topics. Split it.
When to split:
Example split:
❌ "User management handles registration, authentication, profiles, and permissions"✅ Split into:
"Registration creates new user accounts from email/password"
"Authentication verifies user identity via login flow"
"Profiles let users view and edit their information"
"Permissions control what actions users can perform"
Counter-example (don't split):
✅ Keep together: "Color extraction analyzes images and returns dominant color palettes"Why: "analyzes" and "returns" are steps in one operation, not separate concerns.
Autonomous loops converge when wrong outputs get rejected. Three layers:
Tests, type-checking, linting, build validation. Deterministic.
# In AGENTS.md ## Validation - Tests: `npm test` - Typecheck: `npm run typecheck` - Lint: `npm run lint`
Existing code patterns guide the agent. It discovers conventions through exploration.
For subjective criteria (tone, UX, aesthetics), use another LLM call with binary pass/fail.
Start with hard gates. Add LLM-as-judge for subjective criteria only after mechanical backpressure works.
Geoffrey's prompts follow a numbered pattern:
| Section | Purpose |
|---|---|
| 0a-0d | Orient: Study specs, source, current plan |
| 1-4 | Main instructions: What to do this iteration |
| 999+ | Guardrails: Invariants (higher number = more critical) |
Guardrails use escalating numbers (99999, 999999, 9999999...) to signal priority:
99999. Important: Capture the why in documentation.
Important: Single sources of truth, no migrations.
Create git tags after successful builds.
Add logging if needed to debug.
Keep IMPLEMENTATION_PLAN.md current.
Why this works:
The "Important:" prefix is deliberate — it triggers Claude's attention.
Use Geoffrey's specific phrasing — it matters:
mkdir -p myproject/specs cd myproject git init # Ralph expects git for commitsCopy templates
cp .//templates/PROMPT_plan.md . cp .//templates/PROMPT_build.md . cp .//templates/AGENTS.md . cp .//templates/loop.sh . chmod +x loop.sh
PROMPT_plan.md — Replace
[PROJECT_GOAL] with your actual goal:
# Before: ULTIMATE GOAL: We want to achieve [PROJECT_GOAL].After:
ULTIMATE GOAL: We want to achieve a fully functional mood board app with image upload and color extraction.
PROMPT_build.md — Adjust source paths if not using
src/:
# Before: 0c. For reference, the application source code is in `src/*`.After:
0c. For reference, the application source code is in.lib/*
AGENTS.md — Update build/test/lint commands for your stack.
This phase happens WITH the human. Use the interview template:
cat .//templates/requirements-interview.md
The workflow:
specs/topic-name.mdExample output:
specs/ ├── image-collection.md ├── color-extraction.md ├── layout-system.md └── sharing.md
./loop.sh plan
Wait for
IMPLEMENTATION_PLAN.md to be generated (usually 1-2 iterations). Review it — this is your task list.
./loop.sh build 20 # Max 20 iterations
Watch it work. Add backpressure (tests, lints) as patterns emerge. Check commits for progress.
./loop.sh # Build mode, unlimited ./loop.sh 20 # Build mode, max 20 iterations ./loop.sh plan # Plan mode, unlimited ./loop.sh plan 5 # Plan mode, max 5 iterations
Or use the Node.js wrapper for more control:
node skills/ralph-loops/scripts/ralph-loop.mjs \ --prompt "./PROMPT_build.md" \ --model opus \ --max 20 \ --done "RALPH_DONE"
Plans drift. Regenerate when:
Just switch back to planning mode:
./loop.sh plan
Regeneration cost is one Planning loop. Cheap compared to Ralph going in circles.
Ralph requires
--dangerously-skip-permissions to run autonomously. This bypasses Claude's permission system entirely.
Philosophy: "It's not if it gets popped, it's when. And what is the blast radius?"
Protections:
git reset --hard reverts uncommitted changes| Task Type | Model | Iterations | Est. Cost |
|---|---|---|---|
| Generate plan | Opus | 1-2 | $0.50-1.00 |
| Implement simple feature | Opus | 3-5 | $1.00-2.00 |
| Implement complex feature | Opus | 10-20 | $3.00-8.00 |
| Full project buildout | Opus | 50+ | $15-50+ |
Tip: Use Sonnet for simpler tasks where plan is clear. Use Opus for planning and complex reasoning.
From Geoffrey Huntley:
For long loops, spawn as sub-agent so main session stays responsive:
sessions_spawn({ task: `cd /path/to/project && ./loop.sh build 20Summarize what was implemented when done.`, label: "ralph-build", model: "opus" })
Check progress:
sessions_list({ kinds: ["spawn"] }) sessions_history({ label: "ralph-build", limit: 5 })
./loop.sh planThe loop script expects git for commits and pushes. For projects without version control:
Option 1: Initialize git anyway (recommended)
git init git add -A git commit -m "Initial commit before Ralph"
Option 2: Modify the prompts
cp -r src/ backups/iteration-$ITERATION/ to loop.shOption 3: Use tarball snapshots
# Add to loop.sh before each iteration: tar -czf "snapshots/pre-iteration-$ITERATION.tar.gz" src/
For codebases with 100K+ lines:
The methodology works with any Claude interface:
Claude API directly:
# Replace loop.sh with API calls using curl or a script curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "content-type: application/json" \ -d '{"model": "claude-sonnet-4-20250514", "max_tokens": 8192, "messages": [...]}'
Alternative agents:
aider --opus --auto-commitsThe key principles (one task per iteration, fresh context, backpressure) apply regardless of tooling.
Adapt AGENTS.md for your stack:
| Stack | Build | Test | Lint |
|---|---|---|---|
| Python | | | |
| Go | | | |
| Rust | | | |
| Ruby | | | |
Also update path references in prompts (
src/* → your source directory).
Built by Johnathan & Q — a human-AI dyad.
No automatic installation available. Please visit the source repository for installation instructions.
View Installation Instructions1,500+ AI skills, agents & workflows. Install in 30 seconds. Part of the Torly.ai family.
© 2026 Torly.ai. All rights reserved.