Recursive Language Models (RLM)¶
| Status | Active |
| Path | AI/ML |
| Prereqs | Instructor |
| Repo | pysprings/rlm |
Overview¶
An implementation of Recursive Language Models in 77 lines of Python, based on the 2025 MIT paper by Zhang, Kraska, and Khattab. The key insight: instead of feeding entire documents into an LLM's context window (where quality degrades with length), store the document as a Python variable and let the LLM write code to explore it.
How It Works¶
User Query → Main LLM → Writes Python Code → Executes in REPL
↓
Calls Sub-LLM on Chunks
↓
Reads Print Output
↓
Iterates Until Done
Architecture (8 Chapters)¶
The project uses literate programming — the presentation slides are the source of truth, and the Python code is automatically extracted from them using Entangled.
- Title & Problem — Context rot: LLMs degrade on long documents
- Build Steps 1–6 — REPL namespace, sub-LLM, conversation loop, FINAL signaling, print capture, full assembly
- Implementation — The complete 77-line function
- Chatbot — Interactive variant
- Demo — Live examples with Project Gutenberg books
Key Concepts¶
- Literate programming (markdown → tangled Python)
- Python
exec()and namespace manipulation - LLM orchestration (main model + sub-model)
- Exception-based control flow (
FINAL()signal) - REPL environment simulation
Try It¶
- Clone:
git clone https://github.com/pysprings/rlm cd rlm && pip install -e .- Browse the slides:
mkdocs serve→ http://localhost:8000 - Run the demo:
python code/demo.py
Where to Go Next¶
- marketing-bot — Another architecture for LLM-driven systems
- Entangled — Deep dive into the literate programming tool used here