Articles

AI Agent Sandboxing: How Agents Run Code Safely

Agent sandboxing isolates the code an AI agent executes from the host system, limiting what a compromised or misbehaving agent can actually reach.

Chisato Chisato · · 5 min read
A digital security shield icon

Agent sandboxing is the practice of isolating the code, commands, and file access an AI agent can execute from the rest of the host system, so that a bug, a misinterpreted instruction, or a successful prompt injection can’t reach beyond a tightly bounded environment. As agents have moved from answering questions to actually running shell commands, editing files, and calling external APIs, the sandbox has become the primary control standing between “the agent did something wrong” and “the agent did something wrong to production.”

Why agents need a different threat model than chatbots

A conventional chatbot’s worst-case output is bad text. An agent with tool access can execute that bad output — run a destructive shell command, delete files, exfiltrate data through a tool call, or make an API request an attacker engineered it into making. The attack surface isn’t just “what can the model say,” it’s “what can the model do,” and that shifts agent security much closer to classical software security than to content moderation.

This matters because the untrusted input an agent processes isn’t only the user’s own prompt. An agent that reads a webpage, a file, or an email as part of its task is also reading whatever text is embedded in that content — and if that content contains instructions crafted to redirect the agent’s behavior, the agent may follow them exactly as if a legitimate user had typed them. A sandbox doesn’t stop an agent from being manipulated this way, but it drastically limits what manipulation can accomplish by containing the blast radius of anything the agent actually executes.

What a sandbox actually restricts

A well-built agent sandbox typically layers several independent controls, since no single one is sufficient on its own:

  • Filesystem isolation. The agent operates inside a directory (or an entirely separate filesystem, as with a container) that doesn’t expose the host’s real files. Writes stay contained; reads can’t wander outside the sandbox boundary.
  • Process and resource limits. CPU, memory, and execution time are capped so a runaway or intentionally abusive process can’t degrade the host or run indefinitely.
  • Network egress control. Outbound connections are restricted to an explicit allowlist — or blocked entirely — so an agent can’t be used to exfiltrate data or reach internal systems it has no legitimate reason to contact.
  • No inherited credentials. The sandbox shouldn’t have automatic access to the host’s SSH keys, cloud credentials, or environment secrets. Any access the agent genuinely needs is granted explicitly and scoped narrowly, following the principle of least privilege rather than inheriting whatever the host process happened to have.
  • Ephemeral lifecycle. Many agent sandboxes are torn down and recreated fresh for each task or session, so state from one run — including anything an attacker managed to plant — doesn’t persist into the next.

Sandbox escapes are the failure mode that matters most

A sandbox is only as strong as its isolation boundary, and a sandbox escape — code that breaks out of the intended container and reaches the host, or reaches another tenant’s sandbox on shared infrastructure — is the worst-case outcome the whole design exists to prevent. Escapes typically exploit gaps between the sandbox’s intended policy and its actual implementation: a shared kernel with an exploitable vulnerability, a misconfigured mount that exposes more of the host filesystem than intended, or a tool the agent has access to that was never meant to be reachable from inside the isolated environment. This is conceptually the same problem as a general-purpose container escape, just with an additional wrinkle: the process attempting the escape may itself be steered by adversarial input the agent processed, rather than by an attacker with direct access to the sandbox.

Sandboxing vs. other agent safety controls

SandboxingOutput filteringHuman-in-the-loop approval
What it constrainsWhat the agent can execute or reachWhat the model is allowed to sayWhether an action happens at all
Stops a successful prompt injection from doing damageYes, by containing the blast radiusPartially, if the injected output is filteredYes, if the human catches it
Adds latencySome, from isolation overheadMinimalSignificant — blocks on a person
Where it fitsInfrastructure layerApplication layerWorkflow layer

These aren’t competing approaches — production agent systems generally use all three together. Sandboxing bounds the damage if something goes wrong regardless of cause; output filtering and approval gates try to catch problems before execution. A sandbox is the layer that still matters even when the earlier layers fail, which is exactly why it’s treated as a baseline requirement rather than an optional hardening step.

Where the model fits relative to the sandbox

It’s worth being precise about what a sandbox does and doesn’t protect against: it constrains the environment the agent’s tool calls run in, not the reasoning that decides which tool calls to make. An agent can still be manipulated into taking a pointless or wasteful action inside a perfectly secure sandbox. What sandboxing guarantees is narrower and more reliable — that whatever action gets taken can’t reach beyond a defined boundary, regardless of why the model decided to take it. That’s a much easier property to verify and enforce than trying to guarantee an agent will never be manipulated in the first place, which is why infrastructure-level containment has become the standard baseline for agent deployments even as prompt-level defenses continue to improve.

The takeaway

Agent sandboxing isolates execution — filesystem, process resources, network egress, and credentials — so that whatever an AI agent actually does stays contained within a defined boundary, independent of whether the agent’s behavior was manipulated by adversarial input. It doesn’t prevent an agent from being tricked, but it caps the cost of being tricked, which is why it functions as the baseline safety layer underneath output filtering and human approval rather than a replacement for either.