TL;DR
Python lacks a reliable, language-level sandbox because its runtime is highly introspective and mutable, allowing many containment attempts to be bypassed. Infrastructure-level isolation — using microVMs, container sandboxes, or emerging WebAssembly approaches — is the practical path forward for securing AI agents that run untrusted code.
What happened
A recent technical note explains that Python does not provide a dependable built-in mechanism for running untrusted code. Because Python’s object model, frames and tracebacks expose core interpreter internals, attempts to remove dangerous operations or restrict built-ins can still be circumvented through introspection and frame inspection. Historically, projects that aimed at language-level sandboxes failed, and OS-level approaches such as containerization or virtual machines became the default strategy. The author reviews current isolation options: Firecracker microVMs and container runtimes like Docker at the agent level, gVisor for finer-grained task isolation, and an emerging option — WebAssembly (WASM) — for low-overhead sandboxing. The note highlights security incidents in 2025, including prompt-injection attacks and implementation flaws in protocols like MCP, to argue that isolation and least-privilege access are necessary design principles for AI agents. The author also mentions developing an open-source WASM-based task decorator to sandbox individual agent tasks.
Why it matters
- AI agents increasingly execute untrusted or external code, raising real security risks beyond resource management.
- Prompt injection and architectural flaws in LLM systems can lead to data exposure if agents have broad access.
- Language-level controls in Python are insufficient; infrastructure-level isolation and least-privilege are required to protect users.
- Choosing the right isolation granularity affects both security and the agent’s ability to access needed resources.
Key facts
- Python has no built-in safe execution sandbox; its introspective, mutable runtime enables bypasses of many restrictions.
- Simple mitigations like removing built-ins can be defeated via object graph traversal and frame/traceback access.
- Older sandbox projects provided OS-level isolation rather than true language-level containment.
- Agent-level isolation tools discussed include Firecracker (microVM) and Docker; Firecracker requires KVM and is Linux-only.
- Task-level isolation commonly uses gVisor, which intercepts and reimplements Linux system calls and is also Linux-only.
- WebAssembly (WASM) offers a low-privilege runtime by default — no filesystem, network, or env access unless explicitly granted.
- WASM’s current limitations include evolving support for C extensions, which affects many ML libraries.
- The author argues isolation and least-privilege are preferable to relying on prompt-based defenses and is building an open-source WASM task sandbox available on GitHub.
- 2025 saw examples of prompt injection and protocol implementation flaws (MCP/SQLite) that increased focus on isolation for agent security.
What to watch next
- Whether WebAssembly runtimes mature to support C extensions and mainstream ML libraries — not confirmed in the source
- Adoption rates of task-level isolation patterns (e.g., gVisor or WASM decorators) across agent frameworks — not confirmed in the source
- Progress and adoption of the author’s open-source WASM-based task sandbox (project on GitHub)
Quick glossary
- Sandboxing: Running code in a restricted environment that limits its access to system resources and sensitive data.
- WebAssembly (WASM): A portable binary instruction format and runtime designed to run code with restricted privileges in a compact sandboxed environment.
- Firecracker: A microVM technology that creates lightweight virtual machines to isolate workloads at the OS level.
- gVisor: A user-space kernel that intercepts and handles system calls to provide stronger isolation between containers and the host OS.
- Prompt injection: An attack that embeds malicious instructions into input or context so an LLM treats them as legitimate directives.
Reader FAQ
Can Python be sandboxed safely using built-in language features?
No. The source explains Python’s introspection and mutable runtime allow many containment attempts to be bypassed.
Are containers like Docker sufficient to protect against untrusted code?
Containers provide OS-level isolation, but the note cautions they are common rather than the most secure; microVMs or gVisor are presented as stronger alternatives.
Is WebAssembly ready to run ML workloads securely?
WASM offers promising low-privilege isolation and supports pure Python, but support for C extensions and many ML libraries is still evolving and not yet fully working.
Can prompt engineering stop prompt injection attacks?
The source argues that prompt-focused defenses are insufficient and that isolation and least-privilege controls are necessary.

Sandboxing Untrusted Python Python doesn't have a built-in way to run untrusted code safely. Multiple attempts have been made, but none really succeeded. Why? Because Python is a highly introspective…
Sources
- Sandboxing Untrusted Python
- AI agents need isolation. Python wasn't built for that.
- Why AI Agents Need Isolated Code Execution – Hopx
- The Glass Sandbox – The Complexity of Python Sandboxing
Related posts
- Web Developer Posts Public Payment-Demand Notice on Client’s Website
- Calibrating My Turtle: One User’s Ongoing Tension with Coding Agents
- Palo Alto Linked to Possible $400M Bid for Israeli Startup Koi — Unconfirmed