Lesson 37.5 · 5 of 5 · 6 min read · Reviewed 24 Nov 2026
Sandboxed code execution: containers and hosted sandboxes
By the end, you can
- Explain why model-written code needs a sandbox.
- Choose between a local container and a hosted sandbox.
- Set the main limits on a sandbox: network, files, resources and time.
Lesson 37.3 showed a code execution toolCode execution tool: A built-in tool that lets the model run code in a sandbox and use the results. that a platform runs for you. Often you need your own: the code must see private data, use your libraries, or run inside your network rules. Then you own the sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials..
Why model-written code is risky
Code the model writes is shaped by everything in its context, including uploaded files and web pages that may hold injected instructions (chapter 45). Even without an attacker, it can loop forever, fill the disk or delete the wrong folder. Treat it as untrusted input that happens to be executable. Never run it with exec() in your own server process, where it can read your keys and files.
Two common homes for the code
| Local container | Hosted sandbox | |
|---|---|---|
| Example | Docker, Podman | E2B and similar services |
| Runs on | Your machine or servers | The provider’s machines |
| Isolation | Shares the host kernel | Usually a microVM per sandbox |
| Start-up | Image pull, then fast | An API call, usually well under a few seconds |
| You manage | Images, limits, clean-up, patching | An API key and a budget |
| Good for | CI, internal data, offline work | Many users, untrusted uploads, no ops team |
A container packages a process with its own filesystem and limits, but it shares the host’s operating system kernel. A kernel bug can let code escape. For code from strangers, add a stronger layer: a user-space kernel such as gVisor, or a lightweight virtual machine (microVM) such as Firecracker. Many hosted sandboxes use microVMs for this reason.
A worked example: a locked-down container
An agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. writes analyse.py to summarise sales figures. Your tool runs it like this:
docker run --rm \
--network none \
--read-only --tmpfs /tmp \
--memory 512m --cpus 1 --pids-limit 64 \
--cap-drop ALL --security-opt no-new-privileges \
--user 1000:1000 \
-v "$PWD/job-42:/work:ro" \
python:3.12-slim timeout 30 python /work/analyse.py
Each flag closes a door:
--network none: no calls out, so no data leaves and nothing is downloaded.--read-onlywith a small/tmp: the code can’t change the image.--memory,--cpus,--pids-limit: no memory bombs or fork bombs.--cap-drop ALL,no-new-privileges, a non-root user: fewer kernel powers.- One read-only folder with just this job’s input; no home directory, no secrets.
timeout 30and--rm: it stops, and nothing is left behind.
Your tool then returns stdout, stderr and the exit code to the model as the tool result, cut to a sensible length.
A hosted sandbox, same ideas
With a hosted sandbox you call the provider’s SDK to create a sandbox, upload the input, run the code, read the output and destroy it. The provider handles the isolation. You still choose the limits: a timeout, whether outbound network is allowed, which files go in, and one sandbox per user or task so nothing leaks between them. Keep the provider’s key in an environment variable on your server, never in the sandbox.
Common mistakes
- Mounting the whole project or home folder. The code can read
.envfiles and SSH keys. - Leaving the network on “just for pip”. Build needed packages into the image instead.
- Reusing one sandbox for everyone. Files and processes leak between users.
- No time limit. One
while Trueholds a worker for ever. - Returning unbounded output. A 50 MB print floods the model’s context; truncate it.
When to use which
Use a local container when the data must stay on your infrastructure and you can run it well. Use a hosted sandbox when many users run arbitrary code and you don’t want to operate the isolation yourself. Either way, the rule is the same: least privilegeLeast privilege: Giving each component only the access it needs for its task, and no more., short life, then throw it away.
Key points
- Code an agent writes is untrusted input that happens to be executable: run it somewhere it can't hurt you.
- A container (such as Docker) isolates code on your own machine; a hosted sandbox (such as E2B) runs it in a short-lived remote machine.
- Isolation alone is not enough: switch off the network, mount no secrets, cap CPU, memory and time, and throw the sandbox away afterwards.
- Containers share the host kernel, so for code from strangers prefer stronger isolation such as microVMs or gVisor.
Run model code in a locked-down container
Run a piece of untrusted Python in a Docker container with no network, capped resources and a timeout, and prove each limit works.
You will have: A run-sandbox shell script plus a table showing each limit, the test you ran against it, and the result.
Steps
- Install Docker and pull a slim Python image, for example python:3.12-slim.
- Write run-sandbox.sh that runs a given job folder with --rm, --network none, --read-only with a small --tmpfs /tmp, --memory 512m, --cpus 1, --pids-limit 64, --cap-drop ALL, --security-opt no-new-privileges, a non-root --user and the job folder mounted read-only.
- Wrap the python command in timeout 30 so a stuck job is killed.
- Test each limit with a tiny script: an HTTP request (should fail), writing outside /tmp (should fail), an endless loop (should be killed by the timeout) and a large memory allocation (should be killed).
- Optional: run the same job in a hosted sandbox such as E2B with your provider key in an environment variable on your machine, and compare start-up time and the controls you could set.
- Record each test and its result in a short table.
Done when
- The network test fails with a name resolution or connection error.
- The endless loop stops after about 30 seconds and the container is gone afterwards.
- Writing to /work fails because the folder is mounted read-only.
Docs: docker container run reference (Docker docs) · Docker Engine security (Docker docs) · E2B documentation
Finished the chapter? Take the chapter check.