gitmyhub

monty

Rust ★ 7.8k updated 13h ago

A minimal, secure Python interpreter written in Rust for use by AI

Monty is an experimental Python interpreter written in Rust that lets AI agents safely run Python code they generate, no filesystem or network access by default, microsecond startup, with hard caps on memory and execution time.

RustPythonsetup: moderatecomplexity 4/5

Monty is an experimental Python interpreter written in Rust, built for one specific scenario: safely running Python code generated by AI agents. The idea is that AI agents can work faster and more reliably when they write and execute small programs to complete tasks, rather than relying on conventional tool calls. Monty provides a controlled environment for that code to run without the startup overhead or complexity of a full container-based sandbox.

Startup time is measured in single-digit microseconds, which is orders of magnitude faster than spinning up a container. Runtime performance is similar to CPython, generally within a 5x factor in either direction. The interpreter blocks all access to the host environment by default: filesystem operations, environment variables, and network access are unavailable unless the developer explicitly exposes them as callable functions. Memory usage, stack depth, and execution time can all be capped to prevent runaway code.

From a developer perspective, Monty can be called from Python, JavaScript/TypeScript, or Rust. You give it a piece of Python code, a list of external functions it may call, and any input variables. When the code calls one of those external functions, execution pauses and control returns to your program. You handle the call in your host language, then resume execution with the result. The interpreter state can also be serialized to bytes at any pause point and stored in a database, then restored later, even in a different process.

What Monty cannot do is as important as what it can: it has no access to most of the Python standard library, no support for third-party packages, and does not yet support class definitions or match statements. It is intentionally narrow in scope. The project is experimental, marked as not ready for production use, and built by the Pydantic team, who plan to use it for a code-execution mode inside Pydantic AI.

Where it fits