Ralph Loops
04/23/26 · /ai/agents · ~3 min
Ralph Loops are the Agentic Paradigm of Tail Recursion
Quick Refresher: Tail recursion has 3 parts.
- Recursive step (action): function applied on every call
- Base case (target): terminating condition
- Accumulator (state): stores and carries state forward
def Ralph(action, state, target):
if state == target:
return state
return Ralph(action, action(state), target)
A Ralph loop has the same shape:
- Recursive step (action): PROMPT.md applied on every call
- Base case (target): definition of ‘done’. An exit criteria: sample of target output, comparison statement, screenshot, predicate, etc.
- Accumulator (state): the artifact the agent acts on.
Memory = Context Window
If N is the number of recursive steps:
- Tail Recursion reduces memory from O(N) to O(1)
- Ralph reduces context window from O(N) to O(1)
Tail recursion is necessary where naive recursion would bloat the memory stack.
Tail recursion doesn’t leave a stack frame; it iterates indefinitely in O(1) stack .
Ralph Loop is necessary where naive looping would bloat the context window.
Ralph Loop doesn’t backfill the context; it iterates indefinitely in O(1) context.
Then what is naive agentic recursion?
It’s looping in session, which bloats your context.
That is why any in-session forms of “Ralph Loops” (skills like /auto-loop) are categorically different than the canonical Ralph Loop.
A proper Ralph loop must loop outside the harness itself.