Skip to main content

Pydantic AI RLM

Handle Extremely Large Contexts with Any LLM Provider

GitHub • PyPI • Examples

Python 3.10+ License: MIT Pydantic AI PyPI version

Switch Providers Instantly  •  Sandboxed Code Execution  •  Sub-Model Delegation  •  Grounded Citations  •  Fully Type-Safe


What is RLM?

RLM (Recursive Language Model) is a pattern for handling contexts that exceed a model's context window, introduced by Alex L. Zhang, Tim Kraska, and Omar Khattab in their paper Recursive Language Models. Instead of trying to fit everything into one prompt, the LLM writes Python code to programmatically explore and analyze the data.

The key insight: An LLM can write code to search through millions of lines in seconds, then use llm_query() to delegate semantic analysis of relevant chunks to a sub-model.

This library is an implementation inspired by the original minimal implementation.


Get Started in 60 Seconds

pip install pydantic-ai-rlm
from pydantic_ai_rlm import run_rlm_analysis

answer = await run_rlm_analysis(
    context=massive_document,  # Can be millions of characters
    query="Find the magic number hidden in the text",
    model="openai:gpt-5",
    sub_model="openai:gpt-5-mini",
)

That's it. Your agent can now:

  • Write Python code to analyze massive contexts
  • Use llm_query() to delegate semantic analysis to sub-models
  • Work with any Pydantic AI compatible provider

Why pydantic-ai-rlm?

Switch Providers Instantly

Built on Pydantic AI, you can test any model with a single line change:

# OpenAI
agent = create_rlm_agent(model="openai:gpt-5", sub_model="openai:gpt-5-mini")

# Anthropic
agent = create_rlm_agent(model="anthropic:claude-sonnet-4-5", sub_model="anthropic:claude-haiku-4-5")

agent = create_rlm_agent(model="anthropic:claude-sonnet-4-5", sub_model="openai:gpt-5-mini")

Reusable Toolset

The RLM toolset integrates with any pydantic-ai agent:

from pydantic_ai import Agent
from pydantic_ai_rlm import create_rlm_toolset, RLMDependencies

# Use the toolset in any agent
toolset = create_rlm_toolset(sub_model="openai:gpt-5-mini")
agent = Agent("openai:gpt-5", toolsets=[toolset])

How It Works

┌─────────────────────────────────────────────────────────────────┐
│                         pydantic-ai-rlm                         │
│                                                                 │
│   ┌─────────────┐         ┌─────────────────────────────────┐   │
│   │   Main LLM  │         │     Sandboxed REPL Environment  │   │
│   │   (gpt-5)   │────────>│                                 │   │
│   └─────────────┘         │   context = <your massive data> │   │
│         │                 │                                 │   │
│         │                 │   # LLM writes Python code:     │   │
│         │                 │   for line in context.split():  │   │
│         │                 │       if "magic" in line:       │   │
│         │                 │           result = llm_query(   │   │
│         │                 │               f"Analyze: {line}"│   │
│         │                 │           )                     │   │
│         │                 │                                 │   │
│         │                 └───────────────┬─────────────────┘   │
│         │                                 │                     │
│         │                                 ▼                     │
│         │                       ┌─────────────────┐             │
│         │                       │    Sub LLM      │             │
│         │                       │  (gpt-5-mini)   │             │
│         │                       └─────────────────┘             │
│         │                                                       │
│         ▼                                                       │
│   ┌─────────────┐                                               │
│   │   Answer    │                                               │
│   └─────────────┘                                               │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
  1. Main LLM receives the query and writes Python code
  2. REPL Environment executes code with access to context variable
  3. llm_query() delegates semantic analysis to a cheaper/faster sub-model
  4. Main LLM synthesizes the final answer from code execution results

Examples

Needle in Haystack

Find specific information in massive text:

from pydantic_ai_rlm import run_rlm_analysis

# 1 million lines of text with a hidden number
massive_text = generate_haystack(num_lines=1_000_000)

answer = await run_rlm_analysis(
    context=massive_text,
    query="Find the magic number hidden in the text",
    model="openai:gpt-5",
    sub_model="openai:gpt-5-mini",
)

JSON Data Analysis

Works with structured data too:

from pydantic_ai_rlm import create_rlm_agent, RLMDependencies

agent = create_rlm_agent(model="openai:gpt-5")

deps = RLMDependencies(
    context={"users": [...], "transactions": [...], "logs": [...]},
)

result = await agent.run(
    "Find all users with suspicious transaction patterns",
    deps=deps,
)

Grounded Responses with Citations

Get answers with traceable citations back to the source:

from pydantic_ai_rlm import run_rlm_analysis

# Enable grounding for citation tracking
result = await run_rlm_analysis(
    context=financial_report,
    query="What were the key revenue changes?",
    model="openai:gpt-5",
    grounded=True,  # Returns GroundedResponse instead of str
)

