Skip to content
Blog

Code execution

Code execution sandbox for AI agents: a developer guide

Platinum team2 min read

Choose a code execution sandbox for AI agents by its isolation boundary, what survives between calls, and what the code may reach, with a runnable SDK example.

A code execution sandbox runs the code your agent writes where it cannot touch your app, your keys or other users. Choose one by three things: the isolation boundary, what survives between calls, and what the code may reach. Startup speed comes after.

Keep the agent and the executor apart#

Your service holds the model keys and decides what runs. The sandbox gets the code, a time limit and resource caps. It returns the exit code, stdout, stderr and any files. Treat that output as untrusted input too.

Answer four questions before running anything:

QuestionDecide
AuthorityWhich files, hosts and credentials can the code reach?
ResourcesHow much CPU, memory, disk, time and output per call?
StateDoes the next call reuse this sandbox, restore files, or start empty?
OwnershipWhich user owns the sandbox, and who may reconnect to it?

Bind each sandbox to the authenticated task in your own service. Never let the model choose a sandbox ID.

Choose the isolation boundary#

Three stacks: in a container, agent code calls the shared host kernel directly; gVisor's user-space kernel intercepts the calls so fewer reach the host; a microVM runs its own guest kernel behind a KVM boundary and reaches the host only through the VMM.
The boundary decides what a kernel bug in agent-written code can reach.
ApproachBoundaryTest for
Linux containerNamespaces and cgroups on the shared host kernelKernel exposure. Never mount the Docker socket.
gVisorA user-space kernel answers the code's system callsCompatibility and syscall-heavy workloads
MicroVMA separate guest kernel behind hardware virtualizationGuest images and VM lifecycle

None of them replaces credential scoping or network policy. Sources: Docker security, gVisor, Firecracker. Each Platinum sandbox is its own microVM.

Know what survives#

"Persistent" is not a specification. On Platinum:

OperationWhat survives
Stop, then start with memory restoredMemory, processes and disk
Stop, then start disk-onlyFiles only
Fork or cloneThe disk, in a new sandbox that boots fresh

Check memory_saved on stop and restore on start (details). A fork does not undo an email or API call already sent, so give speculative branches narrow authority.

Run agent code with the SDK#

You need Node 18 or later and an API key from the quickstart in PT_TOKEN. This creates billable compute.

bash
npm install @platinum-dev/sdk

Save this as run-agent-code.mjs:

js
import { Platinum } from "@platinum-dev/sdk";

if (!process.env.PT_TOKEN) throw new Error("Set PT_TOKEN server-side");
const client = new Platinum({
  url: "https://api.platinum.dev",
  token: process.env.PT_TOKEN,
});

const agentWrittenCode = "print(sum([17, 23, 41]))";
const sbx = await client.sandboxes.create(
  { template: "pt-base", auto_stop_minutes: 5 },
  { waitForRunning: true },
);
try {
  const result = await sbx.runCode(agentWrittenCode, {
    lang: "python",
    timeoutMs: 10_000,
  });
  result.check();
  process.stdout.write(result.stdout);
} finally {
  await sbx.delete();
}

node run-agent-code.mjs should print 81. The finally deletes the sandbox even when the code fails. Auto-stop is only a backstop, and a stopped sandbox keeps its disk. The timeout does not restrict the network: set network policy before running untrusted code.

Pick by workload#

  • Container: you control the code and already harden containers.
  • gVisor: you want a syscall boundary, and your dependencies pass testing.
  • MicroVM: you need a separate kernel for every run.

Before rollout, test a timeout, an out-of-memory kill, a disk-only restart and a failed cleanup. To compare providers, see agent sandboxes compared and the E2B, Daytona and Modal pages.