Skip to main content
Recursive Language Models (RLMs) are an inference-time paradigm introduced by MIT CSAIL researchers (Alex L. Zhang, Tim Kraska, and Omar Khattab, 2025). Instead of stuffing massive documents directly into a model’s prompt window, RLMs store input context as an external variable within an isolated Python REPL sandbox. The orchestrator model writes and runs Python code iteratively to inspect data, delegates semantic sub-tasks to sub-language models (sub_lm), and submits structured results via SUBMIT().

Quickstart with Real-Time Text Streaming

Wrap your RLM module with dspy.streamify to stream reasoning tokens and intermediate actions directly to your terminal without waiting for the full multi-iteration execution loop to complete:

Why Use RLMs?

Standard large language models suffer from performance degradation (context rot) when processing inputs that span hundreds of thousands or millions of tokens. In addition, prompt-stuffing incurs high token costs because the entire document must be resent with every turn. RLMs eliminate these constraints by separating memory storage from the active LLM context window:
  1. Context as an External Object: Documents or datasets live in the Python REPL memory as the context variable rather than inside the LLM prompt.
  2. Programmatic Data Filtering: The model writes Python scripts (regex, slicing, data structures, arithmetic) to inspect only relevant subsets.
  3. Sub-Model Delegation (llm_query): When semantic comprehension of a chunk is required, the model calls llm_query(chunk) to delegate to a cost-effective, low-latency worker model.
  4. Iterative Convergence: REPL output is fed back into the next iteration until the model satisfies its task and calls SUBMIT(answer=...).

Architecture Comparison

Decision Matrix: When to Use RLMs