How to Add an AI Agent Sandbox to Your Product Without a Full Rearchitecture (2026): What Stays, What Moves, and the Five Calls
Keep your agent loop, auth, and database where they are, and add the AI agent sandbox as one tool your backend calls over an API: create a session, put files in, run a command, get files out, delete the session. Set CPU,
Keep your agent loop, auth, and database where they are, and add the AI agent sandbox as one tool your backend calls over an API: create a session, put files in, run a command, get files out, delete the session. Set CPU, memory, and process limits, an outbound allow list, and a timeout on every session, and keep secrets in your backend. On Control Plane, an agent sandbox is an HTTP API behind a token, and each task runs in its own session inside a sandbox that has a dedicated microVM, so your product adds one API client and keeps its architecture.
Disclosure: Control Plane publishes this guide.
TL;DR
An AI agent sandbox joins an existing product as a single tool that your backend calls.
- Your agent loop, model calls, conversation state, user auth, billing, and production database stay in your backend.
- The sandbox interface is five calls: create session, put files, run command, get files, delete session.
- Secrets and API keys stay out of the sandbox, and your backend makes any call that needs a credential.
- Every session gets CPU, memory, and process limits, a timeout, and an outbound allow list.
- Each task or user gets its own session, so one run cannot read another runβs files.
- Control Plane Sandboxes give you that interface as a managed API: sessions, limits, outbound rules, and logs, with no cluster or virtual machine fleet for your team to operate.
How do I add an AI agent sandbox to my product without a full rearchitecture?
Treat the sandbox as an execution service that your backend calls for one narrow task, and leave agent orchestration, model calls, and user data where they already run.
- Pick one bounded capability. Start with a single workflow that needs code execution or file transforms, such as a data analysis script, a CSV transform, or a build check.
- Wrap it as one tool in your agent βs tool list. Your agent loop already routes tool calls, so the sandbox becomes one more tool with a typed input and a typed output. The Model Context Protocol tools specification says there should always be a human in the loop with the ability to deny tool invocations, and the tool wrapper is where your backend enforces that.
- Create a session per task. When the agent invokes the tool, your backend creates a fresh session with CPU, memory, and process limits, a timeout, and an outbound allow list.
- Pass inputs as files and arguments. Copy in only the files the task needs, and pass no credentials.
- Read results and artifacts back. When the command finishes, your backend reads stdout, stderr, the exit code, and any generated files, then validates them before anything reaches the user.
- Delete the session. Remove the session once you have the results, and keep its logs outside the sandbox for later review.
The OpenAI Agents SDK sandbox guide describes the same boundary as the split between the harness and compute. The harness owns the agent loop, model calls, tool routing, approvals, tracing, recovery, and run state. The sandbox is the execution plane and runs code with narrow credentials and mounts. Teams that have no agent in the product yet can start with the infrastructure checklist in adding AI features to an existing SaaS product.
What stays in your product, and what moves into the sandbox?
Anything that touches auth, state, secrets, or business logic stays in your backend, and anything that executes model-written code moves into the sandbox.
| Concern | Stays in your backend | Runs in the sandbox | Why |
|---|---|---|---|
| Agent loop and model calls | Yes | No | The harness owns orchestration, retries, and recovery, and holds run state outside any session |
| Conversation and task state | Yes | No | State spans many tool calls and outlives any one session |
| User auth and permissions | Yes | No | Your backend decides what a user may do before it creates a session |
| Production database access | Yes | No | The sandbox holds no connection string; your backend passes in the rows a task needs as files |
| Secrets and API keys | Yes | No | Credentials are runtime configuration, kept out of prompts, task files, and generated artifacts |
| Code and shell execution | No | Yes | Model-written code is untrusted input, and this is the work the sandbox exists for |
| File transforms and generated artifacts | No | Yes | Artifacts are created in the session workspace and copied out when the command finishes |
| Package installs | No | Yes | Installing third-party packages runs their code, so it belongs inside the isolation boundary |
| Previews of generated apps | No | Yes | A preview serves a port from a session that stays open while the user views it, and the preview has no route into your backendβs network |
| Approvals for high-impact actions | Yes | No | The review gate sits in the harness, outside the code under review |
| Audit log | Yes | No | Your backend records who asked, what ran, and the result, and stores it outside the sandbox |
The split applies the OWASP guidance on excessive agency: minimize tool functionality and permissions, avoid open-ended tools where a narrower tool will do, and authorize each action in the downstream system.
What does the sandbox tool interface look like?
Your backend makes five calls, and the first four each carry a limit that caps what a session can consume.
| Call | What your backend sends | What comes back | Limit to set |
|---|---|---|---|
| Create session | Task ID, optional image, CPU, memory, and process caps, a timeout, an outbound allow list | Session ID and a session token that your backend sends on the other four calls | CPU, memory, and process caps (on Linux these map to cpu.max, memory.max, and pids.max); session timeout; outbound allow list |
| Put files | Session ID, file paths and contents | Acknowledgment | Total input size |
| Run command | Session ID, command and arguments, environment variables with no secrets | stdout, stderr, and the exit code | Execution timeout and output size |
| Get files | Session ID, file paths | File contents | Total output size |
| Delete session | Session ID | Confirmation | None; the sessionβs resources are released |
Your backend controls what goes in and what comes out. A sandbox session reaches your backend or the internet only where the outbound allow list permits it.
Should the agent run inside the sandbox or call it as a tool?
Call the sandbox as a tool, because that pattern leaves your existing architecture unchanged.
LangChainβs write-up of the two patterns describes both. In the first pattern the agent runs inside the sandbox. It mirrors local development. Its costs for an existing product are that API keys must live inside the sandbox, agent updates require rebuilding the image, and you build the network layer to talk to the agent unless your sandboxβs SDK provides it.
In the second pattern the sandbox is a tool the agent calls over an API. API keys stay outside the sandbox. Agent code updates need no image rebuild. A sandbox failure does not lose the agentβs state, because the harness still holds it. Sandboxes can run in parallel. Each call crosses the network, and a session that stays open across several commands keeps round trips low.
For a product that already exists, the second pattern means the agent loop, model client, and tool router stay where they are and your backend gains one HTTP client. The sandbox provider also becomes part of the run configuration, separate from the agent definition, so you can change where code executes without rewriting the agent.
Which isolation option fits an existing product?
The options differ in where the isolation boundary sits and in who operates it, and the criteria for choosing an AI agent sandbox cover the wider decision.
| Option | Isolation boundary | Who operates it | Fits when |
|---|---|---|---|
| Control Plane agent sandbox | A dedicated Kata Containers microVM for each sandbox; each session inside it has its own workspace, network namespace, and token | Control Plane; your backend calls the API | You want the five-call interface, limits, outbound rules, and session logs ready to use, and your teamβs time spent on the product |
| Container on a shared kernel | Processes on a kernel shared with other containers on the host | Your team | The code is your own or already trusted |
| User-space kernel (gVisor) | An application kernel in user space that intercepts the applicationβs system calls | Your team, as a container runtime | You want a narrower path to the host kernel and accept reduced application compatibility and higher per-system-call overhead |
| MicroVM you operate yourself (Firecracker) | A microVM created through the Linux Kernel-based Virtual Machine (KVM) with a minimal device model | Your team, including hosts, images, networking, and storage | You want a virtual machine boundary and have engineers to run the fleet |
| WebAssembly runtime (WASI) | A capability-based sandbox that starts with no ambient authority | Your team, inside your own services | The task compiles to WebAssembly and needs only what the host explicitly grants |
| Kubernetes with the agent-sandbox controller | Delegated to a runtime such as gVisor or Kata Containers; the project adds Sandbox, SandboxTemplate, SandboxClaim, and SandboxWarmPool resources | Your team operates the cluster and the controller | You already run Kubernetes and want sandboxes declared as cluster resources |
The Kubernetes documentation describes containers as processes on a shared kernel and calls that less secure than a virtual machine with its own kernel. Kata Containers runs each workload in a lightweight virtual machine with a dedicated kernel and describes hardware virtualization as a second layer of defense, and Firecracker puts the same kind of virtual machine barrier around each workload. For a product where one customerβs code must never affect another customer, a virtual machine boundary is the safer default. Teams that plan to run the isolation layer themselves can compare the work involved in self-hosted AI agent sandboxes.
How do you keep secrets and customer data out of the sandbox?
Assume the code in the sandbox can be steered by a prompt injection, and give it only the taskβs own inputs and no route to anywhere else.
OWASPβs prompt injection entry says it is unclear whether fool-proof prevention exists. It recommends least privilege, API tokens held by the application, and human approval for high-risk actions.
- No production credentials in the sandbox. Database passwords, cloud keys, and internal service tokens stay in your backend.
- A host-side proxy for calls that need credentials. When sandboxed code needs an authenticated API, your backend makes the call and returns only the response. Your backend authenticates the session, allows only named endpoints and methods, and applies the requesting userβs permissions.
- An outbound allow list on every session. Limit outbound traffic to the hostnames the task needs. Many runtimes start open. In Kubernetes a pod allows all outbound connections until a NetworkPolicy with an egress policy type selects it, and default-deny egress is a documented pattern.
- Only the task βs files. Copy in the minimum data the task needs, and mount no shared volumes or configuration from your backend.
- One session per task or user. Separate sessions keep one runβs files out of another runβs reach, and tenants that must not share a kernel get separate sandboxes, each behind its own virtual machine boundary. The wider set of controls is in multi-tenant isolation for SaaS and AI.
- Approval before high-impact actions. Deletions, payments, deployments, and external writes wait for a person in the harness before they execute.
How do you roll it out without breaking what works?
Add one layer at a time, and move on only when the pass test for the current phase succeeds.
| Phase | What you add | Pass test before the next phase |
|---|---|---|
| One tool behind a feature flag | A single sandbox-backed tool for internal users, with CPU, memory, and process caps, a session timeout, and outbound traffic closed | The tool returns correct results on your test tasks, and with the flag off the tool is absent from the tool list and no sessions are created |
| Cap tests and outbound allow list | Caps tuned to what your test tasks use, plus an allow list for tasks that need the network | A fork bomb hits the process cap, a memory-hungry process is killed at the memory cap, a busy loop ends at the timeout, and a request to a host that is off the list is blocked and logged |
| Files and artifacts | File input and output through the put and get calls, with artifact validation in your backend | Malformed files are rejected by your validation rules, oversized outputs are rejected, and a deleted sessionβs files cannot be read |
| Tenant separation and approvals | Sessions keyed to the user and tenant that own them, and approval gates for high-impact actions | A session for tenant A cannot read tenant Bβs files, and an unapproved high-impact action does not run |
| Scale and cost controls | Parallel sessions, a cap on concurrent sessions per tenant, usage recorded per tenant, and cleanup of idle sessions | Session creation succeeds at your expected peak, usage is attributed to a tenant, and no session outlives its timeout |
How Control Plane fits
Control Plane handles Day 2 operations for you, including autoscaling, built-in observability, and security.
Control Plane Sandboxes run in dedicated Kata Containers microVMs with a persistent home volume. There are two types: a dev sandbox for a person and an agent sandbox for AI agents. An agent sandbox serves an HTTP API secured by a token that Control Plane generates. Agents run in sessions. A session is an isolated slot with its own workspace, its own network namespace, its own access token, and optionally its own container image and CPU, memory, and process limits.
Those sessions map directly onto the five calls. Your backend creates a session, copies files in, runs a command and reads stdout, stderr, and the exit code, copies files out, and deletes the session, through the sandbox API or the cpln sandbox session and cpln cp commands. A session can carry a timeout after which it ends. Outbound traffic can be limited to named hostnames for the whole sandbox or for one session, and a session can have outbound traffic closed entirely.
Each session keeps a log and a resource series that outlive the session, so you can review what an agent ran after the session is gone. The agent sandboxes bound to the same Control Plane identity form a pool. Control Plane places a new session on an agent sandbox with room, adds another when all are full, and removes the ones it added once they sit empty. A session can expose a port as a preview URL that opens with a one-time code.
An idle sandbox scales to zero and resumes in place on the next request, with its home volume intact. Secrets reach a sandbox only when you assign it a Control Plane identity and reference the secret, as the sandbox concepts page and the agent sandbox guide describe, so by default a sandbox holds none of your secrets. The agent that calls the sandbox can run as a Control Plane workload next to the rest of your product, or stay where your product runs today and call the sandbox API over HTTPS. Sandboxes are for building and running agent tasks, and production services run as Control Plane workloads.
Control Plane is PCI DSS Level 1, SOC 2 Type II, HIPAA, and GDPR compliant. The wider field is compared in the best AI agent sandboxes of 2026.
FAQ
Each answer applies to a product that already has users, a backend, and a database.
How do I add an AI agent sandbox to my product without a full rearchitecture?
Add the sandbox as one tool your backend calls over an API, and leave the agent loop, auth, and database in place. The interface is five calls, and every session needs resource caps, a timeout, and an outbound allow list.
Do I need to rewrite my agent to use a sandbox?
No. The sandbox is one more entry in the agentβs tool list, so the agent loop, model calls, and tool routing stay in your backend. The only new code is the tool wrapper and an API client.
Should each user get their own sandbox?
Each task or user should get its own session. A session has its own workspace and network settings, which keeps one runβs files away from another. Customers whose code must stay apart at the kernel level get a sandbox each. On Control Plane, create one identity per tenant. Sessions created for that identity run on that identityβs agent sandboxes, and each of those sandboxes is a dedicated Kata Containers microVM.
Can the sandbox reach my production database?
It should not. Database credentials stay in your backend, so sandboxed code has nothing to connect with. When a task needs data, your backend queries the database under the userβs permissions and copies the result in as a file.
How do I stop sandboxed code from sending data out?
Set an outbound allow list on every session, and close outbound traffic entirely for tasks that need no network. Keep secrets out of the sandbox as well, so code that reaches an allowed host can send only the taskβs own input files.
What happens to files when a sandbox session ends?
The sessionβs workspace is deleted with the session, so copy results out first. Keep your audit log in your backend. On Control Plane, a sessionβs log and resource series stay readable after the session is deleted.
Do I need Kubernetes to run agent sandboxes?
No. Kubernetes with the agent-sandbox controller is one option for teams that already operate a cluster. On Control Plane an agent sandbox is an HTTP API your backend calls over HTTPS, with no cluster or controller for your team to run.
How do I add an AI agent to my existing SaaS product?
Start with one bounded workflow. Build an agent loop in your backend that calls a model and routes tools, expose a few existing APIs as typed tools, and add a sandbox as the tool for tasks that run code. On Control Plane the agent runs as a workload next to your services and calls the sandbox over the API.
Is a container enough to sandbox AI-generated code?
For your own trusted code, yes. For code a model writes on behalf of many customers, put a virtual machine around it, because containers on one host share that hostβs kernel.
Talk to the Control Plane team and sign up free.
Originally published on the Control Plane blog.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.