The RALPH Loop

James Phoenix
James Phoenix

Fresh context each iteration. Memory lives in git, docs, and task files—not in the conversation.


Core Concept

The RALPH Loop is a bash script pattern that runs an AI coding agent repeatedly until all tasks are complete, spawning fresh agent instances each iteration. This addresses a fundamental LLM limitation: context degradation in lengthy conversations.

“As conversations extend, attention becomes diluted, leading to degraded performance, hallucinations, and poor decision-making.”


The Iteration Pattern

┌─────────────────────────────────────────────────────────────┐
│  1. Select highest-priority incomplete task                 │
│  2. Implement the single task                               │
│  3. Run quality checks (type-checking, tests)               │
│  4. Commit if checks pass                                   │
│  5. Update task status                                      │
│  6. Document learnings                                      │
│  7. SPAWN NEW AGENT INSTANCE → Repeat                       │
└─────────────────────────────────────────────────────────────┘

Critical innovation: Each iteration starts with a clean context window. Memory persists only through:

  • Git history
  • Progress/task files
  • AGENTS.md / CLAUDE.md documentation

The Four-Phase Cycle

Phase Effort Focus
Plan ~40% Research approaches, analyze codebase, synthesize strategy
Work ~20% Write code and tests per established plan
Review ~40% Examine outputs, extract lessons
Compound Critical Document patterns, gotchas, conventions for future iterations

The Compound phase is what turns individual iterations into accumulated advantage.

The Compound Phase: Four Dimensions

Source: Agentic Patterns

Document findings across four dimensions after each feature:

Udemy Bestseller

Learn Prompt Engineering

My O'Reilly book adapted for hands-on learning. Build production-ready prompts with practical exercises.

4.5/5 rating
306,000+ learners
View Course
Dimension Questions to Answer
Plan effectiveness What succeeded? What required adjustment?
Testing discoveries What issues were missed during development?
Common errors What patterns of agent mistakes emerged?
Reusable patterns What best practices are worth formalizing?

Embed learnings into:

  • System prompts (CLAUDE.md)
  • Slash commands
  • Specialized subagents
  • Automated hooks
  • Test suites

“Each feature becomes curriculum for the next.”

Trade-offs:

  • Benefits: Accelerated productivity, knowledge preservation, improved onboarding, reduced repetition
  • Costs: Discipline demands, maintenance requirements, prompt complexity growth

Why Fresh Context Matters

LLMs experience “context rot”:

  • Attention dilutes over long conversations
  • Decisions degrade
  • Hallucinations increase
  • Performance drops

The RALPH Loop sidesteps this by treating each iteration as autonomous.

This mirrors effective human developer practices:

  • Focused, discrete sessions
  • Not marathon coding where fatigue compounds errors
  • Unlike humans, AI can cycle through infinite fresh contexts without fatigue

AGENTS.md: Knowledge Accumulation

Repository-based files (AGENTS.md or CLAUDE.md) store codebase-specific knowledge:

  • Conventions
  • Patterns
  • Gotchas
  • Best practices

After each loop iteration, agents update these files with learnings.

The flywheel effect:

Development → Documented Knowledge → Faster Future Work → More Development

Since AI coding tools automatically read these files, future iterations benefit immediately from previously discovered insights.


Essential Feedback Loops

The RALPH Loop requires robust verification mechanisms:

Mechanism Purpose
Type-checking Prevents type errors from propagating
Automated tests Verify specifications, prevent regression
Continuous integration Maintains deployable codebase state
Browser verification Confirms frontend changes (Playwright)

Without feedback loops, the RALPH Loop becomes a chaos engine—rapidly producing broken code. With them, it becomes production-quality automation.


Task Sizing Discipline

Success requires breaking work into single-context-window units.

Well-sized tasks:

  • Database migration
  • Single UI component
  • Server action update
  • API endpoint implementation

Oversized tasks (need decomposition):

  • “Build entire dashboard”
  • “Implement auth system”
  • “Refactor the codebase”

“Each task should have a clear definition of done and verifiable acceptance criteria.”


The Economic Shift

The RALPH Loop shifts software economics:

Old Model New Model
Expensive human typing Cheap AI inference cycles
Engineer writes code Engineer orchestrates systems
Code production Architecture + feedback loops + quality gates

This distinguishes software development (code production) from software engineering (system design).


Gas Town: The Multi-Agent Future

The logical extension: multiple agents working simultaneously on granularly-decomposed tasks.

┌─────────────────────────────────────────────────────────────┐
│                    Human Engineer                            │
│              (Architecture + Strategy)                       │
└─────────────────────┬───────────────────────────────────────┘
                      │
        ┌─────────────┼─────────────┐
        ▼             ▼             ▼
   ┌─────────┐   ┌─────────┐   ┌─────────┐
   │ Agent 1 │   │ Agent 2 │   │ Agent 3 │
   │ Task A  │   │ Task B  │   │ Task C  │
   └─────────┘   └─────────┘   └─────────┘
        │             │             │
        └─────────────┴─────────────┘
                      │
               Git + AGENTS.md
              (Shared Memory)

Steve Yegge calls this “Gas Town”—fleets of agents in fresh contexts, with human engineers focusing on architecture and strategy.


Implementation: Minimal RALPH Script

#!/bin/bash
# ralph.sh - Run agent loop until all tasks complete

while true; do
  # Check for incomplete tasks
  TASK=$(cat tasks.md | grep "- \[ \]" | head -1)

  if [ -z "$TASK" ]; then
    echo "All tasks complete"
    exit 0
  fi

  # Fresh agent instance per task
  claude --print "
    Read AGENTS.md for context.
    Read tasks.md and work on the first incomplete task.
    Run tests after implementation.
    If tests pass, mark task complete in tasks.md.
    Update AGENTS.md with any learnings.
  "

  # Loop continues with fresh context
done

Current Reality

Companies like Every already practice compound engineering at scale:

  • Single developers maintaining multiple products
  • Estimated to perform work of 5 developers from years prior
  • Not theoretical—production-stage development happening now

Key Insight

The RALPH Loop treats context degradation as a feature, not a bug. By forcing fresh starts, it ensures each iteration has maximum cognitive clarity.


Related


External References

Topics
Ai Coding AgentsContext RefreshingGit IntegrationLlm Iteration PatternsTask Automation

More Insights

Cover Image for Thought Leaders

Thought Leaders

People to follow for compound engineering, context engineering, and AI agent development.

James Phoenix
James Phoenix
Cover Image for Systems Thinking & Observability

Systems Thinking & Observability

Software should be treated as a measurable dynamical system, not as a collection of features.

James Phoenix
James Phoenix