ralph-loopsRuns autonomous iterative AI loops for requirements, planning, or building phases using structured prompts and fresh context per iteration.
Install via ClawdBot CLI:
clawdbot install qlifebot-coder/ralph-loopsFirst 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 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 ./loop.sh plan (needs specs first) |
| "Start building system X" | Run ./loop.sh build (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 templates/requirements-interview.md protocol |
| Planning | Need specs first ā run planning loop with PROMPT_plan.md |
| Building | Need plan first ā run build loop with PROMPT_build.md |
| Generic | Create prompt file, run ralph-loop.mjs directly |
For simple iterative refinement (not full system builds):
/tmp/ralph-prompt-.md
node 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:
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 |
|------|---------|-------------|
| specs/*.md | Source of truth for requirements | Human + Phase 1 |
| PROMPT_plan.md | Instructions for planning mode | Copy from template |
| PROMPT_build.md | Instructions for building mode | Copy from template |
| AGENTS.md | Build/test/lint commands | Human + Ralph |
| IMPLEMENTATION_PLAN.md | Task list with priorities | Ralph (Phase 2) |
For Clawdbot systems, each Ralph project lives in :
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 specs/ | Phase 1: Requirements | Run requirements interview |
| specs/*.md but no IMPLEMENTATION_PLAN.md | Ready for Phase 2 | Run ./loop.sh plan |
| specs/*.md + IMPLEMENTATION_PLAN.md | Phase 2 or 3 | Review plan, run ./loop.sh build |
| 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.
999999. Important: Single sources of truth, no migrations.
9999999. Create git tags after successful builds.
99999999. Add logging if needed to debug.
999999999. 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 commits
# Copy 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 20
Summarize 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 | pip install -e . | pytest | ruff . |
| Go | go build ./... | go test ./... | golangci-lint run |
| Rust | cargo build | cargo test | cargo clippy |
| Ruby | bundle install | rspec | rubocop |
Also update path references in prompts (src/* ā your source directory).
Built by Johnathan & Q ā a human-AI dyad.
Generated Mar 1, 2026
An agency uses Ralph Loops to streamline client projects by first interviewing stakeholders to define clear requirements, then planning and building features iteratively. This ensures alignment and reduces rework, allowing developers to focus on implementation while the AI handles iterative refinement.
An e-commerce company employs Ralph Loops to refine its checkout process by iteratively testing and improving user flows. Starting with an interview to identify pain points, it plans and builds small, testable changes, leading to increased conversion rates through continuous, data-driven iteration.
A healthcare provider uses Ralph Loops to integrate disparate data systems by first interviewing staff to map out workflows, then planning and building secure, compliant interfaces. This phased approach ensures regulatory adherence and minimizes disruption to critical operations.
An edtech startup leverages Ralph Loops to develop adaptive learning modules by interviewing educators to define learning outcomes, then planning and building interactive content iteratively. This allows for rapid prototyping and refinement based on student feedback.
A marketing firm applies Ralph Loops to automate campaign workflows by interviewing clients to set goals, then planning and building targeted content and analytics tools. This iterative process optimizes engagement and ROI through continuous testing and adjustment.
Offer Ralph Loops as a managed service where clients pay a monthly fee for access to AI-driven iterative development cycles. This includes setup, monitoring, and periodic reviews, providing predictable revenue and long-term client relationships.
Provide consulting services to help organizations integrate Ralph Loops into their workflows, including training, customization, and support. Revenue comes from project-based fees and ongoing maintenance contracts.
License Ralph Loops technology to other companies, such as software vendors or agencies, who rebrand it as their own tool. This generates revenue through licensing fees and potential revenue-sharing agreements based on usage.
š¬ Integration Tip
Start by integrating Ralph Loops into non-critical projects to build familiarity, using the dashboard for real-time monitoring and adjusting phases based on team feedback to optimize workflow.
Captures learnings, errors, and corrections to enable continuous improvement. Use when: (1) A command or operation fails unexpectedly, (2) User corrects Clau...
Helps users discover and install agent skills when they ask questions like "how do I do X", "find a skill for X", "is there a skill that can...", or express interest in extending capabilities. This skill should be used when the user is looking for functionality that might exist as an installable skill.
Search and analyze your own session logs (older/parent conversations) using jq.
Typed knowledge graph for structured agent memory and composable skills. Use when creating/querying entities (Person, Project, Task, Event, Document), linking related objects, enforcing constraints, planning multi-step actions as graph transformations, or when skills need to share state. Trigger on "remember", "what do I know about", "link X to Y", "show dependencies", entity CRUD, or cross-skill data access.
Ultimate AI agent memory system for Cursor, Claude, ChatGPT & Copilot. WAL protocol + vector search + git-notes + cloud backup. Never lose context again. Vibe-coding ready.
Headless browser automation CLI optimized for AI agents with accessibility tree snapshots and ref-based element selection