⚡️Monty: the ultrafast Python interpreter by Agents for Agents — Samuel Colvin, Pydantic

Sam Khavari, the creator of Pydantic, introduces Monty, a new, secure, and high-performance Python interpreter written in Rust. Monty is designed specifically for AI agents, bridging the gap between simple, limited tool-calling and complex, slow, full-featured sandboxes.

4 min read
⚡️Monty: the ultrafast Python interpreter by Agents for Agents — Samuel Colvin, Pydantic
source: https://www.youtube.com/watch?v=nxnQl4AcqFg

Sam Khavari from Pydantic has launched Monty, a novel Python interpreter designed from the ground up in Rust to provide a secure, low-latency execution environment for AI agents. It aims to fill a critical gap between simple, safe tool-calling (which is often not expressive enough for Large Language Models) and full sandboxes (which are powerful but suffer from high latency, setup complexity, and are often unsuitable for enterprise self-hosting).

The Niche for a New Interpreter

The inspiration for Monty came from multiple conversations with engineers at Anthropic, who stressed the importance of type safety for reliable AI tool-calling chains. This, combined with the rise of “code mode” execution (popularized by companies like Cloudflare), revealed a significant need. An investor in the sandboxing space estimated that around 70% of sandbox invocations are for “glorified tool-calling”—tasks like rendering a chart or performing a calculation, which don’t require a full-fledged compute environment but benefit greatly from the expressiveness of code.

Traditional sandboxing solutions present several challenges:

  • Latency: Creating or resuming a sandbox can take seconds (e.g., 1-2.8 seconds for Pyodide in Deno or Daytona), which is too slow for interactive agentic workflows. Monty, running inside the same process, can execute code in single-digit microseconds.
  • Complexity & Security: Solutions like Pyodide, while powerful, present security risks when used outside the browser. Running it in Node.js allows access to the host system, and even with Deno’s restrictions, issues like memory exhaustion attacks and process tainting remain, forcing a costly restart for every invocation.
  • Enterprise Adoption: Self-hosting complex sandboxing infrastructure is a non-starter for many large financial institutions and other enterprises with strict security requirements.

Monty solves these issues by being a single, lightweight Rust binary that can be easily installed and embedded anywhere Rust can run, offering fine-grained control over resources like memory and execution time.

How Monty Works: Trade-offs and Solutions

The biggest trade-off with Monty is that it is not a full CPython implementation. It is a custom interpreter, and as such, it does not support installing third-party CPython libraries like NumPy or even Pydantic directly within its runtime.

However, it overcomes this limitation through a powerful mechanism: registering host functions. You can expose Python functions from your host application to the Monty runtime. This allows the LLM to write code that calls these registered functions, effectively giving it access to any capability you wish to provide—be it making a network request with httpx, parsing HTML with BeautifulSoup, or performing data validation with Pydantic.

In a demo, Khavari showed an agent scraping a website. The agent was given access to functions that wrapped Playwright and BeautifulSoup. The code generated by the LLM ran inside Monty, calling out to these host functions to control a browser and parse its content. Monty also includes a built-in type checker, which provides immediate feedback to the LLM if its generated code has type errors, allowing it to self-correct.

Building an Interpreter with AI

A fascinating aspect of Monty’s development is how heavily it relied on AI. Khavari used LLMs to implement the majority of Python’s built-in functions and standard library modules. He identified four key conditions under which an LLM can be “100x faster” than a human developer:

  1. Well-Known Internal Implementation: The model already knows how to implement concepts like a bytecode interpreter.
  2. Well-Known External API: The target API (the behavior of Python) is deeply embedded in the model’s training data.
  3. Simple Unit Testing: The test for correctness is simply “does it produce the exact same output as CPython?”
  4. No “Bike-Shedding”: There are no debates about API design or error messages; the standard is already set by Python.

This AI-assisted development has allowed Monty to rapidly implement complex modules like datetime and asyncio.

The Future: Self-Improving and Serializable Agents

While Monty itself is open-source, the commercial strategy revolves around services built on top of it, primarily through Pydantic’s observability platform, Logfire. A key concept is the self-improving agent. After an agent successfully completes a complex task (which might involve several trial-and-error steps), it can be prompted to return the single, optimized piece of Python code that performs the task directly.

This optimized code can then be stored and used for future invocations, drastically reducing latency and cost. Logfire aims to be the platform that manages this “agent optimization” layer—storing optimized code, model settings, and system prompts to enhance agent performance over time.

This vision ties into an upcoming Pydantic AI feature: Serializable Agents. The goal is to define an entire agent—its model, system prompt, tools, and capabilities (like code mode powered by Monty)—in a single, portable TOML file. This would make agents easier to manage, deploy, and optimize, with Monty providing the secure, sandboxed execution core.

Share post

Stay In The Loop! Subscribe to Our Newsletter.

Get updates straight to your inbox. No spam, just useful content.

Recommended for You

Production Ready AI Agents

Production Ready AI Agents

Sam Partee, CTO of Arcade, explains the critical gap between AI agents that gather context and those that take secure, real-world actions. He introduces Arcade as a middleware platform that solves complex challenges like user authorization, fine-grained permissions, and token management, enabling developers to build scalable, enterprise-ready agents.

Enterprise-ready MCP // Jiquan Ngiam

Enterprise-ready MCP // Jiquan Ngiam

Jiquan Ngiam, CEO of MintMCP, discusses the paradigm shift from static programs to dynamic AI agents, outlining the significant security risks involved—supply chain vulnerabilities, third-party data poisoning, and inadvertent agent behaviors—and presents a three-pronged strategy for enterprise readiness: comprehensive monitoring, preventative guardrails, and secure, role-based deployment of Model Context Protocols (MCPs).