Secure LLM-driven code execution with Docker sandboxing
Project description
RLM-Python ๐๐ค
Recursive Language Model - A secure Python library for LLM-driven code execution with Docker sandboxing, egress filtering, and memory-efficient context handling.
๐ Features
- ๐ณ Docker Sandbox Execution - Execute untrusted code in isolated containers with gVisor support
- ๐ OS-Level Security - Network isolation, memory limits, process limits, privilege restrictions
- ๐ก๏ธ Egress Filtering - Prevent data exfiltration via entropy detection and pattern matching
- ๐ Memory-Efficient Context - Handle gigabyte-scale files with mmap-based
ContextHandle - ๐ค Multi-Provider LLM - Support for OpenAI, Anthropic, and Google Gemini
- ๐ฐ Budget Management - Track API costs and enforce spending limits
- ๐ Typed & Tested - Full type hints with comprehensive test coverage
๐ Quick Start
Installation
pip install rlm-python
Or install from source:
git clone https://github.com/rlm-python/rlm.git
cd rlm
pip install -e ".[dev]"
Basic Usage
from rlm import Orchestrator, settings
# Configure via environment variables or .env file
# RLM_API_KEY=sk-...
# RLM_API_PROVIDER=openai
# Create orchestrator
orchestrator = Orchestrator()
# Run a query with code execution
result = orchestrator.run("Calculate the first 10 prime numbers")
print(result.final_answer)
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
print(f"Cost: ${result.budget_summary['total_spent_usd']:.4f}")
With Context File
from rlm import Orchestrator
orchestrator = Orchestrator()
# Query against a large document
result = orchestrator.run(
"Find all email addresses mentioned in this document",
context_path="/path/to/large_document.txt"
)
print(result.final_answer)
Direct Sandbox Usage
from rlm import DockerSandbox
sandbox = DockerSandbox()
result = sandbox.execute("""
import math
print(f"Pi = {math.pi}")
print(f"Factorial of 10 = {math.factorial(10)}")
""")
print(result.stdout)
# Pi = 3.141592653589793
# Factorial of 10 = 3628800
Memory-Efficient Context Handling
from rlm import ContextHandle
# Work with large files without loading into memory
with ContextHandle("/path/to/10gb_file.txt") as ctx:
print(f"File size: {ctx.size_mb:.2f} MB")
# Search using regex
matches = ctx.search(r"API_KEY=\w+")
for offset, match in matches:
# Read context around match
snippet = ctx.snippet(offset, window=200)
print(f"Found at {offset}: {snippet}")
โ๏ธ Configuration
RLM is configured via environment variables (with RLM_ prefix) or a .env file:
# API Configuration
RLM_API_PROVIDER=openai # openai, anthropic, google
RLM_API_KEY=sk-... # Your API key
RLM_MODEL_NAME=gpt-4o # Model to use
# Execution Configuration
RLM_EXECUTION_MODE=docker # docker or local (dev only)
RLM_DOCKER_RUNTIME=auto # auto, runsc, or runc
RLM_EXECUTION_TIMEOUT=30 # Seconds
# Safety Limits
RLM_COST_LIMIT_USD=5.0 # Max spending per session
RLM_MAX_RECURSION_DEPTH=5 # Max code execution iterations
# Security Settings
RLM_MEMORY_LIMIT=512m # Container memory limit
RLM_NETWORK_ENABLED=false # Network access (DANGEROUS if true)
RLM_ENTROPY_THRESHOLD=4.5 # Secret detection threshold
๐ Security Architecture
RLM v2.0 implements defense in depth with multiple security layers:
1. Runtime Isolation
- gVisor (runsc) - Intercepts syscalls in userspace, isolating from host kernel
- Automatic fallback - Falls back to runc with seccomp if gVisor unavailable
2. Network Isolation
network_mode="none"- Container has no network access- Prevents data exfiltration via HTTP/DNS
3. Resource Limits
- Memory: 512MB default (prevents OOM attacks)
- PIDs: 50 max (prevents fork bombs)
- CPU: 1 core quota (prevents crypto mining)
4. Egress Filtering
- Shannon Entropy - Detects high-entropy data (secrets, keys)
- Pattern Matching - Recognizes API keys, JWT, private keys
- Context Echo - Prevents printing raw context back
5. Privilege Restrictions
no-new-privileges- Blocks privilege escalationipc_mode="none"- Isolates IPC namespace
๐ API Reference
Orchestrator
class Orchestrator:
def run(query: str, context_path: str = None) -> OrchestratorResult
def chat(message: str) -> str
DockerSandbox
class DockerSandbox:
def execute(code: str, context_mount: str = None) -> ExecutionResult
def validate_security() -> dict
ContextHandle
class ContextHandle:
size: int # File size in bytes
size_mb: float # File size in MB
def search(pattern: str) -> List[Tuple[int, str]]
def read_window(offset: int, radius: int = 500) -> str
def snippet(offset: int, window: int = 500) -> str
def head(n_bytes: int = 1000) -> str
def tail(n_bytes: int = 1000) -> str
def iterate_lines(start_line: int = 1) -> Iterator
EgressFilter
class EgressFilter:
def filter(output: str, raise_on_leak: bool = False) -> str
def check_entropy(text: str) -> Tuple[bool, float]
def check_secrets(text: str) -> List[Tuple[str, str]]
๐งช Testing
# Run all tests
pytest
# Run unit tests only
pytest tests/unit/ -v
# Run security tests (requires Docker)
pytest tests/security/ -v -m security
# With coverage
pytest --cov=rlm --cov-report=html
๐ Project Structure
src/rlm/
โโโ config/ # Pydantic Settings configuration
โโโ core/
โ โโโ memory/ # ContextHandle for large files
โ โโโ repl/ # Docker sandbox execution
โ โโโ exceptions.py
โ โโโ orchestrator.py
โโโ llm/ # Multi-provider LLM clients
โโโ security/ # Egress filtering
โโโ utils/ # Cost management
โโโ prompt_templates/
๐ค Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests.
๐ License
MIT License - see LICENSE for details.
๐ Acknowledgments
Built following the security principles outlined in RFC-002 for hardened LLM code execution.
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_python-2.1.0.tar.gz.
File metadata
- Download URL: rlm_python-2.1.0.tar.gz
- Upload date:
- Size: 42.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83ad19f440846a71900bb202a26d776f840a0bf394335280428ecb1431a2e476
|
|
| MD5 |
35ce3dd54e31341cb978e63d9a42bc72
|
|
| BLAKE2b-256 |
24ae89ae91f5e84f6192020a752986449c20ea28f2d6b8127079f8d87ab36bae
|
File details
Details for the file rlm_python-2.1.0-py3-none-any.whl.
File metadata
- Download URL: rlm_python-2.1.0-py3-none-any.whl
- Upload date:
- Size: 48.6 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 |
c115e9abcb39125b9c676fd2ce76f06b28f5dffe2058bd2a2da47a9023491915
|
|
| MD5 |
4c6196917032535c9c227f80e3a18704
|
|
| BLAKE2b-256 |
3f6645353c6cb70af17736d28826ca4a94d06a9932f7ee39b4b3bbc4bf86b6c5
|