Agents, Tool Use & MCP·Question 46 of 90Advanced

How do you sandbox a code-executing agent?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Treat model-generated code as untrusted input - hostile until proven otherwise - because it fails in two ways: honest bugs (rm -rf the wrong directory) and injected malice (the code was shaped by a prompt-injected instruction). The sandbox is the boundary that makes both survivable.

Full worked answer

Treat model-generated code as untrusted input - hostile until proven otherwise - because it fails in two ways: honest bugs (rm -rf the wrong directory) and injected malice (the code was shaped by a prompt-injected instruction). The sandbox is the boundary that makes both survivable.

Layers, from inside out:

  • Isolation primitive. Containers are the baseline; for stronger isolation use gVisor (syscall interception) or Firecracker-style microVMs - the standard for multi-tenant code execution services, since container escape is a real (if hard) attack class and a kernel boundary is materially stronger. Ephemeral instances: fresh per session, destroyed after, with snapshot/restore if you need fast warm starts.
  • Filesystem scoping. Mount only the task workspace, read-only where possible. The most common real-world failure isn't sandbox escape - it's mounting secrets into the sandbox: .env files, cloud credentials, SSH keys, a token-bearing ~/.gitconfig. Audit the mount list like a security boundary, because it is one.
  • Network policy. Default-deny egress. Allowlist what's genuinely needed (package registries, specific internal APIs) - unrestricted egress from a sandbox converts any prompt injection into an exfiltration channel, silently reassembling the lethal trifecta inside your "safe" environment.
  • Resource limits. CPU, memory, disk, process count, wall-clock timeout. Model-written code produces infinite loops and fork-bomb-adjacent accidents at a nontrivial rate; limits turn them into clean tool errors.
  • No ambient credentials. Empty environment by default; anything the code legitimately needs is injected scoped and short-lived, or better, brokered - the sandbox asks a proxy that enforces per-operation policy rather than holding raw keys.
  • Output handling. Stdout/stderr, truncated and cleaned, returned as the tool result; artifacts exchanged via an explicit, size-limited channel rather than arbitrary filesystem reach-in. Remember sandbox output can itself carry injection payloads if the code processed untrusted data.

What a good answer covers

  • Treat model-generated code as untrusted input - hostile until proven otherwise - because it fails in two ways
  • The sandbox is the boundary that makes both survivable
  • Isolation primitive
  • Filesystem scoping
  • Network policy

Key terms: Isolation primitive · Filesystem scoping · Network policy · Resource limits · No ambient credentials · Output handling

Interviewer follow-ups to expect

  1. Does the calculus change for an agent running on the user's own machine (Claude Code-style) vs a hosted service?

  2. How would you let sandboxed code use pip install safely?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep