Ralph Loop Deep Dive — The AI Coding Loop That Won't Stop Until Your PRD Is Done
Ralph is not a framework. It's not a library. It's not even a proper CLI tool — at least not in its original form. Ralph is a Bash loop. Specifically, it's the idea that you can wrap an AI coding agent in a while loop, give it a task list, and let it run until everything is done.
This idea, born from a community pattern by Geoffrey Huntley, has spawned an entire ecosystem: five major implementations totaling over 30,000 GitHub stars, each layering more sophistication onto the original loop. We spent a full research session mapping this ecosystem, reading source code, and evaluating what belongs in Hermes's autonomous agent toolkit.
The Original Pattern
The core insight is almost insultingly simple:
while [ not done ]; do
read prd.json # What's left to do?
read progress.txt # What did we learn last time?
pick next story # Which task has highest priority?
launch fresh AI agent # Clean context, no baggage
implement one story # Small enough to fit in context window
run quality checks # Typecheck, test, lint
update prd.json # Mark story as done
append progress.txt # Record lessons learned
done
Three design decisions make this work where naive loops fail:
Fresh context every iteration. Each loop cycle launches a brand-new AI agent instance with zero conversation history. Memory is carried between iterations through the file system (prd.json for status, progress.txt for lessons), not through growing context windows. This prevents the entropy death that kills long-running agent sessions — no accumulated misunderstandings, no context pollution, no hallucination cascade.
Small tasks. Each PRD item must be completable in a single context window. If one user story takes 2,000 tokens of conversation, it should be no more than 2,000 tokens of work. This constraint forces good decomposition and prevents the "almost done but context is full" failure mode.
Rigid quality gates. Every iteration ends with mandatory type checking and test execution. If a story introduces a bug, the next iteration starts by fixing it. Bugs don't survive more than one loop cycle.
The Five Implementations
The ecosystem has evolved through five distinct layers of sophistication:
L1: snarktank/ralph (18.5k stars) — The Original
A 120-line Bash script. Supports two backends: Anthropic's Claude Code and Amp. Exits when the agent outputs a <promise>COMPLETE</promise> tag. No cost control, no session management, no parallel execution. It's the minimum viable loop — and it works.
The README explicitly warns: "You will spend money running this." In one documented case, a project ran 10 iterations and cost $45 in API fees. The lack of cost controls is the most cited concern in GitHub issues.
L2: frankbria/ralph-claude-code (9k stars) — The Engineered Version
This is where Ralph becomes production-grade. The headline feature is dual-gate exit detection, arguably the most important contribution to the entire ecosystem:
Exit requires BOTH:
completion_indicators >= 2 AND EXIT_SIGNAL == true
Either alone is insufficient. The agent must both claim completion and demonstrate it — if it says "done" but the indicators don't back it up, the loop continues. This prevents the most common autonomous agent failure mode: premature completion claims.
It also adds a circuit breaker (three consecutive no-progress iterations → 30-minute cooldown), per-hour API call limits, session support with 24-hour expiry, and an impressive 566-unit test suite with 100% pass rate. The .ralph/ directory convention keeps artifacts out of the project root.
L3: michaelshimeles/ralphy (2.8k stars) — The Multi-Engine Universal Remote
Ralphy supports eight AI engines: Claude Code, OpenCode, Codex, Cursor, Qwen, Droid, Copilot, and Gemini. It also introduces parallel execution via git worktrees — each agent gets its own branch and filesystem, then results merge back. The --sandbox flag uses symlinks to isolate dependencies without duplicating node_modules.
The PRD parser is format-agnostic: Markdown checkboxes, YAML task lists, JSON stories, GitHub Issues — all feed into the same execution loop.
L4: AnandChowdhary/continuous-claude (1.3k stars) — The PR-Driven Approach
Continuous-claude integrates the loop directly into the GitHub PR workflow. Each iteration creates a PR, waits for CI checks, and merges on green. It adds three-dimensional cost control (max runs, max cost in dollars, max duration) — the first hit stops the loop. The SHARED_TASK_NOTES.md file serves as a relay baton between iterations.
L5: vercel-labs/ralph-loop-agent (777 stars) — The SDK Approach
This is the most architecturally elegant implementation. Instead of shell scripts, it wraps the loop in a TypeScript class with composable stop conditions:
stopWhen: [
iterationCountIs(50),
tokenCountIs(100_000),
costIs(5.00)
]
Any condition triggers exit. The built-in model pricing table covers Anthropic, OpenAI, Google, xAI, and DeepSeek models. The RalphContextManager handles automatic summarization and token budget enforcement. The verifyCompletion callback is fully user-defined — you decide what "done" means.
What Ralph Gets Right (and Wrong)
Right: The fresh-context-per-iteration model is the design pattern that makes everything else work. It's counterintuitive — we're conditioned to think agents need persistent memory — but for task execution, context isolation is more reliable than context accumulation.
Right: Small tasks enforced by the loop are a forcing function for good architecture. You can't write a 500-line PRD item because it literally won't fit in the context window. The loop forces decomposition.
Wrong: Cost control is an afterthought in most implementations. The original snarktank/ralph has none at all. The ecosystem is gradually retrofitting it (frankbria's circuit breaker, vercel-labs' costIs), but it should have been a first-class concern from day one.
Wrong: The shell-script foundation limits composability. Each implementation reimplements the same loop logic in a slightly different way because there's no shared library. vercel-labs' TypeScript SDK is a step toward fixing this.
Mapping Ralph to Hermes
Hermes already has the building blocks — the question is how to assemble them:
| Ralph Concept | Hermes Equivalent | Gap |
|---|---|---|
| PRD user stories | Kanban cards with status tracking | Hermes kanban is manual; Ralph automates status updates |
| Fresh context per iteration | delegate_task with goal + context |
Hermes tasks share parent context; Ralph iterations are truly fresh |
| Continuous loop | cronjob with short interval |
Cron is time-based; Ralph is completion-based |
| Quality gates | No built-in equivalent | Needs a post_task_validation hook |
| Cost control | No built-in equivalent | vercel-labs' StopCondition pattern is directly portable |
| Progress tracking | Session search for past outcomes | Ralph's progress.txt is simpler but more reliable for loop context |
The most valuable borrowings for Hermes:
-
Dual-gate exit detection (from frankbria). Add a
completion_indicatorscheck after everydelegate_taskcall. The agent claims done? Fine — now prove it. Run tests, check file changes, verify the acceptance criteria. -
Composable stop conditions (from vercel-labs). A
StopConditionprotocol that can be chained:iterationCountIs(10) | tokenCountIs(50k) | costIs(3.00). Any trigger stops the loop. This fits naturally into Hermes's cronjob infrastructure. -
Autonomous kanban driver. A cron job that reads the kanban board, picks the highest-priority pending item, spawns a
delegate_taskfor it, verifies completion, and advances the card. This is the Ralph loop mapped to Hermes primitives.
What Not to Borrow
The shell script wrapping pattern is wrong for Hermes. Hermes agents have native execution capability — they don't need to shell out to Claude Code or Codex CLI. The loop logic should be implemented as Hermes infrastructure (a new tool or a cronjob pipeline), not as external shell scripts.
Multi-engine support is also unnecessary. Hermes already abstracts model selection; the loop doesn't need to know whether the agent is running on Claude, GPT, or DeepSeek.
Verdict
Ralph is worth serious adoption — not as a direct import, but as a design pattern. The fresh-context loop, dual-gate exit detection, and composable stop conditions should be built into Hermes as first-class autonomous execution primitives.
The path forward: implement an autonomous loop driver as a cronjob + delegate_task pipeline, add completion verification hooks, and port the stop condition pattern. This gives us Ralph's core value — continuous, self-verifying task execution — without Ralph's shell-script baggage.
Start small. One kanban card, one iteration, one verification check. The loop scales naturally.