Recursive language models
A visual guide to Alex L. Zhang, Tim Kraska and Omar Khattab, “Recursive Language Models”, arXiv:2512.24601, version 2, January 2026 · the paper · the code. Built with Animated Visuals. Every number is from the paper and says where it comes from.
Recursive language models (RLMs) let a model work with inputs longer than its context window by storing the prompt in a Python variable. The example asks who won a beauty pageant mentioned in one of 1,000 documents, about 8.3 million tokens or roughly 30 times what GPT-5 reads at once. The root model searches the variable and uses a sub-model call, a separate model request on selected text, to read the relevant document. This guide shows how the loop works, how it scales, its results on four tasks, and its limits.
The prompt as a variable
A model's context window is the text it can read at once. GPT-5's window is 272K tokens (the paper's Fig. 1). The example's 1,000 documents total about 8.3M tokens (App. E.1). A base model cannot take the full input. Base GPT-5 therefore scores 0.0 on this task in Table 1, marked as running into its input limit. Even when an input fits, the paper reports context rot, where quality drops as the prompt grows (§1).
An RLM stores the input in a Python variable named context instead of putting it in the model's window. It tells the model what context contains and how long its pieces are (App. C.1). The window holds a short description while the complete input remains available in the variable. Switch between the two models below, or choose a smaller input to see one that fits.
A base model must fit the full input
Fig. 1 · lengths to scaleEach tile is one 272K-token GPT-5 window (the paper's Fig. 1), and the input sizes come from Table 1 and App. E. The RLM window contents are illustrative, based on the instructions, question and input description in §2 and App. C.1; switch models or choose a smaller input.
How the loop runs
The root model controls the loop. On each turn, it writes code. The Python session runs that code and returns a short, cut-off printout to the root model's window (§2). The code can use Python and one extra function, llm_query. This function sends a string to another model and returns the response. Each use is a sub-model call. In the paper's GPT-5 runs, GPT-5 is the root model and GPT-5-mini handles the sub-model calls (§3.2). The results stay in variables outside the root model's window. The root model ends the loop with FINAL(answer), or uses FINAL_VAR(name) to return a variable (App. C.1).
The next figure follows the real pageant-question run from Appendix E.1, one turn at a time.
The root receives only short printouts
App. E.1 · a real runThe steps follow the paper's trajectory for BrowseComp-Plus query 74 (App. E.1); the cost, token count and run times come from its panels, and the animation timing is illustrative. Use Prev and Next to change steps, or hover a box for its role.
What separates it from an agent with tools
Coding agents and retrieval agents also run code and call models. The paper compares its RLM loop in Algorithm 1 with a similar agent loop in Algorithm 2 and identifies three differences (§2).
| Design choice | An RLM | An agent with tools |
|---|---|---|
| Prompt location | A variable; the model receives a handle and a length | The model's window; it reaches the window limit and falls back to summarizing |
| Answer location | A variable, so the answer can exceed one call's output | The model's output, limited to what one call can write |
| Sub-model calls | Code makes them inside loops on selected pieces, including one per line when needed | The model requests a few calls at a time as actions |
How the model splits the input
The instructions show the root model several example strategies but do not prescribe how to split the work (App. C.1). Appendix E shows four different runs. For the pageant question, the root searches first and reads one document. On a code repository, it splits the input into large chunks and asks about each one. On two counting tasks, Qwen3-Coder labels questions in batches in one run and one at a time in the other. The split determines the number of sub-model calls. All four runs use one level of recursion. Their sub-model calls are regular model calls and cannot start another RLM (§6).
The root decides how to split the input
Each tab shows one trajectory from Appendix E, with its counts, sizes and costs. The pieces are spaced evenly except for document 6 in the pageant run, which uses its real position; choose a tab to replay it.
Scaling with input length
The paper's Fig. 1 compares input length with how much of the input the answer depends on (§3). S-NIAH hides one phrase in filler, so the work stays constant as the input grows. OOLONG asks for a count that depends on nearly every line, so the work grows with the input. OOLONG-Pairs asks for pairs of entries, so the work grows with the square of the input (§3.1). GPT-5 finds the phrase at every length it can take. Its score falls to almost nothing on OOLONG-Pairs by 33K tokens. The RLM declines more slowly and continues past GPT-5's window to 1M tokens. Drag the cursor to inspect any length.
RLM scores decline more slowly with input length
Fig. 1 · the paper's dataThe values were extracted from the paper's vector Fig. 1 for GPT-5 and RLM(GPT-5), using one level of sub-model calls; they match Table 1 where the two overlap. Drag the cursor, or focus it and use the arrow keys.
Results on four tasks
Table 1 compares the RLM with a base model; CodeAct, an agent that runs code but keeps the prompt in its window, with either the BM25 search tool or sub-model calls; a summary agent that summarizes when its window fills; and an RLM with a Python session but no sub-model calls (§3.2).
With GPT-5, the RLM scores highest on all four tasks. On BrowseComp+, the pageant question's task, it scores 91.3 against 70.5 for the summary agent at $0.99 a query. The paper's linear estimate for GPT-5-mini reading all 6 to 11M tokens is $1.50 to $2.75 (Obs. 1). On OOLONG, which fits in the window, the RLM improves on the base model by 28.4% (56.5 against 44.0). On OOLONG-Pairs, it scores 58.0 while the base model scores 0.1. Sub-model calls improve the dense tasks: 56.5 against 36.0 without them on OOLONG, and 58.0 against 43.9 on OOLONG-Pairs. With Qwen3-Coder, the version without sub-model calls scores highest on CodeQA (66.0 against 56.0) and BrowseComp+ (46.0 against 44.7) (Obs. 2). Switch the model to compare both.
An RLM variant leads every task for both models
Table 1The bars show scores and average API cost per query from Table 1; higher is better, using F1 on OOLONG-Pairs and percent correct elsewhere. Hatched bars mark input-limit cases from Table 1's asterisk; hover a bar to see its cost spread.
Average cost does not show the most expensive runs. With GPT-5, the median RLM run costs less than the median base-model call. Some runs take many turns and cost much more (the paper's Fig. 3, Obs. 4). Compared with the summary agent, which reads the whole input, the paper reports RLMs up to 3× cheaper (Obs. 4).
Fine-tuning a small model
Qwen3-8B, a small open model, performs poorly as an RLM without fine-tuning. The authors fine-tuned it on 1,000 filtered RLM runs that Qwen3-Coder-480B produced on LongBenchPro, using tasks unrelated to the four benchmarks (§3.2, App. A). Training used 300 steps at a batch size of 64 and took 48 H100 hours (App. A). Each sub-model call is an ordinary model call, so only the root model had to learn how to manage the session (§3.2). Use Next to step through the comparison.
Fine-tuning improves the small model on all tasks
Table 1The scores come from the Qwen3-8B and GPT-5 rows in Table 1, and the gains under the chart are computed from them. Hover a mark to see its value and cost.
The paper reports a 28.3% improvement, described as “on average” in the abstract and “a median” in the introduction. From the rounded Table 1 scores, the four relative gains are and their median is . Allowing for rounding, 28.3% is the median rather than the mean. The fine-tuned model also costs less, at 1 to 4 cents a query instead of 3 to 19 (Table 1). It exceeds base GPT-5 on three tasks, including two that GPT-5 could not fully take in. On OOLONG, it scores 32.0 against 44.0.
Limits
The paper reports four limits (App. B, App. E).
- Latency. Every sub-model call waits for the previous one, so the slowest runs take much longer than any baseline (§4, the paper's Figs. 7 and 8). Parallel sub-model calls are left to future work.
- Model behavior varies. On one OOLONG question, Qwen3-Coder made one sub-model call per line, producing hundreds to thousands for one task. GPT-5 makes “on the order of ten” (App. E). The authors added one line to Qwen3-Coder's instructions to discourage this behavior (App. C.1).
- Final-answer handling. In one OOLONG-Pairs run, the model built the correct list in a variable and kept checking it. It then returned a different answer that it wrote itself, and that answer was wrong (App. E.2).
- Short inputs. The base model is sometimes slightly better on short inputs (Obs. 3). Small models without strong coding skills also struggle to use the session (App. B).
Checked against the source
- This guide uses arXiv:2512.24601 version 2 (28 January 2026), including the appendices. Version 3 (11 May 2026) extends Table 1 with recursion depths 0 to 3 and coding-agent baselines, and it changes some numbers. This guide does not use version 3's numbers.
- GPT-5's 272K-token window comes from the paper's Fig. 1 caption. App. E.1 provides the 1,000 documents, about 8.3M tokens, $0.079, nine search keywords, document index 6, the sub-model call's 1,605 tokens and $0.001682 on GPT-5-mini, run times of 0.158 s, 9.089 s and 6.279 s, and the answer Maria Dalmacio. “About 30 windows” is 8.3M ÷ 272K = 30.5.
- Table 1 provides the task lengths (CodeQA 23K to 4.2M, BrowseComp+ 6M to 11M, OOLONG 131K, OOLONG-Pairs 32K), every score, and every cost with its spread in Figs. 5 and 6. The percentages 28.4% for OOLONG over the base model, 91.3 against 70.5, and 17.9% and 3% for the no-sub-model-call ablation with Qwen3-Coder were recomputed from Table 1 and match Obs. 1 and 2.
- Fig. 4's scores were extracted from the paper's vector Fig. 1. Each plotted point's coordinates were read from the PDF and mapped to its gridlines from 0 to 100. They match Table 1 at 131K (GPT-5 44.0, RLM 56.5) and at 32K for the RLM (58.0). At 33K, Fig. 1 shows GPT-5 at 3.7 on OOLONG-Pairs, while Table 1 has 0.1 at 32K; these appear to be separate runs.
- The paper's Fig. 1 caption in version 2 says the lengths run from 2¹³ to 2¹⁸, while its RLM points extend to 1M (2²⁰). Version 3's caption says 2²⁰. The guide plots the points that are drawn.
- Fig. 3 uses App. E.1 (BrowseComp+ query 74); E.4 (CodeQA query 44, about 900K tokens, chunks of 380,000 characters, 11 sub-model calls, $0.27); E.2 (OOLONG-Pairs query 3, 76,083 characters, 794 lines including 787 questions, 8 batches of 100, 131 users, 8,515 pairs, $1.12, wrong answer); and E.3 (OOLONG query 212, 3,182 questions, 382 sub-model calls in the step shown, $0.38). The CodeQA run's 10 chunks are inferred from 11 calls minus the one that combines them.
- Fine-tuning uses 1,000 trajectories (§3.2), 300 steps, batch 64, and 48 H100 hours (App. A). The page computes the relative gains and their median from Table 1. Appendix A also gives other counts at different filtering stages, 2,250, 1,072, and 2,241 and 953 in its Fig. 5, that do not clearly reconcile with 1,000. The guide uses the number from §3.2.
- The RLM window contents in Fig. 1, animation timing in Fig. 2, and even piece spacing in Fig. 3 are simulated or illustrative. Everything else is data from the paper.
- The guide leaves out the cost-against-length plot in Fig. 11 and the cost and runtime percentile charts in Figs. 3, 7 to 10 because they are raster images without printed values, so no number is read from them. It also leaves out the BrowseComp+ document-count experiment in the paper's Fig. 6 and App. D.2, and the code in the trajectory screenshots.