What is an AI agent sandbox?
An AI agent sandbox is an isolated, short-lived environment where an AI agent runs the code it writes, installs packages or uses tools, without access to the host, other users’ data or the credentials of the application that called it. It combines a stronger boundary than a standard container, usually gVisor or a microVM, with a locked-down network and a pool of pre-started environments, so a session does not wait for a cold start. Code interpreters, coding agents and data analysis features in chat products all depend on one.
Why agent code is untrusted
Code an agent generates is untrusted by definition, even when your own application asked for it. The model can write destructive or wasteful code by mistake. Worse, prompt injection lets anyone who controls the agent’s input steer it: a web page, an uploaded file or a tool result can contain instructions that the model follows. Treat every sandbox as if an attacker wrote the code inside it.
| Risk | Example | Control |
|---|---|---|
| Escape to the node | A kernel or container runtime bug gives the code root on the host | Sandboxed runtime: gVisor or a microVM |
| Secret exfiltration | Code reads environment variables, a mounted service account token or the cloud metadata endpoint, then sends them out | No credentials in the sandbox, egress allowlist |
| Lateral movement | Code calls internal services, databases or the Kubernetes API | Default-deny network policy, authenticated internal services |
| Resource abuse | Infinite loops, fork bombs, crypto-mining | CPU, memory, disk and time limits |
| Data leaking between users | A second user’s session sees files from the first | One sandbox per session, destroyed afterward |
What the isolation boundary is
A standard container shares the node’s kernel with every other container, so one kernel or runtime bug can break isolation. For agent code, the usual choices are:
| Runtime | Boundary | Trade-off |
|---|---|---|
Container (runc) | Namespaces and cgroups on the shared host kernel | Fastest and fully compatible; not a security boundary for hostile code |
| gVisor | A user-space kernel answers system calls | Runs on ordinary cloud VMs; slower for system-call-heavy work |
| microVM (Kata Containers, Firecracker) | Hardware virtualization with a guest kernel per sandbox | Strongest boundary; needs bare metal or nested virtualization |
Hosted sandbox services use the same building blocks. E2B runs each sandbox as a Firecracker microVM with its own kernel, and Modal isolates its sandboxes with either gVisor or a KVM virtual machine. How the runtimes work, their compatibility limits and how to wire them into Kubernetes with a RuntimeClass is covered in running untrusted code with gVisor, Kata or Firecracker.
Startup time vs isolation
Agents call tools in a loop, and a user waits on every call. A new Kubernetes Pod needs scheduling, an image pull and container start, which takes seconds, and a microVM adds a guest kernel boot on top. The Firecracker project states boot times under 125 ms for the VM itself, but the image and runtime inside still have to start.
Sandbox platforms solve this with warm pools: they keep a number of sandboxes started and idle, and hand one out when a session begins. The pool costs capacity while it waits, so size it to expected concurrency and let node autoscaling grow and shrink the nodes beneath it. Many agent sessions are also stateful. A Python kernel that keeps variables between calls lets the agent build on earlier results, so a session keeps one sandbox until it ends, and then the sandbox is destroyed rather than reused.
Network egress control
Most damage from a compromised sandbox needs the network. Start from default deny and add only what the agent needs:
- Allow DNS and the specific backend endpoint the sandbox reports results to.
- Block the cloud metadata address
169.254.169.254, the Kubernetes API server and other Pods. - If the agent must install packages or fetch web pages, route traffic through an egress proxy that allows named domains, such as your package mirror.
- Require authentication on every internal service a sandbox can reach. A network policy cannot tell your runtime’s own traffic from the user code running next to it.
Kubernetes network policies explains default deny and how to allow single destinations. Inside the sandbox, mount no service account token, run as a non-root user, drop all Linux capabilities and use a read-only root filesystem with a small writable scratch volume.
Kubernetes resources for sandboxes
Agent Sandbox is an open-source Kubernetes project under SIG Apps that adds a declarative API for sandboxes. Version 1.0.0 was released on August 28, 2026. It defines four resources:
| Resource | Purpose |
|---|---|
Sandbox | A single stateful Pod with a stable hostname, optional persistent storage, and pause and resume |
SandboxTemplate | A reusable blueprint: image, resources, security context and runtime class |
SandboxWarmPool | Pre-started sandboxes ready to hand out |
SandboxClaim | A request that binds to a sandbox from a warm pool |
The project delegates isolation to the runtime selected in the template, such as gVisor or Kata Containers. That separates the sandbox lifecycle from the isolation choice, so a team can run trusted internal tools on the standard runtime and user-facing agents in microVMs.
Hosted or self-hosted
A hosted sandbox service is the fastest way to start: an API call returns a running sandbox, and the provider handles isolation and capacity. Running sandboxes on your own Kubernetes cluster keeps agent code and the data it touches inside your infrastructure and region, which matters when the agent works on customer data. It also replaces a per-sandbox-hour price with capacity you already pay for. GPU-backed agents and agents that work on large datasets usually favor running next to the data. For multi-tenant setups beyond sandboxes, see Kubernetes multi-tenancy.
Agent sandboxes on Cloudfleet
Cloudfleet documents how to run Agent Sandbox on Cloudfleet Kubernetes Engine (CFKE), with warm pools on cloud, bare-metal or on-premises nodes and node auto-provisioning that scales the nodes with the pool. Kata Containers is available as a managed runtime when sandboxes need a VM boundary.