Sandboxing Code-Executing Agents: E2B, Daytona, and the Isolation Stack
Compare E2B, Daytona, and the isolation stack for sandboxing code-executing AI agents. MicroVMs, gVisor, containers, and threat models explained.
Why Code Execution Is the Hardest Security Problem in AI Agents
An AI agent that writes and runs code is fundamentally different from one that only retrieves documents or calls APIs. The moment you hand a model a shell, you've introduced arbitrary, non-deterministic execution into your architecture. You cannot enumerate in advance what the agent will run, because the agent generates the instructions itself, at runtime, from whatever context it has ingested. A retrieval-augmented agent that only summarizes documents produces text you can inspect before it reaches a user. An agent with a bash tool produces side effects — files written, packages installed, sockets opened — that no reviewer sees before they happen.
That context is the attack surface. A prompt injection buried in a retrieved web page, a malicious README, or a poisoned tool response can escalate into code execution. Consider a concrete chain: an agent is asked to "set up this repo and run the tests." The repo's setup.py contains a post-install hook that reads AWS_ACCESS_KEY_ID from the environment and POSTs it to an external host. The agent did exactly what it was told; the attacker got credentials. Once that happens, the blast radius includes data exfiltration, resource abuse (crypto miners, runaway loops), lateral movement into adjacent services, and persistent filesystem tampering — a miner dropped in a sandbox that survives between sessions keeps billing you long after the task ends. The agent isn't the adversary — it's the confused deputy: it holds credentials and network reach the model was never meant to extend to an attacker's payload, and it executes that payload with your authority. (See: prompt injection defense patterns.)
Static analysis and input filtering don't solve this. You can't pattern-match generated code reliably, and you can't predict which legitimate-looking snippet will curl an internal endpoint or read an environment variable. Filtering also breaks the feature: agents need broad execution freedom to be useful. Block subprocess and the agent can't run tests; block outbound HTTP and it can't install a dependency. The only durable control is runtime isolation — assume the code will be hostile and constrain what it can touch when it runs.
That reframes the decision. Sandboxing isn't a feature toggle you flip in a config file. It's an architecture decision about which isolation primitive sits between untrusted generated code and your production systems — and how much latency and operational complexity you'll pay for that boundary. The rest of this article maps the isolation stack, then compares two of the most common managed options, E2B and Daytona, against that stack.
The Isolation Stack: What "Sandboxing" Actually Means
"Sandbox" is an overloaded term. In practice, isolation for AI agent code execution is a stack of layers, each enforcing a boundary at a different depth. The strongest deployments use several at once — defense in depth, not a single silver bullet.
Isolation Primitives Compared
- Containers (namespaces + cgroups): Share the host kernel. Fast, mature, ubiquitous. But a kernel exploit or a misconfigured capability drops you onto the host. A container run with
--privileged, or one that mounts the Docker socket, is effectively no boundary at all. Weakest boundary, lowest overhead. - gVisor: A user-space kernel (the Sentry) that intercepts syscalls, shrinking the host kernel attack surface. Stronger than plain containers, but with real compatibility gaps:
io_uring, someioctlpaths,perf_event_open, and GPU passthrough are either unsupported or require the ptrace/KVM platform tradeoffs. Expect to validate syscall-heavy or GPU workloads before committing — a Python data-science stack that depends on a specificioctlpath can fail in ways that are painful to debug. - MicroVMs (Firecracker, Cloud Hypervisor): Hardware virtualization with a minimal device model. Each workload gets its own kernel. Near-VM isolation with container-like startup — Firecracker's own boot is roughly 125ms, but a full agent sandbox (VM + guest agent + runtime + filesystem) typically lands in the low hundreds of milliseconds to a couple of seconds.
- WASM: Language-runtime isolation in a capability-based sandbox. Extremely fast and fine-grained, but security is only as strong as the host runtime and the capabilities you import — a permissive WASI host is a weaker boundary than a microVM. Limited to compiled languages and constrained system access — poor fit for arbitrary Python or shell.
| Primitive | Isolation strength | Cold-start | Compatibility | Ops overhead |
|---|---|---|---|---|
| Container | Low–medium | Very fast | Excellent | Low |
| gVisor | Medium–high | Fast | Good (syscall/GPU gaps) | Medium |
| MicroVM | High | Fast (~100ms–2s end-to-end) | Excellent (full VM) | Medium–high |
| WASM | High if the host is correct | Fastest | Limited | Medium |
The Threat Model for Code-Executing Agents
Before picking a primitive, write down what you're defending against:
- Untrusted code execution — the agent runs code you didn't author.
- Network egress — the code calls out to attacker infrastructure or internal services.
- Filesystem persistence — state survives between sessions or leaks across tenants.
- Multi-tenant blast radius — one tenant's code compromises another's.
- Resource abuse — CPU, memory, and GPU exhaustion.
No single primitive covers all five. MicroVMs give you the strongest kernel boundary; you still need explicit network policy, ephemeral storage, and per-tenant quotas on top. Map each threat to a control and note which ones the primitive handles for free versus which you must build.
The egress layer deserves its own paragraph because it's where most real incidents happen. Default-deny outbound traffic and allowlist only what the task needs — a PyPI mirror, a specific API host, nothing else. Block the cloud metadata endpoint (169.254.169.254) and link-local ranges so generated code can't harvest instance credentials; on AWS, IMDSv2 with a hop limit of 1 and a token requirement is a meaningful backstop, but don't rely on it alone. Resolve DNS through a controlled resolver so exfiltration over DNS is visible. And never hand a sandbox your production service credentials — issue short-lived, scoped tokens per session, ideally with a TTL measured in minutes and an audience restricted to the one API the task needs.
E2B — Firecracker MicroVMs for Agent Code Execution
E2B built its sandbox around Firecracker microVMs, the open-source virtualization project AWS created for Lambda. Each sandbox is a lightweight VM with its own kernel, spun up per session, giving agent code a hardware-level boundary from the host and from other tenants. Note that Lambda's isolation is Firecracker plus a large proprietary control plane; if you self-host E2B, you own the orchestration, capacity, and patching that AWS abstracts away.
Architecture and Isolation Guarantees
The model is SDK-first: your agent calls into E2B to create a sandbox, execute code, read files, and tear it down. Sandboxes are ephemeral by default, with isolated filesystems that don't persist unless you explicitly snapshot. Because each session runs in its own microVM, a kernel-level exploit in one sandbox doesn't reach the host or sibling workloads. Network access is controllable, so you can restrict egress rather than trusting the code to behave.
Startup is fast enough for interactive agent loops — Firecracker boots in ~125ms, and a full sandbox with a Python runtime is ready in a fraction of a second to a couple of seconds depending on image and region — which matters when an agent may spawn dozens of short-lived execution contexts per task. A typical pattern is one sandbox per tool call: the agent writes a script, runs it, reads stdout, and discards the VM. That pattern is cheap precisely because startup is sub-second and teardown is a VM kill rather than a filesystem cleanup.
Where E2B Fits (and Its Tradeoffs)
E2B is purpose-built for ephemeral code evaluation: run this snippet, return the output, discard the environment. That focus is its strength and its limit.
- Strong isolation from microVMs at near-container latency.
- Simple SDK ergonomics tuned for agent frameworks.
- Self-host or cloud — you can run it on your own infrastructure, which matters for regulated workloads.
Tradeoffs: microVMs carry more per-sandbox overhead than containers, so very high-density, ultra-short workloads may cost more than a container approach. Persistence is deliberately minimal — long-running, stateful workloads (a dev server, a database, a multi-step build) need explicit snapshot/restore or a different runtime, and snapshot/restore adds latency and a state-management problem of its own. And self-hosting means you own the Firecracker fleet: capacity planning, host patching, and the control plane are yours. The failure mode to plan for is a fleet that runs out of capacity mid-task — an agent that can't get a sandbox stalls, and a retry loop against a saturated pool makes it worse.
Daytona — Dev-Environment Sandboxes for Agents
Daytona approaches the same problem from the developer-environment side. Where E2B optimizes for throwaway code evaluation, Daytona optimizes for stateful, long-lived sandboxes that behave like a full development environment: a persistent filesystem, a running process tree, language toolchains, and an API to create, start, stop, and snapshot workspaces.
Architecture and Isolation Guarantees
Daytona sandboxes run as isolated workspaces with their own filesystem and process space, created on demand through an API and SDK. The design target is the workload E2B treats as a limitation: agents that need to install dependencies, run a server, iterate across many turns, and preserve state between steps.
- Stateful by design — files and processes survive across calls within a workspace.
- Environment parity — the sandbox resembles a real dev box, so agent workflows that mirror human dev loops work without shoehorning.
- Lifecycle control — explicit start/stop/snapshot semantics let you bound cost and reset state deliberately.
Where Daytona Fits (and Its Tradeoffs)
Daytona is the better fit when the agent's job is building and iterating, not just evaluating a snippet: coding agents, CI-style automation, long-horizon tasks. The tradeoff is that statefulness is a security surface — a persistent workspace accumulates whatever the agent installed or wrote, including a backdoor planted on turn three that the agent (or a later injection) triggers on turn thirty. Treat snapshots as untrusted artifacts, scan them, and expire workspaces on a fixed schedule rather than letting them live indefinitely. Cost also scales with wall-clock lifetime, not just compute, so a forgotten running workspace bills continuously. The practical rule: use Daytona when the task genuinely needs continuity, and E2B when it doesn't — and default to the ephemeral option when you're unsure.