grishahq/recursive-llm
Recursive Language Models for efficient long-context processing. Analyze 1M+ tokens by storing context in a Python REPL while reducing LLM token usage.
What it solves
Recursive Language Models (RLM) addresses the problem of analyzing extremely large contexts that are too expensive or too large to fit into a standard LLM prompt. Instead of sending the entire source text to the model, RLM keeps the data in a restricted Python REPL, allowing the model to selectively search, compute, and process only the necessary sections of the text.
How it works
RLM operates by storing the source context as a variable within a persistent Python REPL environment. The root model receives the query and instructions but not the full text. The model then interacts with the context using Python code to perform tasks like regex searches, peeking at specific character ranges, or performing local computations.
When needed, the model can delegate specific tasks to other model calls: it can make a plain LLM call (llm_query) or spawn a child RLM (rlm_query) if the configured recursion depth allows. The process continues iteratively until the model provides a final answer through a specific termination function.
Who it’s for
This library is designed for developers building applications that require high-precision analysis of massive documents where cost and token usage must be bounded. It is particularly useful for tasks that benefit from exact counting, filtering, and local Python computation rather than just semantic synthesis.
Highlights
- Externalized Context: Keeps large source text in a REPL rather than repeating it in prompts.
- Bounded Execution: Implements tree-wide limits on the number of calls, tokens, estimated cost, and elapsed time.
- Provider Portability: Supports over 100 providers via LiteLLM, including OpenAI, Anthropic, DeepSeek, and local models (Ollama, llama.cpp).
- Restricted Execution: Uses a spawned RestrictedPython worker with hard timeouts and resource limits for security.
- Observable Runs: Provides detailed per-run statistics, typed failures, and complete execution trajectories in JSONL format.
- Custom Validation: Allows developers to define a
final_answer_validatorto reject answers and provide feedback to the model without ending the run.
Related
- Project
- Project
- Project
- Project
- Project