Persistent VMs are now available.Learn more

What Is an AI Sandbox? Definition, Isolation and Risks

Engineering
Eli Mernit9 min read
What Is an AI Sandbox? Definition, Isolation and Risks

An AI sandbox is an isolated, disposable environment where an AI agent runs code, shell commands and tools without access to your machine, your network or your production data. Whatever the agent does stays inside, and you delete the sandbox when the task ends. The same name is also used for campus chat tools and for regulatory test programs, but this guide covers the first meaning, the one that agent builders work with.

What an AI sandbox is

An AI sandbox is a short-lived computer that your application creates through an API, hands to a model, and throws away. Three properties separate it from an ordinary server or container:

  • Isolated. Code inside can't read the host's files, see other tenants, or reach networks you haven't allowed.
  • Disposable. Each task or session gets a fresh one. When it ends, the filesystem and every process in it are gone.
  • Programmable. Your code starts it, runs commands, moves files in and out, and stops it, with no person at a terminal.

The underlying technology varies. Some sandboxes are hardened containers, some are microVMs, and some are operating-system rules applied to a local process. Isolation and disposability are what make each of them a sandbox.

The term has two other uses. Some universities and companies call their private chat workspace an "AI sandbox", meaning a safe place for staff to try language models on internal data. Regulators use "AI regulatory sandbox" for a supervised program where companies test AI systems before release, and the EU AI Act requires each member state to set one up. Neither one executes code for an agent.

Why AI agents need a sandbox

Agents need a sandbox because they run code nobody has reviewed. A model writes a script, the agent executes it seconds later, and the output goes back into the conversation. No one reads the script first.

Most of that code is harmless. The problem is the rest, and you can't tell which is which in advance. A model can write a loop that never exits or a command that deletes the wrong directory. A prompt injection hidden in a web page, a document or a tool result can instruct the agent to read your environment variables and post them to an outside server. If the agent runs with the same access as your application, the attacker now has that access too.

A sandbox limits what a mistake or an attack can touch. Four things are at risk when you run agent code: the host it runs on, the secrets and networks it can reach, your compute bill, and the next user's data. The guide to running AI-generated code in a sandbox walks through the controls for each one.

How an AI sandbox works

Every AI sandbox follows the same lifecycle: create it, run code in it, collect the output, destroy it. The agent calls this as a tool, and the model only ever sees the results.

On Beam the whole loop is a few lines of Python:

python
from beam import Image, Sandbox

sb = Sandbox(
    cpu=1,
    memory="1Gi",
    image=Image(python_version="python3.11"),
    block_network=True,     # no outbound connections
    keep_warm_seconds=120,  # lifetime cap for the whole sandbox
).create()

try:
    result = sb.process.run_code("print(sum(range(10)))")
    print(result.exit_code, result.stdout)
finally:
    sb.terminate()

Sandbox(...).create() boots a container from the image and returns a handle. process.run_code() executes Python inside it and returns the exit code, stdout and stderr. terminate() deletes it. Swap the hard-coded string for whatever the model wrote and you have a code-execution tool. The same handle uploads and downloads files, runs shell commands with process.exec(), and exposes ports, and the sandbox docs list the rest.

Two settings in the snippet matter more than the others. block_network=True stops the code from opening outbound connections, and keep_warm_seconds=120 gives the sandbox a two-minute lifetime, so a forgotten sandbox can't run all night.

Containers vs gVisor vs microVMs: how AI sandboxes isolate code

AI sandboxes use one of three isolation layers: a standard container, a user-space kernel such as gVisor, or a microVM. They differ in how much of the host kernel the untrusted code can reach, and that is the main thing to check when you compare providers.

IsolationHow it worksKernel exposureTradeoff
Standard container (runc)Linux namespaces and cgroups on the host kernelCode makes system calls straight to the shared host kernelFastest and most compatible; weakest boundary for untrusted code
User-space kernel (gVisor)An application kernel intercepts system calls and handles them outside the host kernelOnly a narrow set of host calls is reachableMore overhead on system-call-heavy work; not every Linux call is implemented
MicroVM (Firecracker and others)A small virtual machine with its own guest kernel on KVMThe guest kernel is the one the code can attack; the host sits behind the hypervisorNeeds hardware virtualization (KVM) on the host

A standard Docker container is fine for code you wrote or reviewed. For code a model wrote from an untrusted prompt, most teams want one of the other two rows. gVisor describes its own cost as reduced application compatibility and higher overhead per system call. Firecracker, the microVM manager AWS built for Lambda, lists a boot time under 125 ms and less than 5 MiB of memory overhead per VM, which is why per-task VMs are practical at all. The explainer on what a microVM is goes deeper on that row.

Providers pick different defaults. E2B describes each sandbox as a Linux VM. Modal offers a gVisor runtime and a VM runtime. Beam sandboxes run as containers by default, and beta9, the open-source engine underneath, runs sandboxes under gVisor by default. Passing use_vm=True runs a CPU sandbox as a microVM instead. Our explainer on what a microVM is goes deeper on that layer. If you're moving off plain Docker, the Docker sandbox alternatives post compares the options.

What an AI sandbox needs besides isolation

Isolation protects the host, and that covers only one of the four risks. A production sandbox also needs controls for the network, credentials, resources and state.

  • Egress rules. Block outbound traffic, or allow only the hosts the task needs. With open egress, an injected instruction can ship anything the sandbox holds to any server. Beam takes block_network=True or an allow_list of up to 64 hostnames, wildcards, IPs or CIDRs.
  • Credential handling. Keep real keys out of the sandbox. Anything the code can read, it can print. Beam's brokered secrets leave a placeholder in the environment and substitute the real key only in HTTPS headers sent to hosts you approved.
  • Resource limits. Cap CPU, memory, process runtime and the sandbox's total lifetime, so a fork bomb or an endless loop costs you seconds of compute.
  • One sandbox per task. Reusing a sandbox across users leaks files between them. Start a new one, or restore from a clean snapshot.

