Recursive Language Models (RLM)
What is an RLM
Recursive Language Model (scaffold that works on data via a Python REPL and recursive sub-LLM calls.">RLM) - an LLM that uses LLM calls recursively to finish a task. It is a new agent architecture, a scaffold.
- Inference-only paradigm. Same LLM underneath, no training or evals involved.
- Scaffold - the way you arrange prompting, tools and sub-agents around an LLM. (Claude Code, Cursor, Codex are coding scaffolds)
- Some companies (e.g. Prime Intellect) are training their next models for RLM-style use.
Traditional LLM: whole prompt goes into the transformer. Context rot appears as it grows.
RLM: give the LLM a Python environment. The input sits in a variable called context. The model only gets the query and the variable name.
- It reads small bits, e.g. first 500 or 1000 characters.
- It manipulates the variable with code (slicing, regex).
- It calls sub-LLMs on chunks.
- Results come back as variables. Context stays small.
Instead of generating the answer, the LLM generates code. Running that code produces the answer.
REPL
REPL - Read, Eval, Print, Loop. An interactive environment that runs code line by line.
- Read -> takes the code you type
- Eval -> runs it
- Print -> shows output
- Loop -> waits for the next input
context = "some large string"
len(context) # variables from earlier lines are still thereVariables persist across turns. So the root LLM and sub-LLM results can be reused from earlier steps. A prompt becomes just a variable.
RLM components and agent loop
- Root LLM - decides what to look at, when to use sub-agents.
- Sub-LLMs - get a query plus a small sub-context. They can spawn their own sub-agents, so depth can go deep.
- REPL environment - runs the generated code.
- Tools - slicing, regex, standard Python.
- Sub-LLM answers are stored in variables. The root sees only the variable name and reads it if needed.
Loop: write code -> execute -> see output -> decide if more code is needed -> repeat. Same as the agent loop.
Demo: vanilla LLM fails
Setup: synthetic dataset of 5,000 lines like Uma D, age 37, is a chef and is based in New York. Question: how many engineers are in Tokyo? Ground truth is 40, known because we generated the data.
- Whole dataset in the prompt to Gemini 3 Flash (small model) -> answered 12. Wrong.
- Changing the prompt wording gave the same 12.
- Cause: small model, big context, no reliable counting. Hallucination.
- A strong reasoning model (Opus, GPT-5.x) might get it, but slowly and expensively.
Building the REPL and RLM agent
REPL class. Its namespace is everything allowed inside the environment:
- context -> the full input, preloaded
- FINAL(answer) -> root calls this to submit the answer and stop
- llm_query(query, sub_context) -> calls a sub-LLM; prompt = query + sub_context; a counter tracks calls
- standard Python: re, json, len, sorted, min, max, zip, etc.
exec(code, self.namespace) # runs the LLM-written string inside the REPL namespaceexec captures stdout and errors and returns them as the observation. Tests: print slice of context, regex count, FINAL(42) ends the run.
System prompt tells the root:
- You are an RLM with a Python REPL.
- Data is in
context, possibly millions of characters. You cannot see it directly, write code. - Tools: context, llm_query, FINAL, standard libraries.
- Strategy: check length -> print first 500 chars to see structure -> search, filter, count, slice with code -> use llm_query for complex parts -> FINAL.
- Reply with Python code only, in backtick blocks.
Run loop: user message holds only the task and the length of the context. For up to max_iterations (e.g. 10): call the model -> extract code from backticks -> run in REPL -> feed output back -> stop when FINAL is called.
The max iteration limit prevents infinite loops. The system prompt is the main way to steer behaviour.
Demo: RLM solves it
Same model, same question, run as RLM:
- Iteration 1 -> prints context length and first 1000 characters.
- Iteration 2 -> sees the pattern
name, age, profession, based in city. Writes a regex, counts matches, calls FINAL. - Result: 40. Correct. Two iterations, zero sub-LLM calls.
Scaled to 20,000 entries (~1 million characters). Ground truth 146. RLM returned 146 in about a second. To the root LLM the task looks identical.
- Saves tokens, reasoning, time and money.
- If the pattern changes later in the data, the model sees it and explores more.
- Quality depends on the model's code-generation ability, which keeps improving.
Demo: unstructured reviews with sub-LLMs
Harder task where code alone is not enough, because sentiment needs language understanding.
Each sub-LLM sees only one restaurant's reviews as its sub-context. Example output: restaurant with 23 reviews, mixed sentiment, burger most mentioned; another with 29 reviews, positive.
Evidence, comparison and limits
- Paper (Recursive Language Models): as context grows, plain GPT-5 drops, RLM stays flat even at a million tokens.
- Tested on GPT-5, GPT-5 mini and Qwen models. RLM version was best on every long-context benchmark.
- Reported: GPT-5 mini with RLM beat full GPT-5 by about 34 points. Small models also gained 20-30%.
| Approach | How it finds relevant data |
|---|---|
| Stuff everything in prompt | None, model reads all |
| RAG | Index and retrieve chunks up front |
| RLM | Root LLM explores with code and sub-LLMs, no vectors |
Agentic search vs indexing (coding tools):
- Claude Code -> grep, glob, ls, cat. Exact matches, no chunking, embedding or re-indexing, no stored data.
- Cursor -> indexing, with Merkle trees (hashes per file) to re-index only changed files.
- Grep is precise: searching
exception 401returns exact hits. Embedding search also returns near matches like 402, 403, adding noise.
Limits:
- Depends on code generation quality.
- Can hallucinate, e.g. on relational data. Fix by tailoring the system prompt.
- Cannot loop forever because max iterations and depth are set.
Glossary
- RLM
- Recursive Language Model: an LLM scaffold that works on data via a Python REPL and recursive sub-LLM calls.
- Scaffold
- The structure of prompts, tools and sub-agents built around an LLM.
- REPL
- Read, Eval, Print, Loop: an interactive code environment that keeps variables between inputs.
- Root LLM
- The main model in an RLM that writes code and decides when to call sub-LLMs.
- Sub-LLM
- A model called by the root via llm_query on a small piece of data.
- Namespace
- The set of variables and functions available inside the REPL.
- exec
- Python built-in that runs a string as code.