Appearance
Text size
Typeface
Line spacing
3
Chapter 3 · 4 min

The Ralph loop underneath everything

The oldest loop is a bash while-loop that feeds one prompt file to an agent forever. Fresh context, one task per iteration and tests as backpressure are why it works, and having no stop condition is why it needs the rest of this course.


The loop is almost embarrassingly small

Geoffrey Huntley published the technique on 2025-07-14 and called it Ralph. In its purest form it is a bash while-loop that pipes one prompt file into a coding agent, then does it again, forever.

while true; do
  cat PROMPT.md | <your coding agent command>
done

There is no orchestrator, no framework and no memory inside the process. When Nicholas Carlini built a C compiler with a team of Claudes, the base was the same: a while-true bash loop that restarts Claude Code with a prompt file.

Why study something so small? Because every command in the previous lesson is this loop with better parts. /goal adds a checker, /loop adds a clock. The shape underneath did not change.

Three choices that make it work

Fresh context every iteration. Each pass starts with an empty context window. The agent does not carry a long, cluttered conversation forward, so it does not degrade as the run goes on. Whatever must survive lives on disk.

One task per iteration. The agent reads a prioritized plan file, picks the single most important item and does only that. Huntley treats the plan file as disposable: when it drifts, regenerate it. Specs live as separate files that are read on every pass, and a short file records how to build and run the project.

Backpressure. Huntley's word for everything that pushes back on bad output: tests, builds, type checkers, analyzers. They reject wrong work so the loop can move fast and still stay correct. He runs only the tests for the touched unit, and the prompt forbids placeholder implementations, which closes the easy way out.

Read the prompt, the specs and the plan
Pick the single most important task
Implement it
Backpressure: tests, build, type check
Commit if green, then start over empty
Back to the top until the work is done
Check yourself

Why does the Ralph loop start every iteration with a fresh context window?

How it behaves in practice

Huntley's agent acts as a scheduler. Expensive work, such as searching the code or summarizing test output, goes to sub-agents, and he caps how many build and test sub-agents run in parallel. When Ralph does something wrong he does not argue with it. He adds a sign to the prompt, a short instruction that prevents that mistake next time. That is tuning, not conversation.

When a run goes off track he has two exits: regenerate the plan, or reset with git and restart. Git tags on green builds give him known good points to return to.

The known weakness

Ralph has no formal stop condition. It runs until the TODO list is empty or it goes off track, and a person is the one who notices. That is acceptable when you are sitting next to it. It is dangerous when nobody is.

Carlini's run shows what lives in that gap. New features broke old ones, so he added CI. All the agents got stuck on the same bug. One agent killed its own loop with pkill. These are the ordinary failures of a loop that has backpressure but no supervisor.

Loop engineering is the answer to that gap, and the answer is not a smarter model. It is the same shape plus the parts Ralph leaves out: a checker the agent cannot talk its way past, a stop condition written as evidence, a hard cap, and a rule for what to do when the same check keeps failing. The next three lessons add them one at a time.

Check yourself

What is the known weakness of the plain Ralph loop?

Read further