Skip to main content

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

  1. Your query and context metadata go to a root LLM
  2. The root LLM writes Python code in ```repl blocks to explore the context
  3. Code executes in a sandboxed REPL with access to context (your data) and llm_query() (a sub-LLM)
  4. Execution results feed back to the root LLM
  5. 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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rlm_core-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rlm_core-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

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

Hashes for rlm_core-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a5311d469cce9e5df5821ce656ae961146d74c6830e643c7e73b4a3d63197e14
MD5 863e4fcaf74b59470975eaa8580f59cb
BLAKE2b-256 d46f935369b2e0ca9121cbc49da343025f0bc68e390c51d31d853b9d0c1c4333

See more details on using hashes here.

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

Hashes for rlm_core-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50da4cc239827cb7849a3bddd258582689b6013753d26242d54a53d04f4cd184
MD5 6ce67a7a48fd4555646175c5d1b57f55
BLAKE2b-256 6c09ca30444fb2c6da78e6a672523b7b51be5e76516b3d5668eff1b18c9a1a5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page