Agent Sandboxing
Give agent-written code its own disposable computer with no access to your files, secrets, or network. Only the answer comes back; the mess stays inside and gets thrown away.
Why Does This Exist?
Once your agent can run code, one bad line can do real damage. A cleanup command aimed at a temp folder can wipe a home directory, and a pasted web page can carry instructions that tell the agent to read secrets and send them out. You saw the attack side in prompt injection: the model obeys text it should have treated as data.
So you stop running model-written code on machines you care about. A sandbox is a fresh, disposable machine that holds nothing valuable: no host files, no secrets in its environment, no open network. If the code deletes everything in sight, it deletes a fake disk you were going to throw away anyway.
This page covers that one layer: isolating code execution. It does not cover which tools the agent may call. That is tool permissions. The full layered setup lives in agent security. If you have not yet seen how words become actions, start with tool use and function calling: the model names a function and its arguments, and your code decides what really runs.
Think of It Like This
The school lab's fume hood
You hand a student chemicals and ask for a result. They mix everything inside a glass box with its own beakers and its own tray. If they knock a beaker over, the spill stays on the tray. Only their written reading slides out under the glass, and you bin the tray liner before the next class.
Point for point: the student is the agent, the mixture is the generated code, the glass is the sandbox boundary, the tray is the allowlisted work directory, the written reading is stdout, and binning the liner is destroying the container.
Where it stops: the hood never judges whether the experiment was a good idea. If the student needs a library book, you fetch it yourself. That permission decision lives outside the glass.
How It Actually Works
The rule is simple: the agent never executes anything. It submits a request, and a broker, a small piece of your code you control, runs it somewhere safe and hands back only the result.
The boundary: what crosses and what does not
Think of each run as R = (C, F, L). C is the code text the model wrote. F is the declared input files, and they must sit under one allowlisted directory, /work. L is the limits for the run: CPU, memory, wall-clock time, and network, which defaults to closed.
The broker builds a fresh machine around that request: a Docker container (Linux namespaces plus cgroups) or a microVM with its own kernel. It mounts only /work, scrubs secrets out of the environment, and closes egress. The code runs, and the broker captures three things: the exit code, stdout, and stderr. Then it destroys the machine. Host files, credentials, and sockets never cross the boundary in either direction.
One run, end to end
Say the agent wants a total over /work/sales.csv and also fancies a look at /etc/passwd. The broker mounts /work read-only, hands the container an empty /tmp, and starts the clock with, say, 512 MB of RAM and 30 seconds. The read of /work/sales.csv proceeds. The read of /etc/passwd fails because that path is not mounted. An outbound socket fails because egress is closed. The broker returns stdout, the exit code, and stderr, then deletes the container, including anything the code downloaded or changed.
That is the trade-off, stated plainly: each run costs you a cold start, a bigger image to maintain, and some jobs you simply cannot do inside (no package installs, no browsing) unless you punch named holes. Do not pay it when no code runs at all. Pure text reasoning needs no sandbox.
And know the assumption it rests on. The sandbox assumes the broker cannot be talked into mounting something sensitive. A confused broker that bind-mounts your home directory, or a kernel or hypervisor bug that lets code break out, breaks the whole promise. That is why sandboxing is one layer inside agent security, never the whole plan.
Worked example: four numbers, two refusals
The container sees one file, /work/sales.csv, holding four values: 12, 18, 9, and 21. The agent's script asks for the sum and the mean, then tries two extras: reading /etc/passwd and opening a socket.
The sum goes step by step: 12 + 18 = 30, then 30 + 9 = 39, then 39 + 21 = 60. The mean divides by the count: 60 / 4 = 15. So stdout carries sum = 60 and mean = 15.
The scorecard for the run: three requested actions, one allowed, two denied. The file read under /work is allowed. The host path read is denied because only /work is mounted. The socket is denied because egress is closed. The picture above draws exactly this run.
Code
The point of this snippet is the check, not the arithmetic. It mirrors what the broker decides for every path and socket, plus the four-number computation, so you can see the allow and deny logic as plain code.
def is_path_allowed(path: str) -> bool: return path.startswith("/work/")
def is_socket_allowed(egress_open: bool) -> bool: return egress_open
def summarize(values: list[float]) -> tuple[float, float]: total: float = sum(values) mean: float = total / len(values) return total, mean
sales: list[float] = [12.0, 18.0, 9.0, 21.0]total, mean = summarize(sales)print(total) # -> 60.0print(mean) # -> 15.0print(is_path_allowed("/work/sales.csv")) # -> Trueprint(is_path_allowed("/etc/passwd")) # -> Falseprint(is_socket_allowed(False)) # -> FalseThe outputs are what a real run prints: 60.0 and 15.0 from 12 + 18 + 9 + 21, True for the work file, False for the host path, False for the closed socket. In production the same two functions live in your broker, not in the agent, and the agent never gets to edit them.
Watch Out For
Egress left open while files stay locked down
You lock the filesystem, keep the sandbox, and leave the network on for package installs. Then an injected instruction tells the agent to fetch a payload or post your prompt, with its pasted secrets, to an outside server. Nothing on disk breaks, so the run looks clean.
The symptom shows up in your proxy logs, not your files: unexpected outbound connections or large uploads from the sandbox host. The fix: start every sandbox with egress closed, add named domain allowlist entries only when the task truly needs them, and never place live secrets in the sandbox environment. If installs are needed, bake them into the image at build time.
Reusing one sandbox across runs
You keep a warm container to skip cold starts, and runs start disagreeing with each other. One run pip-installs a different library version, leaves files in /tmp, or edits the very test it was supposed to pass. Rerunning the same prompt gives a different answer, and you cannot tell which run to believe.
The symptom is flakiness that vanishes on a fresh boot. The fix: one fresh filesystem per run, inputs mounted read-only, a wall-clock plus CPU and memory cap on each execution, and destroy-after-return with no exceptions. Cache the image, never the running machine.
The Quick Version
- Agent sandboxing runs model-written code in a fresh container or microVM that sees only an allowlisted work directory.
- Only the exit code, stdout, and stderr cross the boundary while host files, secrets, and the network stay outside.
- A four-value run over 12, 18, 9, and 21 returns sum 60 and mean 15 while a host path read and an outbound socket are denied.
- Closed egress, a fresh filesystem per run, and CPU, memory, and time caps turn isolation into dependable containment.
- Sandboxing limits blast radius without judging intent, so least-privilege tool permissions and human review still belong in the loop.