RLM Class Reference
Complete API documentation for the core RLM class.Overview
TheRLM class is the main entry point for Recursive Language Model completions. It wraps an LM client and execution environment to enable iterative, code-augmented reasoning.
Constructor
backend
Type: Literal["openai", "portkey", "openrouter", "vllm", "litellm", "anthropic"]Default:
"openai"
The LM provider backend to use for the root model.
backend_kwargs
Type: dict[str, Any] | NoneDefault:
None
Configuration passed to the LM client. Required fields vary by backend:
| Backend | Required | Optional |
|---|---|---|
openai | model_name | api_key, base_url |
anthropic | model_name | api_key |
portkey | model_name, api_key | base_url |
openrouter | model_name | api_key |
vllm | model_name, base_url | — |
litellm | model_name | varies by provider |
environment
Type: Literal["local", "modal", "docker"]Default:
"local"
The execution environment for running generated code.
| Environment | Description |
|---|---|
local | Same-process execution with sandboxed builtins |
docker | Containerized execution in Docker |
modal | Cloud sandbox via Modal |
environment_kwargs
Type: dict[str, Any] | NoneDefault:
None
Configuration for the execution environment:
Local:
max_depth
Type: intDefault:
1
Maximum recursion depth for nested RLM calls. Currently only depth 1 is fully supported.
When depth >= max_depth, the RLM falls back to a regular LM completion.
max_iterations
Type: intDefault:
30
Maximum number of REPL iterations before forcing a final answer.
Each iteration consists of:
- LM generates response (potentially with code blocks)
- Code blocks are executed
- Results are appended to conversation history
custom_system_prompt
Type: str | NoneDefault:
None
Override the default RLM system prompt. The default prompt instructs the LM on:
- How to use the
contextvariable - How to call
llm_query()andllm_query_batched() - How to signal completion with
FINAL()
other_backends / other_backend_kwargs
Type: list[str] | None / list[dict] | NoneDefault:
None
Register additional LM backends available for sub-calls via llm_query().
logger
Type: RLMLogger | NoneDefault:
None
Logger for saving iteration trajectories to disk.
verbose
Type: boolDefault:
False
Enable rich console output showing:
- Metadata at startup
- Each iteration’s response
- Code execution results
- Final answer and statistics
Methods
completion()
Main entry point for RLM completions.
Parameters
prompt
The context/input to process. Becomes the context variable in the REPL.
root_prompt
Optional short prompt shown to the root LM. Useful for Q&A tasks where the question should be visible throughout.
Returns
RLMChatCompletion dataclass:
Example
Response Types
RLMChatCompletion
UsageSummary
Error Handling
RLM follows a “fail fast” philosophy:max_iterations without finding a FINAL() answer, it prompts the LM one more time to provide a final answer based on the conversation history.
Thread Safety
Eachcompletion() call:
- Spawns its own
LMHandlersocket server - Creates a fresh environment instance
- Cleans up both when done
completion() calls independent, but the RLM instance itself should not be shared across threads without external synchronization.