Recursive Language Model (RLM)

An inference paradigm where an LLM processes arbitrarily long prompts by treating them as external environment variables in a REPL, writing code to explore, decompose, and recursively call itself on prompt slices. From MIT CSAIL (arXiv:2512.24601v3).

Core Idea

Traditional LLMs are limited by their context window. RLMs bypass this by offloading the prompt to a persistent Python REPL as a variable (prompt), then having the LLM write code to:

  1. Explore the prompt via slicing, search, string operations
  2. Decompose the task by calling sub_RLM(sub_prompt) on smaller pieces
  3. Aggregate results in REPL variables
  4. Finalize by assigning the answer to a Final variable

The LLM never sees the full prompt in its context — only constant-size metadata (length, prefix, REPL state). This enables effectively unbounded input length.

Prompt Pattern

The RLM is defined by a system prompt (see recursive-language-model-prompt) that establishes:

  • A REPL environment with prompt variable
  • A sub_RLM() function for recursive calls
  • A loop: LLM writes code → REPL executes → LLM sees truncated output + state → repeat
  • Termination when Final is set

This is a prompt-only architecture — no model modification needed. Works with any code-capable LLM (tested on GPT-5, Qwen3-Coder-480B-A35B).

Key Properties

PropertyDescription
Input scaling10M+ tokens demonstrated (vs. 272K context window)
Output scalingUnbounded — final answer built in REPL variables, not LLM output
Semantic horizonΩ(|P|) or Ω(|P|²) work possible via recursive loops
CostComparable to baselines; median run cheaper than base model
Recursion depthConfigurable (0-3 tested); depth=1 allows sub-LLM calls, depth>1 allows sub-RLM calls

Comparison to Baselines (from paper)

MethodContext HandlingRecursionLong-Context Performance
Base LLMDirect in contextNoneDegrades sharply
Compaction agentSummarize when fullNoneLoses fine-grained info
CodeAct (+ BM25)Direct + retrievalNoneLimited by context
CodeAct (+ sub-calls)DirectVerbalized sub-callsOutput-length limited
RLMREPL variableSymbolic (code)Scales to 10M+
Claude CodeFile offloadSub-agentsGood with offloading
OpenCodeFile offloadSub-agentsGood with offloading

Decomposition Patterns Observed

The paper analyzes RLM trajectories (Section 5, Appendix E):

  1. Probe → Decompose → Solve — LLM first explores prompt structure, then identifies sub-tasks, launches sub_RLM calls
  2. Model priors for search space narrowing — On BrowseComp+, LLM uses knowledge to programmatically filter documents before sub-calls
  3. Stitching for long outputs — On OOLONG-Pairs, LLM builds answer incrementally in REPL variables via sub-call loops
  4. Recovery from bad decomposition — RLMs can recognize failed decompositions and retry (Figure 4a shows in-context examples improve first decomposition)

Fine-Tuning RLMs

The paper demonstrates RLM-Qwen3-8B: Qwen3-8B fine-tuned on 1,000 distilled RLM(Qwen3-Coder-480B) trajectories on LongBenchPro tasks.

Results:

  • +28% median performance over base Qwen3-8B as RLM across 4 benchmarks
  • 3× faster inference due to better decisions, fewer mistakes
  • Length generalization: RL training on 64k context generalizes to 1M context (MRCRv2 benchmark)

This suggests the RLM prompt pattern is learnable — models can internalize the REPL interaction and recursion strategy.

Implications

For prompt engineering: This is a new class of architecture-defining prompts. The system prompt doesn’t just instruct — it defines the computational model (REPL + recursion). This blurs the line between prompting and programming.

For long-context applications: Any task requiring dense access to large corpora (code repos, legal docs, scientific literature, logs) can use this pattern without waiting for larger context windows.

For agent design: RLMs provide a unified framework for tool use, sub-agent delegation, and context management — all through code execution in a REPL. The sub_RLM primitive subsumes specialized sub-agent types.

Open Questions:

  • How to implement guardrails for unbounded recursion (cost explosion risk)?
  • Can the REPL be sandboxed for safe execution of untrusted prompts?
  • How does this interact with prompt caching (the prompt variable changes each turn)?
  • What’s the minimal model capability required? (Paper uses frontier models; 8B works after fine-tuning)

Sources

1

Footnotes

  1. raw/prompts/articles/recursive-language-models-arxiv-2512-24601v3.md ↩