skip to content
jaeaeichsysdes

Design an Agent Sandbox Platform (E2B / Daytona)

Sub-2s isolated code execution for 1,000+ concurrent AI agents: prewarmed Firecracker microVMs with CoW repo snapshots, host-side L7 Envoy egress & secret injection, S3 presigned artifacts, and dual-layer TTL reaping.

The whiteboard

Trace the 4 flows below to see sub-2s microVM provisioning, zero-trust egress, live vs. S3 file retrieval, and zombie-VM reaping. Builds on Distributed Locks & Leases, Queues vs Event Logs, and Design an Enterprise LLM Gateway.

whiteboard · press 1–5 to trace flows

Control Plane & SchedulingExecution Node (Firecracker microVMs · <2s Startup)Artifacts, Hard-TTL Reaper & Observabilityrecord sandboxexec / streamvsockupload on canceltelemetryretrysys_error / reapAgent / Clientcreate · execfiles · cancelSandbox APIgateway + authroute by node_idSchedulerpick healthynode + adminRedisheartbeat TTLnode capacityPostgressandboxesexpires_atExec Daemonlifecycle · vsock30m local TTLFirecracker VMprewarm (~150ms)CoW repo snapL3/L4 iptablesblock VM-to-VMblock 169.254.*L7 Egress Proxydomain allowlistinject auth hdrS3 → Glacierartifacts / snappresigned URLsKafkalogs · metricstenant/run/traceRecon / Reaperdead TTL → errexpires_at sweepOrchestratorTemporal retryfresh sandbox

Full platform: prewarmed Firecracker microVMs + CoW snapshots · host iptables + L7 Envoy secret injection · S3 artifacts & dual-layer TTL reaper1,000+ VMs · <2s startup · hard isolation

Scale & APIs

1,000+

concurrent active sandboxes (10s–30m runs)

< 2 s

startup with repo & deps mounted

0 secrets

raw credentials stored on VM filesystem

POST /sandbox -> 200 { sandbox_id }
POST /sandbox/{id}/exec -> streams stdout/stderr (SSE / WS / gRPC) + exit_code
GET /sandbox/{id}/files?p= -> live stream (running) OR S3 presigned URL (archived)
DELETE /sandbox/{id} -> 200 OK (upload artifacts to S3 & destroy microVM)

How it works

1. Control plane & heartbeat scheduling

  • Capacity-aware scheduling: exec daemon refreshes a Redis lease (heartbeat TTL) with available CPU/RAM slots. scheduler picks a healthy node from Redis and writes (sandbox_id, tenant_id, node_id, status, expires_at, s3_path) to Postgres.
  • Admin control plane: Registers/drains execution nodes (write-through to Redis) and pushes egress allowlist policies.

2. Compute isolation & sub-2s startup (execution node)

  • Firecracker microVMs over containers: Untrusted AI-generated code can exploit kernel CVEs in shared-kernel containers; Firecracker gives each sandbox its own minimal Linux guest kernel via KVM with strict cgroups/disk/PID quotas and vsock command transport (no SSH daemon needed).
  • Prewarm pool (~150ms) + CoW snapshots: Pre-booted blank microVMs eliminate kernel boot latency, and mounting a Copy-on-Write (OverlayFS / ZFS) pre-baked repo + dependency snapshot eliminates 15–30s git clone / npm install delays.

3. Zero-trust network & secrets control

  • L3/L4 iptables: Per-VM network namespaces block lateral VM-to-VM traffic and cloud metadata endpoints (169.254.169.254).
  • Host-side L7 Egress Proxy (Envoy): Domain allowlisting (api.github.com, registry.npmjs.org, llm-gateway.internal) breaks with raw IP firewall rules because CDNs rotate IPs. Routing all egress through host Envoy solves both default-deny domain filtering and credential injection.
  • Zero raw secrets on VM disk: Never mount long-lived Vault secrets into the VM where prompt-injected code can cat them. Inject only short-lived scoped tokens (e.g., 15-min GitHub App token) or let host Envoy attach auth headers on outbound requests.

4. Artifacts, file retrieval & cold archival

  • Live vs. terminated routing: files resolves node_id via Redis/Postgres and streams directly from exec daemon while running. On DELETE /sandbox/{id} (or completion), exec daemon uploads workspace artifacts to S3 and destroys the VM; future GET /files calls return an S3 presigned URL.
  • Lifecycle archival: S3 lifecycle rules transition aged artifacts to Glacier after 1 year and update Postgres metadata.

5. Dead-node recovery, hard TTL reaper & observability

  • Dead-node detection (recon): Watches Redis heartbeat TTL; if an execution node vanishes, recon marks its sandboxes sys_error in Postgres so upstream durable orchestrators (Temporal) retry cleanly in a fresh sandbox.
  • Dual-layer hard TTL kill-switch: exec daemon enforces a local hard timeout (e.g., 30 min) AND recon sweeps expires_at < now() in Postgres — killing runaway infinite-loop agents even on healthy nodes.
  • Tagged telemetry: exec daemon streams lifecycle events, CPU/RAM metrics, and logs to Kafka tagged with (tenant_id, run_id, trace_id).

lesson

Why L7 Envoy on the host beats pure iptables: Raw IP rules cannot reliably allowlist api.github.com or registry.npmjs.org behind rotating CDN IPs — and a host-side proxy lets you inject API/Git credentials outside the VM so untrusted agent code never sees raw tokens.

Check yourself

interactive · stress-test this sandbox design against security & infra probes

01 · Why Firecracker microVMs instead of standard Kubernetes containers?

untrusted code · multi-tenant · <2s boot

Your team already runs Kubernetes. Why pay the operational cost of Firecracker microVMs for running AI-generated agent code instead of regular Docker containers with resource limits?

02 · Why inject credentials in a host-side L7 proxy instead of VM env vars?

prompt injection · default-deny egress · zero exfiltration

An agent needs to call your internal LLM gateway and push a PR to GitHub. Why have the host-side Envoy proxy inject auth headers instead of passing API keys as environment variables inside the microVM?

03 · Why enforce a hard TTL in BOTH Exec Daemon and Recon?

zombie VMs · node crash · billing safety

Exec Daemon already runs a local 30-minute timer to kill runaway microVMs. Why does Recon also sweep Postgres for expires_at < now()?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme