AI Agent Sandbox: What It Isolates and What It Doesn't
A sandbox keeps an agent's code away from your machines. What the agent does in your email, customer and payment systems happens outside it.
Jeet Adeshara- 10 min read
Key takeaways
- An AI agent sandbox is an isolated environment where an agent runs code and commands. Set up well, it has its own files, limited network access and none of the host computer's passwords or keys.
- A sandbox covers the code an agent runs. The agent's tool calls to email, customer records or payment systems run outside it, so they need tool permissions and human approval.
- Anything you put inside the sandbox is within the code's reach: files, secrets and open network routes. Give each sandbox only what its job needs.
- The less you trust the code, the stronger the isolation should be. Code shaped by outside input, such as web pages or uploaded files, should run behind a separate kernel, such as gVisor or a virtual machine.
- To test an agent's actions without real effects, use a test copy of your systems and saved stand-in replies for its tool calls, and refuse any call that has no stand-in.
Every AI agent should run its code in a sandbox, says the security lead. The operations lead takes that to mean the agent can no longer email the wrong customer. It still can.
A sandbox is a sealed-off computer environment where an agent's code runs, away from your own servers and files. Claude Code is Anthropic's coding agent. As of December 2026, its documentation states its sandbox's limit plainly: "The sandbox covers shell commands only." Shell commands are the typed instructions that run programs.
Everything else the agent does goes through its tools: the connections it uses to read and change other systems, such as email or a customer database. Those actions happen outside the sandbox. They need their own controls: permissions on each tool and human approval for the risky ones.
What is an AI agent sandbox, and what does it isolate?
An AI agent sandbox is an isolated environment where an agent runs code and commands. Set up well, it has its own files, limited network access and none of the host's credentials (passwords and keys). It isolates the code the agent runs. It doesn't isolate the agent's tool calls: requests that send email, change records or move money in real systems.
In one line:
A sandbox limits what the agent's code can reach, not what its tools can do.
Agents need a sandbox once they write code of their own: to analyse a spreadsheet, convert a file or work through a calculation. That code can be wrong, or it can follow instructions hidden in a document. The sandbox decides how far it can reach. A default sandbox may reach further than you expect: Claude Code's can read credential files, and OpenAI's local one on macOS has no network isolation (more below).
What runs inside the sandbox, and what runs outside it?
Where the line sits

- The code runs inside. Whatever the agent writes runs in the sandbox, not on your servers.
- Files inside are reachable. The code can read whatever files are copied in or mounted.
- Secrets inside are reachable. Any password or key placed in the sandbox can be read by the code.
- Open network routes are reachable. If the sandbox can reach the internet, so can the code.
- Tool calls run outside. Requests to email, customer records or payment systems go through the agent's tools, and the sandbox never sees them.
Here is a typical case. A customer uploads a spreadsheet, and a hidden line in it tells the agent to send its contents to an outside address. If the agent writes code to do that, a closed network stops it. If a cloud key sits inside the sandbox and the network is open, the key leaves too. Now you have a key to replace and a data leak to explain. If the agent uses its email tool instead, the sandbox plays no part: only the tool's permissions or a person's approval can stop the message.
Common risks on each side
| Risk | Does the sandbox stop it? | What stops it |
|---|---|---|
| Code deletes files on the host | Yes, if no host folders are mounted | The sandbox's own file system |
| Code runs forever or uses all the memory | Yes, with limits set | Time and memory limits |
| Code reads a secret placed in the sandbox | No | Put fewer secrets inside |
| Code sends data to an outside address | Only if the network is closed | Network limits |
| Agent emails the wrong customer | No | AI agent permissions and approval |
| Agent refunds money it shouldn't | No | Human approval for high-impact actions |
The bottom two rows are what OWASP, the open security community, calls excessive agency in its 2025 list of risks for AI applications. Its first fix is to limit the tools an agent can call "to only the minimum necessary".
How isolated does an AI agent sandbox environment need to be?
Sandboxes range from light to heavy, and the right one depends on how much you trust the code. A local sandbox on a laptop can be very thin. As of December 2026, the OpenAI Agents SDK reference says its local Linux sandbox adds "no OS-level command confinement". On macOS, its sessions restrict files "but do not provide network isolation".

