explaingit

pydantic/monty

7,162RustAudience · developerComplexity · 4/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((Monty))
    What it does
      Run Python safely
      AI agent sandbox
      Microsecond startup
    Safety features
      No filesystem access
      No network access
      Time and memory caps
    How to call it
      From Python
      From JavaScript
      From Rust
    Limitations
      No stdlib
      No third-party packages
      Experimental only
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Let an AI agent safely execute small Python programs it writes on the fly, without risking access to your filesystem or network.

USE CASE 2

Call Monty from a Python, JavaScript, or Rust host and expose only the specific functions you want the sandboxed code to use.

USE CASE 3

Serialize the interpreter state mid-execution to a database and resume it later in a different process.

USE CASE 4

Set hard limits on memory, stack depth, and execution time to prevent AI-generated code from hanging your server.

Tech stack

RustPython

Getting it running

Difficulty · moderate Time to first run · 30min

Experimental and not production-ready, no Python standard library or third-party package support inside the sandbox.

In plain English

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.

Copy-paste prompts

Prompt 1
I'm building an AI agent that generates Python code to complete tasks. Show me how to use Monty from Python to execute that code safely, expose a custom function, and handle the result.
Prompt 2
How do I set memory and time limits when running code inside Monty so runaway AI-generated scripts cannot hang my server?
Prompt 3
I want to serialize Monty's interpreter state to a database so I can pause execution and resume it later. Show me the Monty API for checkpointing and restoring state.
Prompt 4
What Python features does Monty NOT support? Give me a checklist of limitations I need to know before deciding if it fits my AI agent use case.
Open on GitHub → Explain another repo

← pydantic on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.