DEV Community

kosa8
kosa8

Posted on Originally published at kosa8.com

Secure sandbox infrastructure for AI agent tool execution

Secure tool execution means every tool call that touches code, files, a shell, a browser or the network runs inside isolated compute the agent cannot escape, with the rules decided outside it. In practice that is five layers: a sandbox with its own kernel per task or session, a network that is closed by default and enforced on the host, credentials added outside the sandbox, approval gates for consequential actions, and an audit record the agent cannot edit. Design it on the assumption that some agent will be steered by a prompt injection, and keep that agent's reach small.

What counts as tool execution

A model only produces text. Everything an agent does that has an effect goes through tools: a code interpreter, a shell, file reads and writes, a headless browser, MCP servers, HTTP calls to internal and external APIs, and database queries. Each one runs code on some machine with some permissions. The sandbox decides which machine and how much permission.

Tools fall into two groups, and most production agents have both:

  • Compute tools (interpreters, shells, builds, browsers, third-party MCP servers) run code you did not write. They need an isolation boundary.
  • Action tools (send an email, merge a pull request, issue a refund) call a real system with real authority. They need policy and approval more than compute isolation.

A reference architecture

Six layers, from the model outwards:

  1. Agent runtime. The loop that calls the model and picks the next tool. Treat everything it emits as untrusted input to the layers below.
  2. Tool gateway. Receives each call, checks it against policy (allow, deny, or ask a person), pins tool definitions so a server cannot quietly change what a tool does, and routes the call.
  3. Sandbox compute. One isolated machine per task, session or tenant, where code runs: its own kernel, a resource quota and one working folder.
  4. Egress guard. Every packet leaving the sandbox passes a policy point on the host. Deny by default, allow named hosts, and answer DNS for those names only.
  5. Credential broker. Holds the real keys outside the sandbox and adds them to outbound requests for the right host only.
  6. Audit and evidence. An append-only record of calls, decisions, approvals and network attempts, written outside the sandbox and forwarded off the machine.

Layers 2, 4, 5 and 6 sit outside the sandbox, and that is the point. Root inside the sandbox must not be able to change its own rules, read the real keys or rewrite its history. This is the zero trust model of NIST SP 800-207 (no implicit trust, least privilege per request, decisions made where the subject cannot change them) applied to a workload that takes instructions from whatever it reads. Zero trust sandboxes for AI agents maps each tenet to a control.

Requirements, and how to test each one

Test every requirement as root inside the sandbox, the position a hijacked agent would be in.

Requirement Why it matters How to check it
Own kernel per sandbox A shared-kernel bug reaches every tenant on the host Compare uname -r inside and on the host; confirm the runtime is a VM or a user-space kernel
Network denied by default, enforced on the host Exfiltration needs a route out Flush the firewall rules inside, then reach an unlisted host; it must still fail
DNS limited to allowed names DNS is a data channel and a discovery tool Resolve an unlisted name from inside; expect no answer
Metadata endpoints blocked Cloud metadata hands out credentials Request 169.254.169.254 from inside; expect failure
No real secrets inside Anything readable can be printed and sent Dump the environment and search the filesystem; find no live keys
Hard quotas Runaway loops and fork bombs Run a memory and process bomb; the sandbox stops, the host does not
Tenant separation in the API A guessed ID must not reach another tenant's sandbox Call another tenant's sandbox with your token; expect a refusal
Clean state per task Leftovers leak data and skew results Write a file in one task, start the next; the file must be gone
Approval for consequential actions Some actions cannot be undone Trigger a gated tool; it must wait for a named person
Tamper-evident audit You must be able to say what the agent did Verify the log, edit one entry, verify again; it must fail

Isolation choices for tool execution

Technology Boundary Fits tool execution when Watch out for
Container (runc) Shared host kernel; namespaces, cgroups, seccomp The tool runs your own reviewed code One kernel or runtime bug can reach the host
gVisor User-space kernel in front of the host kernel You want a smaller kernel surface on existing container tooling Not every system call is supported; syscall-heavy work is slower
WebAssembly Runtime with only the interfaces you grant Small, pure functions you can compile to Wasm Most agent tools expect a full Linux with pip and npm
MicroVM Own guest kernel behind hardware virtualization The tool runs code nobody reviewed, installs packages or runs unattended Memory per instance; you need KVM or a hypervisor framework
Full VM Own guest kernel, full device model Long-lived, rarely recreated environments Slow to create; heavy to run per task

For general tool execution the realistic choice is gVisor or a microVM. The trade-offs are set out in MicroVM vs container isolation for LLM code execution.

Session models and latency

Model Lifetime Use for Watch out for
Per call One tool call Stateless code interpreters, untrusted snippets Start-up cost on every call; no state between steps
Per session One task or conversation Coding agents, data analysis, browser agents Clean-up at the end; idle cost
Per tenant Long-lived, one customer Persistent workspaces Drift; never share across tenants
Forked Live copies of one running sandbox Parallel attempts, evals, RL rollouts Clone safety: duplicated IDs, random seeds and network identity

Latency decides which model is practical, and two techniques make per-session and even per-call virtual machines affordable. A warm pool keeps pre-booted sandboxes paused, so a create is a resume. A memory snapshot lets a prepared environment restore instead of booting. In kosa8's own benchmark on one server (3 Nov 2026), a create from the warm pool took 1.9 ms at the median and a restore from snapshot 74 ms. Under load the host's CPU and memory became the limit, so the pool has to grow and shrink with demand. The benchmark study has the hardware, method and raw figures.

MCP servers and third-party tools

An MCP server is third-party code that your agent calls with your data. Run each one in its own sandbox with its own allow-list, so a compromised server reaches only the hosts it needs and none of your files. Pin each server's tool definitions when you approve it, and hold any later change for review: a server that changes a tool's description after approval, sometimes called a rug pull, is a documented attack on agents. Give each server only the credential for its own API, through the broker. The MCP specification's security best practices cover the protocol side.

Human approval and a stop switch

Isolation limits what a hijacked agent can reach. Approval limits what it can do with the access it legitimately has. Gate the actions that are irreversible or costly: deleting data, moving money, sending messages outside the company, changing access and deploying. An approval should name a person, be recorded and be usable once. For the highest-risk actions, require two people.

Keep one control that stops everything at once: pause every sandbox, refuse new work and suspend keys. You will want it the first time an agent loops on an expensive call at night.

Build it or adopt it

You can assemble this yourself. On Linux with KVM, Firecracker, Cloud Hypervisor or Kata Containers give you microVMs, and gVisor gives you a user-space kernel for containers. What these leave to you is everything around the VM: networking and filtering, snapshot management, clone safety (Firecracker's documentation lists the duplicated identifiers, random state and network configuration to handle when one snapshot is restored into many VMs), credential brokering, approvals, a multi-tenant API and logging. Teams with platform engineers to own that stack do build it. Others adopt a finished layer. Either way, run the requirements table above against it before an agent does.

One way to do it: kosa8

kosa8 is Zero Trust cybersecurity infrastructure for AI agents, built as this layer. Each agent runs in its own microVM with its own kernel. Network egress goes to an allow-list enforced on the host, outside the microVM. Secrets stay outside the sandbox, and the host adds the real key on requests to that key's own host. Risky steps can wait for a person, with two-person approval where policy requires it. Third-party MCP servers can each run in their own sandbox behind one policy. Every action goes to a hash-chained, signed audit log, and kosa8 audit verify checks it. Checkpoint, rewind and fork work on a running machine.

kosa8 sandbox create sealed --egress-allow api.anthropic.com
kosa8 checkpoint sealed --label "before the risky bit"
kosa8 rewind sealed
kosa8 audit verify
Enter fullscreen mode Exit fullscreen mode

It runs on your computer (macOS on Apple silicon, Linux x86-64, Windows), on kosa8 Cloud (one region, us-ash1, in Dallas, Texas), on your own servers joined to kosa8 Cloud, or air-gapped. Tools that drive Docker can drive kosa8 by pointing DOCKER_HOST at it; containers started that way share one engine VM, so use kosa8 sandbox when each workload needs a VM of its own. The audit log, evidence export and signed policy are features of the software on your own machines and hosts; hosted Cloud sandboxes do not expose them yet.

What kosa8 does not do: it limits what a hijacked agent can reach, but it does not detect or prevent prompt injection. SOC 2 Type II is in progress; there is no SOC 2 report and no external penetration test yet.

Try free with no account at kosa8.com/try. Starter is free with no card; plans from \$29 a month.

Questions

What is an AI agent tool execution sandbox?

Isolated compute where an agent's tool calls run: code interpreters, shells, browsers and third-party tool servers. A secure one gives each task its own kernel boundary, a network that is closed by default and enforced outside it, no real credentials inside, and an audit record the agent cannot edit.

Does every tool call need its own sandbox?

No. Stateless code execution can use a fresh sandbox per call; coding and analysis agents usually keep one sandbox per task or session and throw it away at the end. What must never happen is one sandbox shared across users or tenants.

Where should the security policy live?

Outside the sandbox: network rules on the host, credentials in a broker, tool rules and approvals in a gateway, and the audit log written by something the agent cannot reach. Anything inside the sandbox can be changed by code running as root there.

How do I sandbox MCP servers?

Run each third-party MCP server in its own sandbox with an allow-list for the hosts it needs, give it only its own API credential through a broker, and pin its tool definitions when you approve it so that later changes need review.

Does sandboxing stop prompt injection?

No. Nothing stops an agent from reading a malicious instruction. Sandboxing, network policy, keeping secrets out and approvals limit what a hijacked agent can do with it.

Sources and further reading

Related


This article was written with AI assistance and reviewed by the kosa8 team. It was first published at kosa8.com.

Top comments (0)