Skip to content
AI Engineer Guide.

concept · concepts · intermediate · concept 36 of 37

Sandboxing

0 of 37 complete

Why it matters

An agent that writes and runs code, or drives a browser, will eventually run something wrong: a recursive delete, a request to an internal endpoint, a package installed from a typo. The model cannot be relied on to avoid these, and the consequences depend entirely on what the process may reach. Sandboxing converts a class of catastrophic outcomes into failed steps the agent can observe and correct.

Mental model

A sandbox is a fenced yard, not a trained dog. The metaphor breaks because a fence is a fixed perimeter, while an agent's needs change per task, so the boundary must be configured per run and is only as good as that configuration.

How it works

Two boundaries: what credential enters, which hosts it reachesInteractive
credential scope
egress allowlist
Step 1/6 · boundary
  1. trusted · host
    outside the boxtrusted
    Agent orchestrator
  2. must not crosstrusted
    Production credential
  3. neutral · sandbox · one run
    own credentialneutral
    Task credential · short-lived
    scoped to the run
  4. Agent orchestrator· task + tools
    Production credential· token mountedcredential scope · open
    untrusted workloadneutral
    Agent code + tools
    needed callegress allowlist · open
  5. untrusted · network
    needed calluntrusted
    Allowlisted host
  6. Agent code + tools· exfil / internalegress allowlist · open
    off-listuntrusted
    Any other host
trusted pathtainted pathcontrol openboundary holdspassespending approvaltrusted zoneuntrusted zoneneutral zone3 zones · 6 nodes · illustrative geometry

boundary. The orchestrator starts one run with a defined job. The boundary is chosen from the threat model: a container, a microVM or a separate account, each with a different kernel story.

Figure. The boundaries are the credential scope and the egress allowlist, drawn on separate edges. They refuse a mounted production token and connections off the list; they do not remove the authority of the task credential inside the box. Resource caps and kernel isolation are separate decisions and are not drawn. Shown with both controls open; use Blocked on each to apply it.

What to notice. Residual risk: a container shares the host kernel, so this is not the same boundary as a microVM. A permitted host that proxies to an unpermitted one bypasses the allowlist; log resolved destinations, not only hostnames.

Text version

Three zones: TRUSTED · HOST holds the orchestrator and a production credential; NEUTRAL · SANDBOX · ONE RUN holds the agent code and tools plus a short-lived task credential; UNTRUSTED · NETWORK holds an allowlisted host and any other host. A solid path runs from the orchestrator to the code, labelled task and tools. A dashed red path runs from the production credential to the code, labelled token mounted, and is the only edge under the credential-scope control. A solid path runs from the task credential to the code, labelled scoped to the run, and a solid path runs from the code to the allowlisted host. A dashed red path runs from the code to any other host. Both code-to-network edges are under the egress allowlist: open, both leave; blocked, the allowlisted call gets a teal tick and the off-list path is cut at a teal bar.

  1. Choose the isolation boundary: a container, a microVM, a separate account or a throwaway project.
  2. Mount only the directories the task needs, read-only where possible, and discard the filesystem after the run.
  3. Deny network egress by default and allowlist specific hosts and ports for the task.
  4. Inject short-lived credentials scoped to the task instead of mounting host or production secrets.
  5. Cap CPU, memory, disk and wall-clock so a runaway process fails fast rather than consuming the host.
  6. Capture stdout, stderr and exit codes as observations so the agent can react to a blocked action.

Trade-offs

ChoiceGood forCostFails when
Container on the hostFast start, simple toolingShares the kernel, weaker isolationCode is untrusted or hostile
MicroVM per runStrong isolation, clean teardownSlower start and higher overheadRuns are short and numerous
Full network accessFewest task failuresExfiltration and internal reachabilityThe agent reads untrusted content
Persistent workspaceState carries between stepsLeaks between runs and usersSeveral tenants share the sandbox
Trade-off mapInteractive

Good for

Fast start, simple tooling

Cost

Shares the kernel, weaker isolation

Fails when

Code is untrusted or hostile

  1. Code is untrusted or hostile
  2. Runs are short and numerous
  3. The agent reads untrusted content
  4. Several tenants share the sandbox

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: Container on the host is good for Fast start, simple tooling, costs Shares the kernel, weaker isolation, and fails when Code is untrusted or hostile. MicroVM per run is good for Strong isolation, clean teardown, costs Slower start and higher overhead, and fails when Runs are short and numerous. Full network access is good for Fewest task failures, costs Exfiltration and internal reachability, and fails when The agent reads untrusted content. Persistent workspace is good for State carries between steps, costs Leaks between runs and users, and fails when Several tenants share the sandbox

Failure modes

  • Secret in the environment. The agent reads a token from the environment and uses it outside its scope. Debug: enumerate environment variables and mounted files inside the sandbox and diff against the allowlist.
  • Egress allowlist bypass. A permitted host proxies to an unpermitted one. Debug: log resolved destinations, not just hostnames, and test with a known redirect.
  • Resource exhaustion. A runaway process starves the host and other runs stall. Debug: check cgroup limits and the distribution of run durations.

In production

Monitor sandbox start latency, blocked egress attempts and resource kills. Rehearse an escape attempt in staging and confirm the run fails without touching the host.

Common misconceptions

People think: A container is a security boundary by default.

Actually: A shared-kernel container limits damage but is not equivalent to a VM boundary against hostile code.

People think: Sandboxing slows agents down for little benefit.

Actually: Most of the cost is start time; the benefit is that a destructive step becomes a recoverable error.

Interview angle

Strong answers name the boundary, the egress policy and the credential model, then discuss start-up cost and what the agent sees when a call is blocked. Weak answers say the code runs in Docker and never mention network, secrets or resource limits.

Sources