- Code your team wrote and reviewed. Time and memory limits on each run are often enough.
- Code the agent writes for a trusted user. Use a container: its own files, no host folders, and only the network it needs.
- Code shaped by outside input. Web pages, emails and uploaded files can carry hidden instructions. Run this code behind a separate kernel, the core of the operating system: gVisor or a virtual machine.
OpenAI's advice for such code is a properly set up container or hosted sandbox "or provide external isolation". gVisor goes further: it gives each sandbox its own "application kernel", which handles the code's requests instead of the host's kernel. That lowers the risk of code breaking out of the sandbox. Kubernetes' Agent Sandbox project supports gVisor and Kata Containers. Kata gives each sandbox its own kernel using the processor's own isolation features.
What does stronger isolation cost?
Each step up costs something in start-up time, running cost or effort. The table compares each option with an ordinary container, as of December 2026.
| Option | Start-up | Running cost | Effort to run it |
|---|---|---|---|
| Container | The baseline | The baseline | Your team already knows it |
| gVisor | Little extra: in gVisor's own test, Docker caused most of the added start-up time | Slower for code that makes many requests to the operating system; a "small, mostly fixed" amount of extra memory | Swap the software that starts containers on the servers you already have |
| Small virtual machine (Firecracker, an open-source tool from AWS) | Under 125 milliseconds | Under 5 MiB (about 5 megabytes) of extra memory per machine | Servers whose processors support virtual machines, and someone to run them |
| Hosted sandbox service (E2B as an example) | Handled by the provider | About 8 US cents an hour for 1 processor core and about 2 GB of memory, at its listed per-second rates, plus any plan fee | No servers to run; your files and secrets go to another company |
The figures come from gVisor's performance guide, the Firecracker project and E2B's price list.
What should you put inside a sandbox for AI agents?
An escape is the rare case. As of December 2026, the Claude Code sandbox can read "the entire computer, except certain denied directories" by default. Its docs warn that this "still allows reading credential files".
Whatever you put in the sandbox, the code can use.
So decide four things before you switch code on:
| What goes in | Default to | Why |
|---|---|---|
| Files | Only the files this run needs | A mounted shared drive is readable in full |
| Secrets | Only the keys this job needs, set at runtime | OpenAI's sandbox guide says to "treat sandbox credentials as runtime configuration, not prompt content" |
| Network | Closed, then open named addresses | Open routes let code send data out |
| Time | A limit per run, and stop idle sandboxes | Runaway code and idle sandboxes cost money |
Will locking it down break the agent?
At first, sometimes, and that's useful. A closed sandbox fails loudly when the code needs something it doesn't have. Claude Code, for example, "names the denied host in the command's result", so the agent and anyone reading its output can see which address was blocked. Add each address, folder or key one at a time, and only when the job needs it. Its docs recommend the same narrow approach: give write access "to a specific location" rather than taking a tool out of the sandbox.
Where to start
The order we suggest for a first agent that runs code:
- Pick the sandbox by where the code comes from. Staff requests only: a container or a hosted service. Outside files, emails or web pages: gVisor or a small virtual machine. If customer files can't leave your servers, run either one yourself rather than using a hosted service.
- Switch it on for one agent, with the network closed and no secrets inside.
- Watch what gets blocked for the first few days, and open only what the job needs.
- Then put approval on the risky tools, because the sandbox never sees them.
How do you test an agent's actions without real effects?
A sandbox can't do this on its own, because tool calls run outside it. In a test, a tool call that sends an email still sends one. There are two ways to stop it.
The first is a copy of your business systems with test customers, as our guide on testing an AI agent before go live describes. The second is saved stand-in replies, which engineers call mocks: each tool call in a test gets a recorded answer, and any call without one is refused. Our post on prompt regression testing explains how to build them.
Use both before launch. In live use, put human approval on the few actions that are expensive or impossible to undo.
How we run a code execution sandbox in Avesta AgentOS
In Avesta AgentOS, running code is a setting on each agent. It's off unless the team building the agent turns it on and connects a hosted sandbox service. Three patterns from it carry over to any agent.
1. One sandbox per conversation, stopped when idle
Each conversation gets its own sandbox, so files from one chat never sit beside another's. As of December 2026, a sandbox stops after 5 idle minutes, and a stopped one is archived later. Each run has a time limit, 30 seconds by default. Tools that people write for an agent are reviewed code, level 1 on the scale above. They run separately, in a small isolated runtime with its own memory and time limits.
2. Files in by name, results out through fixed folders
The agent must name each file it wants, and the file lands in a fixed folder. Results leave only through three fixed output folders, each with one destination, such as back to the person. Only files that are new or changed during the run are collected.
3. Only the secrets picked for the sandbox
Since November 2026, a team can pick which stored keys an agent's sandbox receives. The code gets those and nothing else from the key store.
Two controls sit outside the sandbox. In a test run, Avesta AgentOS blocks the code tool unless the test supplies a stand-in reply. And an approval rule can be put on the code tool, just like any other tool.
When does an agent not need a sandbox?
A full sandbox is needed when the agent runs code or commands nobody reviewed. Reviewed code needs limits, not a full sandbox. Many agents run no code at all: they answer from documents and call a fixed set of tools, so a sandbox adds cost and a moving part without reducing risk.
- Question-and-answer agents. An agent that searches a knowledge base and replies has no code to isolate.
- Fixed tools only. If every action is a tool your team built and reviewed, spend the effort on that tool's permissions instead.
- A calculator would do. If the agent only needs sums or date maths, a small reviewed tool is simpler than a general code runner.
The bottom line
An AI agent sandbox is worth having once an agent writes and runs its own code. It keeps that code away from your machines, as long as you're careful about what you put inside it.
For a common way that harmless-looking input turns into harmful code, read our guide to prompt injection in AI agents.
List every file, secret and network address your agent's sandbox can reach, then remove the ones its job doesn't need.
Want agents whose code runs in a sandbox and whose real-world actions wait for approval? See how we run safe AgentOps for production AI agents.
Get started: hello@avestalabs.ai
Frequently asked questions
- Can an AI agent escape its sandbox?
- It can, if the sandbox is set up loosely or the isolation layer has a flaw. That's why the strongest options put a separate kernel, the core of the operating system, between the code and the host. A sandbox escape is rare compared with the everyday problem: code using a secret, folder or network route that was left inside on purpose.
- What is the best AI agent sandbox?
- There's no single best one. Pick by how much you trust the code and where the agent runs. As of December 2026, OpenAI's Agents SDK connects to hosted services such as E2B, Daytona and Modal, to Docker, and to a local sandbox, and Kubernetes has its own Agent Sandbox project. Compare how each handles secrets, network access, saved state and cost.
- Is a sandbox the same as a test environment?
- No. A sandbox isolates where code runs, in testing and in live use. A test environment is a copy of your business systems with fake customers and orders. An agent can run its code in a sandbox while its tool calls still reach live systems.
- Does a sandbox stop prompt injection?
- Only partly. Prompt injection is text hidden in a web page or file that tricks the agent. If the trick makes the agent run harmful code, the sandbox limits what that code can reach. If it makes the agent call a real tool, such as sending an email, the sandbox isn't involved.
Newsletter
Learn how to use AI in your business
Practical lessons on what AI can do, where it falls short, and what’s worth trying.
Share this article




