Topic 3 of 4 · 5 min read
Sandboxing agent-written code
Goal: Specify the sandbox limits for running agent-generated code on user data: filesystem, network, time and memory.
Run agent-written code in an environment where the worst thing it can do is small and known: limited files, no open network, a time limit and a memory limit. You cannot read every script before it runs, so the box has to be safe on its own.
Check yourself · 1 of 3
An agent will run generated Python on a customer's uploaded file. Which network setting is the safest default?
Why it matters
An agent's script runs with the permissions of whatever runs it. On your laptop or production server, that can mean your files, your credentials and your network. The model does not need to be malicious. It can misread a task, follow instructions hidden in a web page it fetched, or write a loop that never ends. The sandbox turns "the code did something wrong" into "the code did something wrong inside a box".
The four limits to specify
- Filesystem. Mount only the input files the task needs, read-only where possible, and one scratch folder for output. No access to home directories, SSH keys, environment files or the host's other projects.
- Network. Deny by default. If the task needs one API, allow that host only. An open network lets code send your data anywhere.
- Time. A hard wall-clock limit, such as a minute for a data task, then the process is killed. Without it a stuck loop holds the machine.
- Memory and CPU. A fixed cap, so a runaway allocation fails its own box and not your server. Docker documents memory and CPU flags for exactly this.
Also drop privileges: run as a non-root user, with no access to the container runtime socket, and pass in no secrets the code does not need.
Choosing the isolation
A container is light and fast, but it shares the host's kernel, so a kernel flaw can escape it. A microVM or full virtual machine has its own kernel and is a stronger boundary at more cost. A hosted code-execution tool from a model provider runs the code on their side, which moves the risk off your machines. Choose by what the code touches: untrusted code on real user data points to stronger isolation.
Before and after
Weak: an agent runs python script.py directly on a server that holds the production .env. Fixed:
docker run --rm --network none --memory 512m --cpus 1 \
--read-only --tmpfs /tmp -v ./input:/data:ro -v ./out:/out \
--user 1000 sandbox-python timeout 60 python /data/script.py
No network, 512 MB of memory, one CPU, read-only input, one output folder, a 60 second timeout.
What goes wrong
- Network left open. Convenient, and a free exfiltration path.
- Secrets in the environment. Everything the process can read, the script can read.
- Cleaning up never. Old sandboxes and output files pile up and leak between users.
Examples
A spreadsheet analysis box
The task is to summarise a customer's uploaded CSV. The sandbox mounts that one file read-only, has no network, 1 CPU, 512 MB of memory and a 60 second limit, and writes only to an output folder. If the script goes wrong, it can only damage its own scratch space.
The one allowed host
An agent needs current exchange rates. The sandbox keeps the network off except for one rates API host. Code that tries to post data to any other address fails at the network layer, whatever the script says.
Key points
- Agent-written code should run in a box whose worst case is small and known.
- Specify four limits: filesystem, network, time, and memory with CPU.
- Deny the network by default and allow only the host a task needs.
- Containers share the host kernel, while microVMs and hosted tools give stronger isolation.
- Run as a non-root user and pass in no secrets the script does not need.
Common mistakes
- Running generated code directly on a server that holds production credentials.
- Leaving the network open because the task might need it later.
- Setting no time limit, so one stuck loop holds the machine.
- Mounting the whole home directory instead of the one input file.
Check yourself · 2 of 3
Why set a wall-clock time limit on agent-generated code?
Check yourself · 3 of 3
A team needs stronger isolation than a container gives for untrusted code. Which choice offers that?
Practice items, not counted towards your mastery.
Start here
- Executable Code Actions Elicit Better LLM Agents (CodeAct) (opens in a new tab)PaperFree
The research paper behind code as an agent's action, compared with JSON tool calls.
- Code execution with MCP (Anthropic) (opens in a new tab)ArticleFree
How code actions cut tool-definition and result tokens, and what sandboxing they need.
- Programmatic tool calling (Claude API docs) (opens in a new tab)DocsFree
How Claude calls your tools from code it writes, and what the host must provide.
More for this topic
- Hugging Face smolagents: secure code execution (opens in a new tab)DocsFree
Compares local interpreters with sandboxed and remote execution for code-writing agents.
- Runtime options with Memory, CPUs, and GPUs (Docker docs) (opens in a new tab)DocsFree
The exact flags for memory and CPU limits on a container.