---
id: "concept-ralph-loop"
type: "concept"
source_timestamps: ["§ Long Horizon Autonomous Execution"]
tags: ["orchestration", "long-horizon"]
related: ["concept-filesystem-primitive", "claim-long-horizon-compounds", "action-use-ralph-loops"]
definition: "A harness pattern that intercepts a model's exit attempt and reinjects the prompt in a clean context window to force task continuation."
---
# Ralph Loops

## The Pattern

The **Ralph Loop** is a specific harness orchestration pattern designed to combat **early stopping** and **incoherence** in long-horizon tasks.

Mechanically:

1. The model attempts to exit (emit a completion / stop token).
2. A harness **hook intercepts** the exit.
3. The harness **reinjects the original prompt into a completely clean context window**.
4. The agent reads its prior state and progress from the [[concept-filesystem-primitive|filesystem]].
5. The agent resumes work against its completion goal.

Each new iteration starts with a fresh context window, sidestepping [[concept-context-rot|context rot]] entirely, while durable state on disk preserves continuity.

## Why It Compounds

Ralph Loops are useless without durable state; durable state is under-utilized without continuation forcing. This compounding is the heart of [[claim-long-horizon-compounds]]. The action item [[action-use-ralph-loops]] codifies the recommendation.

## Counter-Perspective

Over-aggressive continuation can cause **runaway agents** — wasted compute, repeated work, low-value diffs. Production deployments typically combine Ralph-style loops with **budgets, stop criteria, and human approval gates**.

## Naming Note

The term *Ralph Loop* is largely LangChain / deepagents terminology. The general pattern (reset context + rehydrate from durable state + force continuation) is well known under other names in long-horizon agent literature.
