Kolkata ·  
AI engineering
agents
deep · 6 min

Sandboxing

Running what an agent does inside a fence, so a bad command or a hijacked agent can only damage what's inside it.

updated 29 Sept 2026

Say we want a coding agent to just get on with it. No permission prompts, everything approved by default. People call this YOLO mode, and it's where agents get the most done. It's also plainly dangerous. Simon Willison puts the choice as three options: run the agent in a sandbox, run it on someone else's computer, or take the risk. Most people take the risk.

Let's see what the risk actually is, and then what a fence around the agent has to look like to remove it.

What could happen

Our agent, running its loop, clones a repo, runs npm install, runs the tests, reads some docs on the web and edits files until the tests pass. Every one of those is a shell command it chose, and a shell command can do anything we can do. Three kinds of thing can go wrong:

  • A bad command deletes or mangles files we care about.
  • Something steals what the agent can see, like source code or secrets in environment variables. A web page or README with hidden instructions is enough to try this (prompt injection).
  • Our machine gets used as a stepping stone to attack someone else.

The theft one has a name worth knowing, the lethal trifecta. An agent that can see private data, reads untrusted content, and can send data out can be tricked into sending our data to an attacker. Can't we filter the bad instructions out? A filter that catches 95% of attacks sounds good until you remember that in security, missing one in twenty is a failing grade. The dependable fix is to take away one of the three. A sandbox does that without taking away the agent's tools: it limits what the agent can see and where it can send things. (The leak itself is data exfiltration.)

Two walls

A useful sandbox has two boundaries, and it needs both.

The filesystem wall: the agent can read and write its working directory and nothing else. Not our SSH keys, not our shell startup files, not its own settings.

The network wall: the agent can only connect to servers we've approved, like the package registry and our git host.

Why both? Each covers the other's gap. Without the network wall, a tricked agent reads our SSH keys and sends them anywhere. Without the filesystem wall, it can climb out of the sandbox and get network access anyway.

Claude Code's sandbox is a good concrete example. It uses features of the operating system (bubblewrap on Linux, Seatbelt on macOS), and the rules apply to every script and subprocess a command starts. Network traffic goes through a proxy outside the sandbox that lets through approved domains and asks the user about new ones. Inside it, Anthropic's own engineers saw 84% fewer permission prompts, because the agent no longer had to ask about commands that stayed inside the fence. So the fence ended up giving the agent more room to work.

Keep the keys outside

The safest secret is one that was never inside the box.

One way is a proxy that holds the real credential. In Claude Code's cloud sessions, the GitHub token lives outside the sandbox. Inside, git only has a scoped credential that works with the proxy. The proxy checks each request (is this push going to the configured branch?) and only then adds the real token. Even a fully compromised sandbox never sees it.

The other way is a key that can't do much. If the agent needs one, make it a test or staging key, and if it can spend money, cap it. Willison's example is a dedicated Fly.io organization with a $5 budget and an API key that only works there.

How hard is the wall to break?

The walls say what to block. How they're built decides how hard they are to get through. The difference comes down to the kernel, the core of the operating system that every program asks when it wants to open a file or a socket.

Containers, like Docker, all share the host's one kernel. Seccomp filters can trim which system calls a container may make, but a single kernel bug reachable through an allowed call can mean escape to the host.

gVisor puts a second kernel in the way. Its Sentry, written in Go and running as an ordinary process, catches the program's system calls and answers them itself. The host kernel only sees a small set of calls. The cost is that every system call goes through extra software.

Firecracker runs each job in a microVM: a tiny virtual machine with its own guest kernel. It emulates only 5 devices, and a separate jailer process is a second line of defense in case the VM boundary ever breaks. It's AWS's project, the thing under Lambda, and a microVM boots in under 125 ms with under 5 MiB of overhead.

Why does this matter for agents in particular? An agent loop is thousands of small file and network operations: clones, installs, test runs. gVisor pays a toll on each one. Inside a Firecracker guest they hit a normal kernel at close to native speed.

So which one? Fly.io's rule of thumb goes by threat. If the worry is the agent doing something dumb, or getting tricked into deleting the repo, a container with scoped keys and a locked-down network covers most of it. If someone is actively trying to break out, or the code is truly untrusted, add a second kernel: Firecracker if you control hardware with KVM, gVisor if you're on Kubernetes or cloud VMs without it.

The ladder, and the bottom rung

In practice you pick a rung. Claude Code's docs lay them out like this:

The bottom rung is the trap. Sandbox only the shell commands, and the file tools, MCP servers and hooks still run with full access to our machine. An agent that runs unattended needs the whole process inside the fence. For a repo we don't trust at all, the docs point to a dedicated VM or a cloud session. And it isn't only coding agents. Any design where the model writes and runs code needs one of these rungs.

There's a sneaky way out, too. If the agent can write the files that set up its own tools, like git hooks, MCP settings or shell startup files, it can plant something that runs outside the sandbox next time. Claude Code's runtime blocks those writes at the project root. On Linux it builds that list once at launch, so a repo the agent clones later isn't covered.

And the network can't be fully closed. Agents need pip install and git clone. The isolation design decides what a breakout costs; the list of allowed domains decides what a breakout is worth. Also, a sandbox doesn't hide anything from the model. Every file the agent reads still goes to the model provider.

Fences and approvals do different jobs

A permission prompt decides whether an action runs. A sandbox limits what it can reach once it does. Asking about every step wears people out (human in the loop covers that), and a classifier that approves for them still misses some risky actions. So the less we ask, the more the fence has to carry. Runs with all prompts off should always be in a container, a VM or the sandbox runtime.

How strong does the fence need to be? People disagree. Willison calls Docker a reasonable risk for most people despite known container escapes. Fly.io says one shared kernel means one bug away from escape, and wants a second kernel for untrusted code. They're mostly talking about different threats, an agent making mistakes versus an attacker working to get out. (Worth knowing: Fly.io sells microVM sandboxes.) Which threat you're actually facing is the question to settle before you pick.

Further reading