TL;DR
Instead of stuffing millions of tokens directly into a language model's neural network—where it inevitably suffers from "context rot" or hits a hard memory wall—what if we treated the prompt as an external variable in a Python environment? Recursive Language Models (RLMs) let an LLM write code to inspect, slice, and recursively call itself over pieces of massive inputs. This simple shift allows models to process 10M+ tokens, crush information-dense tasks where frontier models score 0%, and keep API costs surprisingly low.
What Problem Does This Paper Address?
Even as context windows expand, frontier LLMs suffer from context rot: their reasoning quality degrades steeply as prompts get longer, especially on complex tasks.
Currently, the go-to workaround for massive inputs is context compaction—periodically summarizing older parts of the text to free up space. That’s fine for casual chat, but fatal if a task requires dense, fine-grained access to every line of a massive dataset. Meanwhile, existing coding or sub-agent frameworks either still load the prompt into the root window first, or require the LLM to verbalize every sub-task token-by-token—meaning they hit an output-length bottleneck if they need to launch thousands of sub-queries. We need a way to give LLMs effectively unbounded input, output, and semantic reasoning horizons.
Methods
An RLM wraps a base LLM in an inference-time scaffold that exposes the exact same interface as a standard model: string in, string out. Behind the scenes, however, the prompt is never fed directly into the root Transformer.
Instead, the RLM relies on three core design choices:
- Symbolic Handle to the Prompt: The RLM initializes a persistent Python REPL (Read-Eval-Print Loop) and loads the massive prompt string into memory as a variable
P. The root LLM only sees constant-size metadata (likelen(P)and a tiny prefix). - Truncated Observation: The LLM writes Python code to inspect or slice
P. Anystdoutprinted back to the LLM is truncated to a constant size, forcing the model to manage data via code variables rather than polluting its own context window. - Symbolic Recursion: Inside the REPL, the LLM has access to a
sub_RLMfunction. Instead of manually typing out three sub-agent prompts, the LLM can write a three-lineforloop in Python to programmatically invoke itself (or a smaller model) thousands of times over slices ofP, stitching the results into aFinalvariable to return.
The authors also show you can post-train small models (via rejection fine-tuning on 1,000 trajectories or RL with verifiable rewards) to become "natively recursive."
Datasets
To test how complexity scales with length, the authors evaluate across tasks requiring $O(1)$, $O(N)$, and $O(N^2)$ semantic work:
- Evaluation: S-NIAH (single needle-in-a-haystack from RULER), BrowseComp-Plus (multi-hop QA over 1,000 documents / 6M–11M tokens), OOLONG (linear-complexity semantic labeling and aggregation), LongBench-v2 CodeQA (codebase understanding), and LongCoT-mini (compositional reasoning).
- Newly Introduced: OOLONG-Pairs, a brutal modification of OOLONG containing 20 queries that require aggregating pairs of chunks—forcing quadratic $O(N^2)$ processing over the input.
- Training: LongBenchPro (used to distill 1,000 RLM trajectories for fine-tuning Qwen3-8B) and MRCRv2 (a synthetic counting/reproduction benchmark used for RL training).
Key Insights and Ideas
- Effective context length depends on task complexity: Frontier models ace 1M-token needle-in-a-haystack tasks because the "needle" stays constant ($O(1)$). When the answer depends on every line ($O(N)$) or pairs of lines ($O(N^2)$), vanilla models collapse long before hitting their token limits.
- Offloading scales length; recursion scales density: Simply putting the prompt in a REPL (
depth=0, no sub-calls) lets models read 10M+ tokens using keyword searches. However, recursive sub-calling (depth >= 1) is the secret sauce for information-dense tasks where every chunk requires genuine semantic understanding rather than regex heuristics. - Recursion generalizes across domains and lengths: Teaching a small model how to probe variables and launch sub-calls on one dataset transfers to completely unrelated tasks—and RL-training on 64k-token prompts generalizes zero-shot to 1M-token evaluations.
Key Results
Comparison of vanilla GPT-5 vs. RLM(GPT-5) showing how RLM avoids steep context rot and scales beyond the 272K context limit.
- Crushing Dense & Massive Tasks: On OOLONG-Pairs (32K tokens—well inside standard context limits!), base GPT-5 and Qwen3-Coder-480B score $\le$ 0.1% F1. Wrapped in an RLM (
depth=1), they jump to 58.0% and 23.1% F1 (and 76.0% for GPT-5 atdepth=3). On BrowseComp-Plus (6M–11M tokens), RLM(GPT-5) hits 91.3%, beating compaction (70.5%) and BM25 retrieval (51.0%) at an average cost of just $0.99. - General Reasoning Boost: Even on short prompts requiring deep compositional thought (LongCoT-mini), RLM(GPT-5.2) with decomposition hints boosts overall solve rates from 38.7% to 65.6% (a 69.5% relative gain).
- Punching Above Weight with Fine-Tuning: Fine-tuning Qwen3-8B on just 1,000 unrelated trajectories creates RLM-Qwen3-8B, which improves median RLM performance by 28.3%, runs 3$\times$ faster, and approaches vanilla GPT-5 on several tasks. Meanwhile, RL-training a 4B model on 64k contexts pushes its 1M-token MRCRv2 score near 1.0, blowing past Gemini 3.1 Pro (~0.26).
Limitations and Future Work
RLMs aren't magic bullets without tradeoffs:
- Runaway Costs & Latency: While median RLM runs are often cheaper than base LLM calls, outlier trajectories where the model gets stuck in bad loops can cause exploding sub-call costs. Furthermore, all sub-calls in this implementation were sequential/blocking, leading to high wall-clock runtimes.
- Syntax Fragility: Models that write buggy code struggle at higher recursion depths; for instance, Qwen3-Coder frequently made syntax errors that compounded when sub-RLMs also made syntax errors.
- Next Steps: Implementing asynchronous parallel sub-calls, robust sandboxing, cost guardrails, and scaling up native RLM post-training as a brand-new axis of inference-time compute.
Summary updated