← govind p lal
AI and ML Bootcamp · Lecture 12 · lecture notes

Recursive Language Models (RLM)

In short — How can an LLM handle huge inputs without reading them all in its prompt? A Recursive Language Model gives the LLM a Python environment and keeps the data in a variable. The model writes code to explore the data and calls sub-LLMs on chunks. A small model that fails with a plain prompt gets the right answer this way.

What is an RLM

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 11: Week 10 - AI&ML | RAG Continuation - Part 2 | 23/03/2026

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.

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.

!

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.

context = "some large string"
len(context)   # variables from earlier lines are still there

Variables 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

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026
writes codellm_queryoutputresult variableUser queryRoot LLMsees query onlyPython REPLholds context variableSub-LLMswork on chunksFINAL answer
RLM loop: the context stays in the REPL, never in the root prompt.

Loop: write code -> execute -> see output -> decide if more code is needed -> repeat. Same as the agent loop.

Demo: vanilla LLM fails

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

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.

Building the REPL and RLM agent

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

REPL class. Its namespace is everything allowed inside the environment:

exec(code, self.namespace)  # runs the LLM-written string inside the REPL namespace

exec 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:

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:

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.

Demo: unstructured reviews with sub-LLMs

Harder task where code alone is not enough, because sentiment needs language understanding.

sub-contextPeek first 1000charsRegex: group byrestaurantllm_query perrestaurant10 sub-LLM callsJSON: sentiment, topdishFINAL summary
Code does the grouping; sub-LLMs do the language judgement.

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

builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026, Lecture 11: Week 10 - AI&ML | RAG Continuation - Part 2 | 23/03/2026
ApproachHow it finds relevant data
Stuff everything in promptNone, model reads all
RAGIndex and retrieve chunks up front
RLMRoot LLM explores with code and sub-LLMs, no vectors

Agentic search vs indexing (coding tools):

Limits:

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.