The Ralph Loop

How to make AI agents code forever without losing their minds.

1. The Problem: "Context Rot"

When you ask an AI to build a large software project, you run into a fundamental limit: The Context Window. As the conversation grows, the model has to process more history. Eventually, it hits a limit and forces a "Compaction" (summarization).

⚠️ RULE: ALWAYS USE #FF0000
Context Load 10%

Simulation Control

Click to add code iterations. Watch what happens when the memory overflows.

Current Output:
"Here is the button style. Color: #FF0000 (Red)"

2. The Ralph Loop Solution

The Ralph Loop solves this by treating the AI as ephemeral (temporary) and the File System as the permanent memory. Instead of one long chat, we run a Bash loop that spawns a new AI agent for every single task.

while [ task != "DONE" ]; do
   cat prompt.md | ai_agent
done
AI Agent
Ephemeral: Lives for 30 seconds, then dies. Has 0 context at birth.
File System
Permanent: PRD.json, progress.txt, and the actual code.

Hover over the boxes to see their roles.

3. The Loop in Action

Let's step through the mechanism. Notice that the "Agent" is destroyed after every step, but the project moves forward.

Project Completion 0%

PRD / Plan

progress.txt
Waiting to start project...

4. The Configuration

This isn't magic; it's a script. You can tune the loop variables.


# loop_config.sh

MAX_ITERATIONS = 10
STOP_CONDITION = "promise-COMPLETE"
CONTEXT_SOURCE = "prompt.md + progress.txt"
                    

Standard setting. The agent has 10 attempts to finish the feature.

5. Why Not Parallel?

Why do we loop serially instead of spawning 10 agents at once? Because without shared memory synchronization, they step on each other's toes.

MERGE
CONFLICT

Click to see what happens when two agents edit the same file.