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:
- Explore the prompt via slicing, search, string operations
- Decompose the task by calling
sub_RLM(sub_prompt)on smaller pieces - Aggregate results in REPL variables
- Finalize by assigning the answer to a
Finalvariable
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
promptvariable - A
sub_RLM()function for recursive calls - A loop: LLM writes code → REPL executes → LLM sees truncated output + state → repeat
- Termination when
Finalis 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
| Property | Description |
|---|---|
| Input scaling | 10M+ tokens demonstrated (vs. 272K context window) |
| Output scaling | Unbounded — final answer built in REPL variables, not LLM output |
| Semantic horizon | Ω(|P|) or Ω(|P|²) work possible via recursive loops |
| Cost | Comparable to baselines; median run cheaper than base model |
| Recursion depth | Configurable (0-3 tested); depth=1 allows sub-LLM calls, depth>1 allows sub-RLM calls |
Comparison to Baselines (from paper)
| Method | Context Handling | Recursion | Long-Context Performance |
|---|---|---|---|
| Base LLM | Direct in context | None | Degrades sharply |
| Compaction agent | Summarize when full | None | Loses fine-grained info |
| CodeAct (+ BM25) | Direct + retrieval | None | Limited by context |
| CodeAct (+ sub-calls) | Direct | Verbalized sub-calls | Output-length limited |
| RLM | REPL variable | Symbolic (code) | Scales to 10M+ |
| Claude Code | File offload | Sub-agents | Good with offloading |
| OpenCode | File offload | Sub-agents | Good with offloading |
Decomposition Patterns Observed
The paper analyzes RLM trajectories (Section 5, Appendix E):
- Probe → Decompose → Solve — LLM first explores prompt structure, then identifies sub-tasks, launches
sub_RLMcalls - Model priors for search space narrowing — On BrowseComp+, LLM uses knowledge to programmatically filter documents before sub-calls
- Stitching for long outputs — On OOLONG-Pairs, LLM builds answer incrementally in REPL variables via sub-call loops
- 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
promptvariable changes each turn)? - What’s the minimal model capability required? (Paper uses frontier models; 8B works after fine-tuning)
Related Pages
- recursive-language-model-prompt — The system prompt entity
- longcot-mini-decomposition-prompt — Variant for complex reasoning
- summary-agent-baseline-prompt — Baseline for comparison
- inference-time-scaling-via-prompt — Prompt-engineering framing
- task-state-machine — Pattern embodied in RLM loop
- code-generation — Required capability
- context-compaction — Alternative approach
- monolithic-prompt-limits — Problem this solves
- multi-file-agent-architecture — Related distributed prompt pattern
Sources
Footnotes
-
raw/prompts/articles/recursive-language-models-arxiv-2512-24601v3.md ↩