1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Sandboxing Agent Code

Run code an agent writes without trusting it: isolation levels from process sandboxes to gVisor and microVMs, a locked-down container, egress through a proxy, secrets kept outside, resource limits and managed sandbox defaults.

Last updated

After this section you can

  • Choose an isolation level (process sandbox, container, gVisor, microVM) for a given threat
  • Lock down a container for untrusted code, and route its egress through a proxy that holds the secrets
  • Spot the gaps in a sandbox design: open defaults, DNS, the metadata address, leaked timeouts
36

Running Agent-Written Code Safely: Sandboxes, Egress and Secrets

An agent that writes code and runs it is running code from a stranger: whoever wrote the web page, README or package it just read. The sandbox has to hold even when the model has been fooled.

Key idea

Assume the code is hostile and design so that does not matter. It gets compute and a scratch disk, and nothing else: no network except a proxy you control, no secret it can read, hard caps on CPU, memory, processes and time, and a fresh box per run or tenant. How strong the wall must be depends on who wrote the code and who else shares the host.

The boundary: compute inside, secrets and the only route out outside
ONE RUN OF AGENT-WRITTEN CODE Sandbox · one per run, thrown away after agent-written code and the packages it pulled in: untrusted read-only root 64 MB scratch no secrets caps: ½ CPU · 256 MB memory · 64 processes · 10 s network: none, except a socket to the proxy only way out Egress proxy outside the sandbox allows hosts by name adds the real token logs every request secrets vault api.partner.com ✓ attacker.example ✕ 169.254.169.254 ✕ cloud metadata service A fooled agent can still run anything inside the box. What it cannot do is read a secret or reach a host you did not allow.

Related

More in LLM & Agentic

Get full access to all 149 sections with code examples, diagrams, and interactive animations.

Unlock Premium