How to Sandbox an AI Agent That Runs Code: Containers, gVisor or Firecracker
Run agent-written code in a sandbox you would trust with hostile code, because injected code is hostile code. Choose the boundary by who shares the host: hardened containers for trusted, single-tenant work; gVisor or Firecracker microVMs for untrusted or multi-tenant code. Then set five defaults whatever the runtime: no network, hard resource limits, a throwaway filesystem, no standing credentials, no root.
On this page
An agent that can run code can do almost anything. Data-analysis agents write Python to answer a question. Coding agents run tests and install packages. Support agents call a "run script" tool someone added for convenience. In each case a model wrote the code, and the model was reading text it did not choose.
That makes agent-written code untrusted code, whoever deployed the agent. The sandbox it runs in decides what that code can reach.
What happens when agent-run code escapes its container
In June 2023 Positive Security published an attack on Auto-GPT that walked the whole path. Text hidden on a web page the agent was asked to summarise got it to execute attacker-supplied code. In the default mode a user approved each command, and the researchers found ways to obtain that approval, including injecting colour-coded messages into the console.
Then came the escape. Auto-GPT ran in Docker, but in self-built versions its docker-compose.yml was mounted inside the container. The malicious code rewrote that file to mount the host's root filesystem and run privileged, then stopped the container. The next start ran the attacker's configuration. The console spoofing and the Docker escape were both fixed in v0.4.3.
The container was there. The boundary was not, because code inside it could write to the file that defined it.
OWASP's Top 10 for Agentic Applications files this class as ASI05 Unexpected Code Execution, whose outcomes, such as container compromise and sandbox escape, "require host and runtime-specific mitigations beyond ordinary tool-use controls". Our post on coding agents with production access covers what such an agent should be able to reach. This one is about the box it runs in.
Containers vs gVisor vs Firecracker: which sandbox for AI agents
Three kinds of boundary are in common use. They differ in what the code talks to when it makes a system call.
Containers use kernel namespaces and control groups, and the code runs on the host's own kernel. NIST's Application Container Security Guide, SP 800-190, states the consequence: "the use of a shared kernel invariably results in a larger inter-object attack surface than seen with hypervisors". A kernel bug reachable from inside is a way out.
User-space kernels such as gVisor put an application kernel, written in Go, between the code and the host. Its Sentry implements system calls itself and "does not pass system calls through to the host kernel"; a separate Gofer process mediates file access. The documentation names the cost: "reduced application compatibility and higher per-system call overhead". It plugs into Docker and Kubernetes as the OCI runtime runsc.
MicroVMs such as Firecracker give each workload its own guest kernel behind KVM, with a deliberately small device model of five emulated devices. Its design document treats guest vCPU threads as "running malicious code as soon as they have been started", and says production deployments should start Firecracker only through its jailer, which adds a second line of defence.
| Containers | gVisor | Firecracker microVM | |
|---|---|---|---|
| The code's system calls reach | The host kernel, filtered | A user-space kernel per sandbox | A guest kernel per microVM |
| Main cost | Shares the host kernel | Compatibility and syscall overhead | KVM hosts, guest images, your own networking |
| Fits | Trusted code, one tenant, hardened | Untrusted code, where compatibility allows | Untrusted code from many tenants |
Two cautions keep the table honest. gVisor's security model warns that "one should not assume that the mere use of virtualization hardware makes a system more or less secure", and that "a sandbox is not a substitute for a secure architecture". A badly configured microVM is still badly configured.
The practical rule: choose the boundary by who wrote the code and who shares the host. If one customer's agent-written code can run on the same kernel as another customer's, the boundary is too weak. Kubernetes lets you choose per workload with RuntimeClass, so sandboxed pods can use a stronger runtime without moving everything else.
How to sandbox an AI agent: five defaults
The runtime decides how hard the walls are. These settings decide what is inside them, whichever runtime you pick.
1. No network by default
Code that cannot open a connection cannot send your data anywhere. Docker's none network driver leaves a container with only a loopback interface. Start there, and add egress only through a proxy with an allowlist of the hosts the task needs.
Firecracker is explicit that this is your job: it "does not perform any network traffic filtering", so guest egress "should be filtered at the host-level". On a cloud VM, block the metadata address too. AWS documents applications on an instance retrieving the role's security credentials from instance metadata; sandboxed code with a route to it can do the same. We cover the other routes in how data leaves an AI agent.
2. Hard resource limits
Docker's documentation says that by default "a container has no resource constraints". Cap memory, CPU, process count (--pids-limit), disk and wall-clock time per execution. A loop the model wrote by mistake should end in a killed sandbox, not a bill that grows overnight.
3. A filesystem that is thrown away
Give each task a fresh sandbox and destroy it afterwards. Mount the root filesystem read-only (--read-only), give the code a tmpfs scratch directory, and mount inputs read-only.
Never mount anything the harness depends on: its configuration, its source or the Docker socket. That was the Auto-GPT escape. And ephemeral is not the same as clean. One of ASI05's scenarios is an agent regenerating a lockfile from unpinned specifications in an ephemeral sandbox and pulling a backdoored version. Pin dependencies, or install from a mirror you control.
4. No standing credentials
The sandbox should start with no secrets: no API keys in environment variables, no cloud credential files, no forwarded SSH agent. If the code needs a service, it should ask a tool outside the sandbox that holds a short-lived, narrowly scoped credential of the agent's own and makes the call. Anything the code can read, the model can be talked into printing.
5. No root, fewer capabilities
Run as an unprivileged user, drop capabilities the code does not need (--cap-drop), never use --privileged, and consider Docker's rootless mode, which runs the daemon and containers as a non-root user. ASI05's execution-environment mitigation opens with four words: "Never run as root."
What a sandbox doesn't do
A sandbox limits what code can touch while it runs. It does not decide whether the result is right, or whether what the code produced should go where the agent sends it next. Output from a sandboxed script returns to the agent's context, then to a reply, a file or another tool call. Treat it with the same suspicion as text that arrived from a web page.
A sandbox checklist for code-executing agents
- Can one tenant's code run on a kernel shared with another tenant's?
- Does the sandbox start with no network, and is every allowed destination written down?
- Is the cloud metadata address unreachable from inside?
- Are memory, CPU, process count, disk and run time all capped?
- Is the sandbox destroyed after each task, with nothing writable that outlives it?
- Are there secrets anywhere inside it?
- Does any code run as root?
Where Syntrox fits
Syntrox sits outside the agent and outside the sandbox its code runs in. Our judge, Nucleus, treats any text inside the material it evaluates as evidence, never as instructions: a comment in generated code telling the evaluator to approve counts against the output. Syntrox records, for each trace it evaluates, the score, confidence and judge version, outside the agent.