An evaluation checklist for a provider is short: which isolation layer, what egress controls, how secrets are handled, what the limits are, and how fast a fresh sandbox starts.

How AI agents escape sandboxes

An agent can get out of a sandbox by defeating the isolation layer, but the easier routes go through what the sandbox was allowed to do: a network it could reach, a credential it could read, or a host directory someone mounted into it.

The routes fall into four groups:

  • Kernel and runtime bugs. A container shares the host kernel, so a kernel vulnerability or a flaw in the container runtime can let code reach the host. This is the classic container escape, and it's the reason gVisor and microVMs exist.
  • Misconfiguration. A privileged container, a mounted Docker socket, or a writable host path gives the code a direct line to the host. No exploit is needed.
  • Allowed paths. The sandbox has internet access and a real API key in an environment variable. A prompt injection tells the agent to send the key somewhere. Nothing escaped, and the data is still gone.
  • Inbound exposure. A preview URL or an open port serves whatever the code started. On Beam, URLs from expose_port() are public HTTPS endpoints, so the app behind them needs its own authentication.

Local agent sandboxes add a fifth route: the agent asks for permission to run outside the sandbox, and a tired developer clicks yes. The Claude Code sandbox guide covers how to run unattended coding agents safely.

For design, that means the isolation layer is only the starting point. Block egress by default, pass no real secrets, and assume anything inside the sandbox can be read by whoever controls the prompt.

Stateless vs stateful AI sandboxes

A stateless sandbox starts clean for every call and keeps nothing. A stateful sandbox keeps its files, installed packages and sometimes its running processes between steps, so an agent can work across many turns.

Stateless is the safer default for one-off tool calls such as "run this analysis and return the chart". Stateful suits coding agents and long tasks, where reinstalling dependencies on every step would waste minutes.

Snapshots sit between the two. On Beam, create_image_from_filesystem() saves a sandbox's disk as an image that new sandboxes boot from, and snapshot_memory() captures running processes so create_from_memory_snapshot() can start copies of a live environment. You get a warm starting point for each task without sharing one long-lived machine. The roundup of stateful sandboxes for code execution compares how providers handle this.

Managed vs self-hosted AI sandboxes

A managed AI sandbox is an API: you send code and the provider runs the isolation, scheduling and cleanup. A self-hosted one runs the same stack on machines you operate, which keeps data in your own account and adds the operational work.

Managed is the faster route for most teams, since safe multi-tenant isolation takes real effort to build and patch. Self-hosting fits when task data can't leave your cloud, or when you already run a large cluster. Beam offers both routes: a hosted API, and beta9 as an open-source engine you can run yourself. The guide to self-hosting a code execution sandbox covers the work involved, and the list of E2B alternatives compares the managed providers.

FAQ

What is the difference between an AI sandbox and a Docker container?

A Docker container is a packaging and process-isolation tool that shares the host kernel and allows outbound network traffic by default. An AI sandbox adds a stronger boundary such as gVisor or a microVM, network and credential controls, time limits, and an API to create and destroy it per task. Many AI sandboxes still use container images to define what's installed.

Is an AI sandbox the same as a virtual machine?

No. A virtual machine is one way to build a sandbox, and a persistent VM is a different product from a disposable one. A sandbox is defined by being isolated, short-lived and API-driven. A regular VM keeps its disk and identity until you delete it, which suits a long-running agent workspace more than a single tool call.

How fast does an AI sandbox start?

It depends on the isolation layer and the image. Firecracker lists a microVM boot time under 125 ms, and a container has no kernel to boot, so its start time is mostly pulling the image and starting the process. A large image with many packages takes longer the first time. Restoring from a snapshot skips the install steps on later runs.

How much does an AI sandbox cost?

Most providers bill for CPU and memory while the sandbox runs. On Beam, a sandbox with 1 vCPU and 1 GiB of memory costs $0.06 an hour on the Performance tier, which is $0.05 per vCPU-hour plus $0.01 per GiB-hour, billed by the millisecond. A tool call that runs for two minutes at that size costs $0.002.

Can an AI sandbox use a GPU?

Some can. On Beam, pass gpu="RTX4090" when you create the sandbox and include the CUDA libraries in the image. GPU sandboxes use the default container runtime, because microVM sandboxes are CPU-only for now.

Can code in an AI sandbox access the internet?

Only if you allow it. A sandbox can block all outbound traffic or allow a list of hosts, such as a package index and one API. Blocking by default is the safer choice, because open egress is how leaked data leaves.

Does an AI sandbox prevent every risk from AI-generated code?

No. A sandbox contains what code can do on the machine it runs on. It doesn't stop the agent from misusing access you granted, such as an allowed API or a real key in the environment. Pair it with narrow egress rules, brokered credentials, and a review step for actions that change production systems.

Run agent code in a Beam sandbox

Create a sandbox, run the model's code with the network blocked, and delete it when the call returns. Start with a Beam sandbox and use the example at the top of this page as your agent's first tool.

Eli MernitPublished November 17, 2026

Pay as you go
billed by the second

Give your agents their own machines

Sign up, install the CLI, and launch a sandbox, VM, or GPU from your terminal or your coding agent.