Let an AI agent safely execute small Python programs it writes on the fly, without risking access to your filesystem or network.
Call Monty from a Python, JavaScript, or Rust host and expose only the specific functions you want the sandboxed code to use.
Serialize the interpreter state mid-execution to a database and resume it later in a different process.
Set hard limits on memory, stack depth, and execution time to prevent AI-generated code from hanging your server.
Experimental and not production-ready, no Python standard library or third-party package support inside the sandbox.
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.
← pydantic on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.