Skip to main content

A lightweight, embeddable Python sandbox for LLM tool execution

Project description

Littrs Logo

Littrs

A lightweight, embeddable Python sandbox for LLM tool execution.

PyPI version Crates.io License CI GitHub stars


Littrs is a Python sandbox that you embed directly into your Rust or Python application. There's no container to start, no runtime to boot, no network call to make — just a library that executes LLM-generated Python safely, with only the tools you give it.

It was built for a specific workflow: an LLM writes Python code that calls your functions, and you need to run that code without giving it access to anything else. Littrs compiles Python to bytecode and runs it on a stack-based VM with zero ambient capabilities. The only way sandboxed code can interact with the outside world is through tools you explicitly register.

Installation

pip install littrs

Quick Start

from littrs import Sandbox

sandbox = Sandbox()

@sandbox.tool
def get_weather(city: str, units: str = "celsius") -> dict:
    """Get current weather for a city."""
    return {"city": city, "temp": 22, "units": units}

result = sandbox("get_weather('London')")
# result == {"city": "London", "temp": 22, "units": "celsius"}

The @sandbox.tool decorator registers your function with its full signature — the LLM code calls it like a normal Python function. The sandbox is also callable: sandbox(code) is shorthand for sandbox.run(code).

Variables persist across calls, and you can inject values directly:

sandbox["user_id"] = 42
sandbox("name = get_weather('London')['city']")
sandbox("name")  # "London"

Resource Limits

Prevent runaway code from consuming unbounded resources:

sandbox.limit(max_instructions=10_000, max_recursion_depth=50)

try:
    sandbox.run("while True: pass")
except RuntimeError as e:
    print(e)  # "Instruction limit exceeded (limit: 10000)"

Resource limit errors are uncatchabletry/except in the sandbox code cannot suppress them. This is by design: the host must always be able to regain control.

Capturing Print Output

capture() returns both the result and everything that was print()-ed:

result, printed = sandbox.capture("""
for i in range(5):
    print(i)
"done"
""")
# result  == "done"
# printed == ["0", "1", "2", "3", "4"]

Tool Documentation for LLM Prompts

describe() auto-generates Python-style signatures and docstrings from registered tools, ready to embed in a system prompt:

print(sandbox.describe())
# def get_weather(city: str, units: str = 'celsius') -> dict:
#     """Get current weather for a city."""

Low-level Registration

If you need to bypass the decorator (e.g. registering a function that takes raw positional args):

def fetch_data(args):
    return {"id": args[0], "name": "Example"}

sandbox.register("fetch_data", fetch_data)

File Mounting

Mount host files into the sandbox so LLM-generated code can read input and write output without full filesystem access:

sandbox.mount("data.json", "./data/input.json")                    # read-only (default)
sandbox.mount("output.txt", "./output/result.txt", writable=True)  # read-write

result = sandbox("""
f = open("data.json")
data = f.read()
f.close()

f = open("output.txt", "w")
f.write("processed: " + data)
f.close()
""")

# Inspect written files from the host
sandbox.files()  # {"output.txt": "processed: ..."}

Unmounted paths raise FileNotFoundError; writing to read-only mounts raises PermissionError. Both are catchable with try/except inside the sandbox.

WASM Sandbox (Stronger Isolation)

For stronger isolation, Littrs can run the interpreter inside a WebAssembly guest module with memory isolation and fuel-based computation limits:

from littrs import WasmSandbox, WasmSandboxConfig

config = WasmSandboxConfig().with_fuel(1_000_000).with_max_memory(32 * 1024 * 1024)
sandbox = WasmSandbox(config)

result = sandbox.run("sum(range(100))")
assert result == 4950

Littrs does not support third-party packages, classes, closures, async/await, finally, or match. See the full list of supported Python features.

Citation

If you use Littrs in your research, please cite it as:

@software{littrs,
  title = {Littrs: A Minimal, Secure Python Sandbox for AI Agents},
  author = {Chonkie Inc.},
  url = {https://github.com/chonkie-inc/littrs},
  license = {Apache-2.0},
  year = {2025}
}

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

littrs-0.6.0-cp312-cp312-win_amd64.whl (7.8 MB view details)

Uploaded CPython 3.12Windows x86-64

littrs-0.6.0-cp312-cp312-manylinux_2_34_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

littrs-0.6.0-cp312-cp312-macosx_11_0_arm64.whl (7.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file littrs-0.6.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: littrs-0.6.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 7.8 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for littrs-0.6.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0f0c7040755a599731db93ab86726b52d7833d1fd221c9666e43ee39058cd100
MD5 bda4d049c31765b85fa6b3e152e2bca3
BLAKE2b-256 e0e83e19224674f2bcdcbfa67a4c989e3fc05d7808dbbbe18fcee54048b498a7

See more details on using hashes here.

File details

Details for the file littrs-0.6.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for littrs-0.6.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 6b2e841b22452386b9698fc98c25082b1e7d553d51fa05ee61fd36cc7bf6958d
MD5 32c44d9e8dc42196042953b12be4301f
BLAKE2b-256 bca74842e30f89a8bfaa529114ab21c05443b886620e42702ed5263a2bc6ef99

See more details on using hashes here.

File details

Details for the file littrs-0.6.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for littrs-0.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c03f484071874e04ad2c04485a1383fe0a87bf3806bdc4a0b61bcc55d13a524d
MD5 77572e15aa7b62d5540f0cb6f599f3bd
BLAKE2b-256 9789ebb61ee4bbac250ff97cdb2d194a6aa6340ae3b6a49c9af0504a77f667c8

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