Recursive Language Model (RLM) inference engine — explore massive contexts via iterative LLM + REPL
Project description
rlm-core
Recursive Language Model (RLM) inference engine for Python.
Based on "Recursive Language Models" by Zhang et al. (MIT CSAIL, 2025). The key idea: instead of stuffing massive contexts into the LLM's context window, give the LLM a Python REPL and let it programmatically explore the data, query sub-LLMs, and iteratively build an answer.
Install
pip install rlm-core
Quick Start
import asyncio
from rlm_core import RLM, RLMConfig
config = RLMConfig(
model="gpt-4o-mini",
api_key="sk-...",
)
rlm = RLM(config)
result = asyncio.run(rlm.completion(
query="What is the main argument in this document?",
context=open("huge_document.txt").read(),
))
print(result.answer)
How It Works
- Your query and context metadata go to a root LLM
- The root LLM writes Python code in
```replblocks to explore the context - Code executes in a sandboxed REPL with access to
context(your data) andllm_query()(a sub-LLM) - Execution results feed back to the root LLM
- Loop continues until the LLM calls
FINAL(answer)or max iterations
Local LLM
Point api_base at any OpenAI-compatible server:
config = RLMConfig(
model="qwen2.5-coder-32b",
api_base="http://localhost:8080/v1",
api_key="not-needed",
)
Split Models
Use a strong model for reasoning and a cheaper one for sub-queries:
config = RLMConfig(
model="gpt-4o", # root: does the reasoning
sub_model="gpt-4o-mini", # sub: answers chunk queries
api_key="sk-...",
)
Or use entirely different backends:
config = RLMConfig(
model="gpt-4o",
api_key="sk-...",
sub_model="local-model",
sub_api_base="http://localhost:8080/v1",
sub_api_key="not-needed",
)
API
RLMConfig
| Field | Default | Description |
|---|---|---|
model |
"gpt-4o-mini" |
Root model name |
sub_model |
"gpt-4o-mini" |
Sub-LLM model name |
api_base |
OpenAI | Root model API base URL |
api_key |
"" |
Root model API key |
sub_api_base |
same as api_base |
Sub model API base URL |
sub_api_key |
same as api_key |
Sub model API key |
max_iterations |
15 |
Max REPL interaction loops |
max_output_length |
50000 |
Truncate REPL output at this length |
timeout |
300.0 |
HTTP request timeout (seconds) |
RLMResult
| Field | Description |
|---|---|
answer |
Final answer string |
iterations |
Number of LLM interaction rounds |
total_tokens |
Combined token usage (root + sub) |
trajectory |
List of per-iteration dicts with response/code/output |
RLM.completion(query, context, context_type="text") -> RLMResult
Main entry point. Async.
See Also
- rlm-mcp-server-webui -- Full MCP server with web UI built on this library
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rlm_core-0.1.0.tar.gz.
File metadata
- Download URL: rlm_core-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5311d469cce9e5df5821ce656ae961146d74c6830e643c7e73b4a3d63197e14
|
|
| MD5 |
863e4fcaf74b59470975eaa8580f59cb
|
|
| BLAKE2b-256 |
d46f935369b2e0ca9121cbc49da343025f0bc68e390c51d31d853b9d0c1c4333
|
File details
Details for the file rlm_core-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rlm_core-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50da4cc239827cb7849a3bddd258582689b6013753d26242d54a53d04f4cd184
|
|
| MD5 |
6ce67a7a48fd4555646175c5d1b57f55
|
|
| BLAKE2b-256 |
6c09ca30444fb2c6da78e6a672523b7b51be5e76516b3d5668eff1b18c9a1a5a
|