Ralph Loop
The Ralph Loop (also “Ralph Wiggum Loop”) is a harness pattern for forcing an agent to continue long-horizon work past the point where it would otherwise stop. Named reference: https://ghuntley.com/loop/The Problem
Models tend toward early stopping. When a context window fills up or a task seems “done enough,” the agent exits. For long tasks that span multiple context windows, this means incomplete work without a mechanism to continue.The Pattern
The harness intercepts the agent’s exit signal via a hook. Instead of allowing the exit, it:- Clears or compacts the current context window
- Reinjects the original goal/prompt into the fresh context
- The agent reads current state from the filesystem (durable across iterations)
- Continues working toward completion
Why It Works
The filesystem is the memory. Clean context means no context rot, no accumulated noise, no degraded reasoning from a full window. The agent reconstructs where it is from durable artifacts, then continues. This is the same reasonautoresearch runs 5-minute bounded experiments in a loop rather than one long run — each iteration is self-contained and comparable; state persists in train.py modifications.
Canonical Loop Architecture (PRD → JSON → Single Task)
The pattern that emerged across independent implementations (Ralph, Anthropic’s demo, coding agents):In Practice (OpenAI Codex case study)
The full development loop is a Ralph Loop variant:- Engineer writes prompt → Codex opens a PR
- Codex reviews its own changes, requests agent reviews, responds to feedback
- Iterates until all agent reviewers pass
- Agent handles build failures, re-runs, and retries automatically
- Escalates to human only when judgment is required
- Merges
Completion Conditions
The harness must define what “done” means, or the loop never exits. Common approaches:- All automated tests pass
- No agent reviewer has outstanding objections
- A specific artifact is produced (PR opened, plan marked complete)
- Metric threshold reached (e.g.,
val_bpbimprovement for autoresearch)
Related Pages
- Agent Harness — harness component model; Ralph Loop as one primitive
- Autoresearch Karpathy — the 5-min experiment loop as a bounded Ralph Loop analog