# Response contains citation markers
print(result.info)
# "Revenue increased [1] primarily due to [2]"

# Grounding maps markers to exact quotes from the source
print(result.grounding)
# {"1": "by 45% year-over-year", "2": "expansion into Asian markets"}

API Reference

create_rlm_agent()

Create a Pydantic AI agent with RLM capabilities.

agent = create_rlm_agent(
    model="openai:gpt-5",           # Main model for orchestration
    sub_model="openai:gpt-5-mini",  # Model for llm_query() (optional)
    code_timeout=60.0,               # Timeout for code execution
    custom_instructions="...",       # Additional instructions
    grounded=True,                   # Return GroundedResponse with citations
)

create_rlm_toolset()

Create a standalone RLM toolset for composition.

toolset = create_rlm_toolset(
    code_timeout=60.0,
    sub_model="openai:gpt-5-mini",
)

run_rlm_analysis() / run_rlm_analysis_sync()

Convenience functions for quick analysis.

# Async
answer = await run_rlm_analysis(context, query, model="openai:gpt-5")

# Sync
answer = run_rlm_analysis_sync(context, query, model="openai:gpt-5")

# With grounding (returns GroundedResponse)
result = await run_rlm_analysis(context, query, grounded=True)
print(result.info)       # Text with [N] markers
print(result.grounding)  # {"1": "exact quote", ...}

RLMDependencies

Dependencies for RLM agents.

deps = RLMDependencies(
    context="...",  # str, dict, or list
    config=RLMConfig(
        code_timeout=60.0,
        truncate_output_chars=50_000,
        sub_model="openai:gpt-5-mini",
    ),
)

configure_logging()

Enable verbose logging to see what the agent is doing in real-time.

from pydantic_ai_rlm import configure_logging, run_rlm_analysis

# Enable logging (uses rich if installed, falls back to plain text)
configure_logging(enabled=True)

# Now you'll see code executions and outputs in the terminal
answer = await run_rlm_analysis(
    context=massive_document,
    query="Find the magic number",
    model="openai:gpt-5",
)

# Disable logging when done
configure_logging(enabled=False)

Install with rich logging support for syntax highlighting and styled output:

pip install pydantic-ai-rlm[logging]

Or install rich separately:

pip install rich

When enabled, you'll see:

  • Syntax-highlighted code being executed (with rich)
  • Execution results with status indicators (SUCCESS/ERROR)
  • Execution time for each code block
  • Variables created during execution
  • LLM sub-queries and responses (when using llm_query())

Note: Logging works without rich installed - it will use plain text output instead of styled panels


REPL Environment

The sandboxed REPL provides:

Feature Description
context variable Your data loaded and ready to use
llm_query(prompt) Delegate to sub-model (if configured)
Safe built-ins print, len, range, etc.
Common imports json, re, collections, etc.
Persistent state Variables persist across executions
Output capture stdout/stderr returned to agent

Blocked for security: eval, exec, compile, open (outside temp dir)


Related Projects

  • rlm - Original RLM implementation by Alex L. Zhang, Tim Kraska, and Omar Khattab
  • rlm-minimal - Minal RLM implementation by Alex L. Zhang
  • pydantic-ai - The foundation: Agent framework by Pydantic
  • pydantic-deep - Full deep agent framework with planning, filesystem, and more

Contributing

git clone https://github.com/vstorm-co/pydantic-ai-rlm.git
cd pydantic-ai-rlm
pip install -e ".[dev]"
pytest

License

MIT — see LICENSE

Built with Pydantic AI by vstorm-co

Metadata

Release files for pydantic-ai-rlm 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-ai-rlm 0.1.2
File Size Uploaded Provenance
pydantic_ai_rlm-0.1.2.tar.gz 21.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-rlm 0.1.2
File Interpreter ABI Platform
pydantic_ai_rlm-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 44.1 kB

Release files / pydantic_ai_rlm-0.1.2.tar.gz

Download URL pydantic_ai_rlm-0.1.2.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5429a24dd904e1c6377e392ca0fcb82816d958820d87205478c63dcb81f31ce7
BLAKE2b-256 checksum
How to use checksums
4c18983a647ee4b8764abb6310ed8c15d492b726ca02a80e0dc13bcf93143cfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 4, 2026.

Transparency log

Release files / pydantic_ai_rlm-0.1.2-py3-none-any.whl

Download URL pydantic_ai_rlm-0.1.2-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c088fc2724a61d32b50a5191e6afb33d8425f873f2d823b6ecf622a2d5ad890
BLAKE2b-256 checksum
How to use checksums
e192563a17babb301b7feadbfc3f1022af3a4adff1c4adf24211bc2052332db0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 4, 2026.

Transparency log

Security for pydantic-ai-rlm 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pydantic_ai_rlm-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page