Skip to content
← all posts

Why do AI agents need a sandbox?

How isolated runtimes stop untrusted agent code from breaking your host, leaking secrets, or colliding across runs

Cocona L5 min read

Why do AI agents need a sandbox?

An AI agent sandbox is an isolated compute environment where an agent can write files, install packages, and run shell commands without touching your host machine or production data. AI agents need a sandbox because they generate and execute code dynamically at runtime, which means a mistake, prompt injection, or bad dependency can corrupt the surrounding system if the code runs directly on your host.

Traditional software runs a fixed binary that engineers reviewed before deployment. Coding and tool-using agents work differently. You give an agent a goal, and the model writes a Python script, edits a config file, runs pip install, or executes shell commands on the fly to see what works. That trial-and-error loop is useful, and it also puts arbitrary, unreviewed code on whatever machine hosts the agent.

What breaks when agents run on your host

Running agent-generated commands directly on your laptop or application server creates four practical risks.

First, an agent can read secrets it was never meant to see. Any process running as your user can usually read environment variables, ~/.ssh keys, cloud CLI tokens, or .env files in nearby folders. A single env dump or a prompt injection hidden inside a fetched web page can expose those credentials.

Second, agents make destructive mistakes. When an agent tries to clean up temporary files, fix a broken build, or reset a database, a wrong path or flag can wipe project files outside its working folder.

Third, package changes pollute future runs. If one agent task upgrades numpy or modifies system libraries globally, the next task inherits a dirty environment and fails for reasons unrelated to the prompt.

Fourth, runaway loops exhaust the host. An agent testing a recursive script or launching background servers can fill up disk space, eat all available RAM, or leave orphan processes listening on ports.

What a sandbox gives an AI agent

When you give an agent a dedicated sandbox, you define the outcome you want first, run the agent's tools inside a controlled boundary, and get back clean artifacts or a running app without risking the host.

Every run gets its own filesystem, process tree, and controlled network access. If the agent installs three Python libraries, crashes a test script, and deletes half its working directory, only that sandbox changes. When the task finishes, you can keep the workspace state for the next turn or throw the environment away and start fresh.

You can also enforce resource limits at the boundary. Capping CPU, memory, disk quota, and outbound network destinations stops one bad generation from taking down your API server or other users' sessions.

Isolation options compared

Teams usually choose between four isolation layers depending on whether the code is trusted, multi-tenant, or long-lived.

ApproachFilesystem and packagesKernel boundaryBest fit
Host process or venvShared host filesystem, partial Python isolationShared host kernel and user permissionsLocal prototyping with code you wrote yourself
Standard OCI containerIsolated root filesystem per containerShared host Linux kernelTrusted services and predictable CI steps
MicroVM or isolated runtimeSeparate root filesystem and virtualized devicesDedicated guest kernel per sandboxUntrusted LLM-generated code and multi-tenant agents
Managed agent cloudEphemeral or stateful remote sandbox per sessionIsolated compute managed off your hostProduction agent apps that run tools, keep state, and expose preview services

A quick way to verify what your current environment exposes is to check what a shell command can see from inside the agent's process:

# Run inside a test environment to audit visible host paths and env vars
pwd
ls -la ~
printenv | cut -d= -f1

If that command prints your real home directory, SSH folder, or production API keys, your agent is running too close to the host.

Common questions

Is a Python virtualenv enough for an AI agent?

No. A venv only changes which Python interpreter and packages a script imports. Any script running inside that venv still shares your OS user permissions, network access, and host filesystem, so it can still read ~/.aws, run rm outside the repo, or spawn system processes.

When is a standard Docker container enough?

Standard Docker containers work well when you run your own code or single-tenant workflows on a dedicated machine and keep tight flags on mounts, capabilities, and network access. Because containers share the host Linux kernel, teams running arbitrary, untrusted agent code from external users often step up to microVM-level isolation or a dedicated agent runtime. (For a refresher on how containers work and where kernel sharing stops, see our Docker 101 guide.)

How does BoxLite fit in?

BoxLite (Polygala, Inc.) builds two options depending on whether you want to host the infrastructure yourself or use a managed service. The BoxLite open-source embeddable runtime lets you run and manage isolated environments inside your own stack. BoxLite Cloud provides a managed isolated compute environment so AI agents can run code, use tools, keep state, and publish apps as services.