Pattern

ralph
loop

Fresh-Context Determinism. The reliability pattern where each loop run is a clean chance to finish a real unit of work.

"Why don't you just put it in a loop?"
The Ralph Loop succeeds because it programs the model environment rather than trusting one context-heavy monologue. Each iteration is a separate chance to advance.
Interactive Demonstration
STARTFINISH
STARTFINISH
STARTFINISH
PLANNINGWORKVALIDATION

Features

Fresh Context, Clear Exit

Each loop restarts with a deterministic input and exits only when criteria are met.

Program the Loop

Reliability comes from loop-level state, prompts, and gates—not from model charisma.

Planning / Work / Validation

A clean split that makes execution predictable: plan the work, run the work, validate before advancing.

Ralph-Style Context Hygiene

Continuity is preserved through files, plans, and logs rather than ever-growing chat memory.

The Ralph Loop is a control pattern, not a prompt trick. The core move is to let one loop complete a bounded unit of work, then intentionally reset context for the next loop.

Every loop begins with a fresh context window, so the model can retry with a clean view instead of carrying fragile conversational drift. Continuity is not kept by memory; it is kept by artifacts: plan files, state files, run logs, and explicit gates.

For us, this becomes three linked loops: Planning, Work, and Validation. Planning defines what "done" means, Work attempts the unit, and Validation decides whether it can move on or needs another disciplined run.

"Ralph Loop succeeds because it programs the model environment rather than trusting one context-heavy monologue. Each iteration is a separate chance to advance, and each transition is controlled by deterministic handoff conditions."

Origin & Attribution

Geoffrey Huntley

Creator of the Ralph Loop Pattern

"Why don't you just put it in a loop?"
Geoffrey's son, Christmas 2024

The Philosophy

The Ralph Loop isn't just a code pattern — it's a way of thinking about AI-assisted work.

01

Fresh Context Is a New Chance

A loop reset is not a failure mode; it's the mechanism. Treat each iteration as a fresh context window with a clear objective, and you get predictable recovery points instead of runaway drift.

02

Program the Loop, Not the Luck

Most unreliability lives in prompt glue. The loop should encode retry policy, handoff rules, and termination checks so each run behaves like a deterministic operation.

03

Context Is Ephemeral, Continuity Is Persistent

Keep progress in files, specs, and logs. The model gets a clean run, the team gets a durable state.

04

Table Stakes in 2026

Most serious CLI workflows now implement this pattern in some form: an explicit plan, bounded execution, and a strong validation gate before moving forward.

Kingly's Approach

We split our Ralph work into three explicit loops and tune each as a configurable stage, not a monolithic prompt recipe.

Planning Loop

Kingly starts each cycle by validating intent, constraints, and acceptance criteria. If the plan is fuzzy, the loop does not enter execution.

Work Loop

Execution runs on a fixed contract: specific files to edit, expected outputs, and hard limits. If execution fails, the system collects signal and retries with a reset context.

Validation Loop

Completion is decided by deterministic gates, then context handoff artifacts are written so the next pass starts clean but informed.

Program the Loop

You can program context management, retry policy, and gate behavior directly into loop controls. That is the leverage point: coding around a loop instead of coding to a personality.

The Future

This pattern keeps getting stronger as tooling learns to reason about loop state, not only loop output.

Composable Loop Blocks

Treat planning, execution, and validation as reusable primitives across codebases and agents.

Adaptive Gate Quality

Track gate outcomes and auto-tune threshold policies when repeated failure modes repeat.

Intent-Driven Handoff

Let each loop handoff include explicit next intent, context budget notes, and completion evidence.

Better Programmatic Oversight

Build loop operators that are easier to inspect, version, and audit than ad-hoc chat prompting.

Tech Stack

Fresh Context Windows
Loop-Level Determinism
Artifact-Driven Continuity
Programmable Automation
Validation Gates

What This Is Used For

01

Context-window safety: prevent drift by forcing structured resets before the model enters unstable states

02

Reliable execution in code tasks: run short, bounded loops with explicit completion checks

03

Plan-first delivery: generate work only after planning and validation gates are declared

04

Context splicing: use serialized artifacts to hand off between loops without losing momentum

Install

NPM Package Coming Soon

Sign up for the newsletter to get notified when it's released.

Also from Labs

Stay Updated

Get notified of new labs, experiments, and updates.

Share