Code Execution Sandboxes
In this article
- Explain why a tool that runs any code the model writes needs a sandbox, while a tool with checked arguments does not.
- Say who enforces the limits when a provider runs the code and when your own application runs it.
- Choose an isolation level, a container or a microVM, from who can write the code and who uses the system.
- Write the minimum sandbox policy for a job across files, processes, credentials, network and output, and name the risk it leaves.
- Terms defined here: sandbox, isolation level.
Overview
A colleague uploads a messy spreadsheet and asks an AI assistant to clean it. The assistant has no "clean spreadsheet" button: it writes a short Python program, and your system runs it. That program came from a model, and the model read a file a stranger may have edited. So what stops the program from reading your server's secrets or calling some address it has no business with?
You will learn why code the model writes needs a walled-off place to run, who builds that wall, how strong it must be, the smallest set of rules that still lets the job finish, and the risk left once the wall is up.
A tool that runs any code
An application can offer the model tools: functions the model may ask for by name, with arguments. The application runs the function and returns the result to the model. [1] Compare two tools. The first, get_invoice(id), takes one argument that your code checks before anything runs: the id must belong to the caller. That tool is a scoped capability: it can do only what its checks allow, however strange the request.
The second tool takes a whole program. Here is a made-up job that carries through this topic: an internal analytics assistant at one company writes Python to clean a user's uploaded sales.xlsx, saves the result as report.csv, and posts it to an internal API. Each request is one run; this one is r-81. All these names and settings are invented for the course. The model writes this:
import pandas as pd
df = pd.read_excel('/work/in/sales.xlsx')
df = df.dropna(subset=['region', 'amount'])
df['amount'] = df['amount'].round(2)
df.to_csv('/work/out/report.csv', index=False)Now suppose the spreadsheet holds a hidden instruction, and the model adds two lines: one opens ../../host/.env, a file of server secrets two folders up, and one sends a request to the address 169.254.169.254, which the job has no reason to call. The job needs exactly one address, the internal API; any other address the code can reach is a place it could send your data or ask for things your server can get and the code should not. Before reading on, ask yourself: if your application runs this program as a plain child process on its own server, what stops either line?
Nothing. The program runs as your application's user, with your files and your network. Scanning the code would not save you either: for Claude's Bash tool, Anthropic calls a command check a rough filter that catches honest slips but cannot hold back a command written to get past it, and puts the real control in isolation: a container or a virtual machine. [1] With a checked tool, authority is what the checks allow. With code, authority grows to whatever the code can reach, so the only safe answer is to limit what it can reach.
The place that limits it is a sandbox: an isolated workspace, such as a container, where an agent runs commands and works on files. [3] The line between that workspace and everything else is a trust boundary: inside sits code you did not write and cannot fully trust, outside sit your secrets and services. [4]
Who runs the code
With a hosted runner, the provider runs it. Claude's code execution tool runs every command server-side in a sandboxed Linux container with 5 GiB of RAM, 5 GiB of workspace storage and one CPU, (Nov 2026). [2] The container has no internet access at all, so the step that posts report.csv to the internal API has no way out of that container. [2]
With a self-run runner, your application runs it. In Claude's Bash tool, Claude picks the command; your application runs the shell, sets the time limit and does every safety check. [1] Your application runs whatever Claude requests, so every command is untrusted input. [1] Our job must reach an internal API, so it is self-run, like the rest of this topic.
Either way, the model writes the code and the runner, ordinary code that someone controls, enforces every limit. A prompt that says "do not read secrets" is a request, not a wall.
How strong a wall
The isolation level is how strongly a sandbox separates code from the host, set by the technology you pick; each step up trades more overhead and complexity for more strength. [4]
The deciding question is the kernel, the core of the operating system that every program asks for files, memory and network. A container gets its own view of the filesystem, process list and network, but it shares the host's kernel, so a kernel bug could let code escape. [4] A virtual machine runs its own kernel, and Firecracker is built for small, fast ones called microVMs. [4] gVisor sits between: it catches the program's calls to the kernel in user space and answers most of them itself, so an attacker must first break gVisor. [4] The table compares the three options for our job:
| runner | shares the host kernel | who can write the code | verdict |
|---|---|---|---|
| child process of your app | yes, plus your files, user and network | only you | never for model-written code |
| container | yes | one organisation's own users | fits one company |
| microVM or gVisor | no (own kernel or user-space kernel) | anyone, including different customers | needed for many customers on one host |
Who writes the code is your threat model. The right controls depend on it, and for one host serving different customers, or for untrusted content, the extra isolation of gVisor is often worth its cost. [4] The Kubernetes agent-sandbox project agrees: strong isolation through gVisor or Kata Containers, another secure sandbox runtime, matters for untrusted code and for different customers on shared hosts. [6]
Our assistant serves one company's staff, so one container per run is our choice. If the same host ran code for different customers, a shared kernel would sit between them, and that needs a microVM or gVisor.
The minimum policy for r-81
Every rule follows least privilege: the job gets what it needs and nothing else. Anthropic's guide gives the same shape: mount only the folders needed, read-only where possible, and send network traffic and credentials through a proxy, a go-between process outside the sandbox that checks and forwards each request. [4] For r-81, five boundaries cover it:
- Files:
/work/inis read-only and holdssales.xlsx;/work/tmpis scratch space; nothing else from the host is mounted. - Artifacts:
/work/outreceivesreport.csvand is the only output that leaves the run. - Network: under a network egress control, a rule on which outbound connections may leave, the running job may reach
api.internal.example:443and nothing else; that other address is unreachable. - Credentials: no real credential is inside the sandbox, only a placeholder; the real API key stays outside, and a proxy adds it to the request on its way out, so the code never holds it. [4]
- Processes: everything the code starts lives in the container, and the container is removed after the run.
Replay the two extra lines. ../../host/.env points at nothing, because the host was never mounted, and the request to 169.254.169.254 hits a closed network. Both fail not because someone spotted them but because the code cannot reach what it was not given. That is the blast radius, how much a bad line can damage, shrunk to /work and one host.
Container flags can enforce this list, or an OS-level sandbox runtime can: Anthropic's sandbox-runtime is a light tool that enforces file and network limits through the operating system, with no container image or Docker setup. [4] Note that processes under sandbox-runtime also share the host kernel. [4]
What the sandbox leaves
A sandbox reduces risk; it never removes it. Two risks stay with r-81. A kernel flaw could let code out of the container, which is the price of sharing a kernel. [4] And the code can still misuse the one host it may reach: an allowlist decides where data may go, not what data goes, so anything the code can read can travel to an allowed host. [4] The gVisor authors put the wider point bluntly: a sandbox does not replace a secure architecture, and an exploitable network service on the host is a way around it. [5]
The rule at work
Put model-written code on the untrusted side, enforce the limits in your runner, size the wall by who could have written the code, and write down the risk that remains.
Common mistakes
- Treating a container as perfect isolation. It shares the host kernel; across customers use gVisor or a VM. [4]
- Running generated code in the host process. It inherits your files, user and network. Fix: run it in a container or VM as a low-privilege user. [1]
- Overbroad mounts and open network. Mounting home folders such as
~/.sshor~/.awshands them to the code. Fix: mount only what the job reads, and deny all other traffic. [4]
All three fail the same way: the code, not you, decides what it reaches.
In production
- Drop privileges inside the container too. Anthropic's hardened example drops all Linux capabilities, makes the root filesystem read-only and runs as a non-root user. [4]
- Budget for gVisor's overhead. CPU-bound work runs at about normal speed, but file-heavy work with many opens and closes can be 10 to 200 times slower. [4]
- Log every command and its output. Anthropic's Bash tool guidance asks for this so you can audit what ran, and for secrets to be redacted from output before it returns to Claude. [1]
Sources
- Bash tool. Anthropic. Checked 24 Oct 2026.
- Code execution tool. Anthropic. Checked 24 Oct 2026.
- Choosing an agent environment. Anthropic. Checked 24 Oct 2026.
- Securely deploying AI agents. Anthropic. Checked 24 Oct 2026.
- gVisor: Security Model. Google (gVisor). Checked 24 Oct 2026.
- Kubernetes agent-sandbox. Kubernetes SIG Apps. Checked 25 Oct 2026.
Check yourself
Check yourself
Question 1 of 13
A model writes programs and your system runs them. What does a sandbox add?
Your first check counts towards your result.