Recursive Language Models (RLMs) are a task-agnostic inference paradigm that enables LLMs to handle near-infinite length contexts by allowing the model to programmatically examine, decompose, and recursively call itself over its input.
RLMs enable you to:
- Process documents far exceeding any context window (books, codebases, logs)
- Let LLMs write Python code to explore and analyze massive texts
- Make recursive sub-LLM calls via
llm_query()on specific chunks - Build divide-and-conquer strategies for complex analytical tasks
This hands-on repository introduces Recursive Language Models through two practical tutorials. You'll learn how RLMs solve the context length problem and how to use them with both a standalone library and DSPy.
Through practical examples in the notebooks, you'll explore:
- RLM Fundamentals - How context-as-variable enables infinite context processing
- The rlm Library - Using the standalone RLM implementation with verbose logging
- DSPy Integration - Using RLM within the DSPy framework
- Long-Context Analysis - Analyzing War and Peace (~3.3M chars) and Complete Shakespeare (~5.5M chars)
- Custom Tools & Pipelines - Extending RLM capabilities for domain-specific tasks
RLMs represent a paradigm shift in how LLMs handle long contexts:
- No Truncation - Process entire documents without losing information
- Systematic Analysis - LLMs write code to count, search, and compare across texts
- Transparency - Verbose logging shows exactly how the model reasons
- Flexibility - Works with any LLM provider (OpenAI, Anthropic, Gemini, etc.)
Whether you're analyzing legal documents, exploring codebases, or processing research papers, RLMs unlock capabilities that RAG and traditional context windows can't match.
git clone https://github.com/AI-Makerspace/RLM-AIM-Event.git
cd RLM-AIM-Eventuv syncThis installs:
- The local
rlmpackage (editable) - DSPy for the second tutorial
- Jupyter for running notebooks
Create a .env file in the project root:
echo "OPENAI_API_KEY=your_key_here" > .env
echo "ANTHROPIC_API_KEY=your_key_here" >> .env # OptionalOr manually create .env with:
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
uv run jupyter labThen open either notebook to begin.
Using the standalone rlm library:
- Setup & Installation - Cloning the repo and configuring API keys
- Long-Context Data - Downloading War and Peace from Project Gutenberg
- Basic RLM Usage - Verbose logging and REPL execution
- Analyzing War and Peace - Queries that require full-text understanding
- Log File Analysis - Examining JSONL trajectories
- Multi-Backend Support - OpenAI, Anthropic, and mixed-model configurations
Using DSPy's RLM module:
- DSPy Fundamentals - Signatures, modules, and configuration
- Long-Context Data - Downloading Complete Works of Shakespeare
- Signature-Based Interface - Defining inputs and outputs declaratively
- Structured Outputs - Multiple output fields for organized responses
- Custom Tools - Adding domain-specific functions to the REPL
- Module Composition - Building pipelines with RLM and ChainOfThought
By completing this workshop, you'll understand how to:
- Explain why RLMs solve problems that RAG and long-context models can't
- Use the standalone
rlmlibrary with verbose logging - Use DSPy's RLM with signatures and custom tools
- Analyze massive documents (millions of characters) systematically
- Choose between standalone rlm and DSPy RLM for your use case
- Build custom pipelines combining RLM with other techniques
- RLM Paper - Full technical details
- RLM Blogpost - Intuitive explanation
- RLM Documentation - API reference
- RLM GitHub - Source code
- DSPy GitHub - DSPy framework
01_RLM_Local_Repo_Tutorial.ipynb- Standalone rlm library tutorial02_DSPy_RLM_Tutorial.ipynb- DSPy RLM integration tutorialrlm/- Local clone of the rlm repository.env- Your API keys (not tracked in git)
Start exploring RLMs by opening the first notebook and running through the examples!
