Why run agent generated code inside an isolated sandbox?
A sandbox is an isolated runtime (container, gVisor, or Firecracker MicroVM) that runs agent-generated code with no host filesystem or network access, used because the LLM can produce buggy or attacker-steered code.
Imagine an apprentice cook who is allowed to invent new recipes on the fly. Most of the time the recipes are fine. Once in a while the apprentice writes a recipe that says 'pour gasoline into the broth'. You would not let the apprentice cook in your main kitchen with access to every shelf, the gas line, and the customer's fridge. You give them a small isolated kitchen with only the ingredients for one dish, sealed off from the rest of the building, and you throw the kitchen away after the dish is done. A code sandbox is that small isolated kitchen for an agent. The code runs, but inside walls so thick that a bad recipe cannot reach anything real.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
A code sandbox in the agent setting is an isolated execution environment where agent-generated code runs without access to the host system. It is one of the most consequential design choices in a code-executing agent stack, because the moment you let a language model write code that your runtime executes, you are running untrusted code on your infrastructure.
The word 'untrusted' is doing a lot of work in that sentence. It does not mean the model is malicious. It means you cannot rely on the code being safe without verifying it, and verifying generated code in real time is not feasible. Some of the unsafe code comes from honest model mistakes: a wrong path in an rm, an infinite loop, a buggy file write. Most of the dangerous code comes from prompt injection, where an attacker hides instructions inside any content the agent reads (a document, a web page, a database row, even a filename) and steers the model into producing malicious code with your credentials attached.
The rest of this explanation walks through why generated code must be treated as untrusted, the tiers of isolation that defend against it, the four controls every real sandbox layers on top of isolation, and the production services that bundle the whole thing together.
Why generated code is untrusted by construction
Two threats need to be named separately, because conflating them leads to weak defences.
The first is accidental. Language models hallucinate destructive commands at a non-zero rate. Even a well-aligned model occasionally writes rm -rf /tmp/data/* against the wrong path, opens a file in write mode instead of append, or produces a loop with no exit condition. These bugs are routine, not exotic. The cure for them, if you are running the code in production, is to make the blast radius small enough that a bug damages nothing important.
The second threat is deliberate. Prompt injection embeds instructions in any text the agent reads: a help-desk ticket, a web page, a row in a database, a code comment, a filename, an OCR'd PDF. The model treats those instructions as part of its working brief and can be steered into writing code that exfiltrates secrets, calls internal services with the agent's credentials, or installs a backdoor. The OWASP Top 10 for LLM Applications ranks prompt injection as the number-one risk to LLM systems for exactly this reason.
What makes the agent setting dangerous is the combination of injection plus tool authority. A chat model that only emits text can be tricked but cannot do much damage. An agent that executes code with credentials can do almost anything the credentials allow. Detection-based defences (scanning the generated code for suspicious strings) are trivially bypassed with obfuscation, so the load-bearing control has to be isolation.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- e2b Cloud Sandbox runs agent-generated Python in Firecracker MicroVMs, with a per-session ephemeral filesystem and configurable egress allowlists.
- Modal executes untrusted agent code in serverless gVisor-backed containers with per-run resource caps and default-deny network egress.
What an interviewer would ask next. Try answering before peeking at the approach.
QA container and a Firecracker MicroVM both isolate processes. Why prefer the MicroVM for arbitrary agent code?
Compare the boundary. A container shares the host kernel, so one kernel exploit escapes to the host. A MicroVM gives each run a separate guest kernel behind a hardware-virtualisation boundary, raising the cost of escape sharply at the price of slower startup, which warm pools largely hide.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Thinking a plain Docker container is a sandbox. A container shares the host kernel, so a kernel exploit escapes. Real agent sandboxes use gVisor or microVMs, default-deny networking, and ephemeral teardown.
60 second bullets to scan on the way to the call.
Define a sandbox as an isolated runtime for agent-generated code.
State why generated code is untrusted by construction (bugs and prompt injection).
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.