Over the past few months I worked as a technical advisor to an enterprise agent company. I covered the agent runtime, and picking a sandbox was part of that job. Everything here comes from that work.
The company chose pi as its agent. pi ships with no sandbox by default, and its README says so plainly: "Pi does not include a built-in permission system for restricting filesystem, process, network, or credential access." Its bash tool spawns a shell subprocess on the host with pi's own permissions. Because the agent already runs in the company's own environment, the sandbox had to live there too: embedded in the agent process, so whatever the agent runs can't touch the host.
npm install and it works. No daemon, no root.python:slim or the repo's own test image, as-is. There are three established routes. OS-level process sandboxes are the lightest to integrate. Claude Code and Codex use this approach (Seatbelt on macOS, bubblewrap on Linux), though they share the host kernel and can't run container images. WASM sandboxes only run programs compiled to WASM and can't run shell commands. (Putting the whole agent inside a container is another route, not covered here.)
What this project needed was the third kind: a microVM embedded as a library, with one independent kernel per task and standard OCI images running directly. Several products do this now, and all of them need hardware virtualization: Apple Silicon on macOS or /dev/kvm on Linux. The differences show up in five places.
Integration cost comes first. BoxLite works right after npm install, with no root, no background service, and no extra process or account to maintain on the machine.
The host process matters too. The kernel boundary governs what happens inside the VM, while the process running the VM still sits on the host. Each BoxLite Box gets its own shim process wrapped in an OS sandbox: seccomp and cgroups on Linux, sandbox-exec on macOS.
Network defaults are worth checking closely. Omitting network in BoxLite means unrestricted outbound access, so you must write allow_net explicitly. Once set, hostname rules match on TLS SNI and HTTP Host, UDP only matches IP/CIDR rules, and private ranges, loopback, and link-local addresses are rejected. With an empty allowlist, host.boxlite.internal can reach services on the host's loopback, so the allowlist is never optional.
Persistence depends on how you create the Box. A Box with auto_delete=0 keeps its installed dependencies and written files across stop and restart. The default SimpleBox deletes itself when it stops.
Finally, look at SDKs and the path to servers. pi runs on Node, so the SDK is @boxlite-ai/boxlite. When you move to a server later, self-hosted boxlite serve and managed BoxLite Cloud use the same core concepts, so you don't have to rewrite your setup.
A pi tool is just a function with a parameter schema. Integration doesn't change the agent's structure: register a tool and hand its commands to BoxLite. The BoxLite part is about ten lines.
import { SimpleBox } from "@boxlite-ai/boxlite";
let box: SimpleBox | undefined;
// inside the tool's execute(): the first call starts the box, later calls reuse it
box ??= new SimpleBox({
image: "python:slim",
workingDir: "/workspace",
volumes: [{ hostPath: process.cwd(), guestPath: "/workspace", readOnly: true }],
network: {
outbound: { mode: "enabled", allowNet: ["pypi.org", "files.pythonhosted.org"] },
},
});
const r = await box.exec("sh", "-c", params.command);
That short snippet buys three things.
The repo is mounted read-only at /workspace. The agent can read code and run tests, and it can't write back to the host. Deleting files, editing configs, or wrecking the environment all land on the Box's own copy-on-write disk. The host never notices.
Outbound traffic is limited to PyPI. pip install works, while any other domain resolves and won't connect. If a web page tricks the agent into sending data somewhere else, there's no route out.
You get one Box per session. The first tool call starts the Box and later calls reuse it, so dependencies installed in turn one are still there in turn two. When the agent exits, the Box exits with it and leaves nothing on the host.
Which commands go through the sandbox is up to the tool description the model sees, or your own code inside the tool. pi's built-in bash tool stays untouched.
The snippet above puts one command inside a Box. In practice there are two usage patterns.
The production setup at that enterprise agent company is a good example. A web service receives a new user query and spawns a subprocess running a customized pi-agent with its own system prompt, tool set, and hooks. The whole pi-agent runs inside a Box, one Box per conversation.
That gives you a clean boundary. Pages the user pastes in, packages the agent installs, files it edits, and commands it runs all stay inside that conversation's Box with its own kernel. When the conversation ends, the Box is deleted and nothing remains. Model keys don't enter the Box either. The Box only holds a placeholder, the host proxy swaps the real key into request headers, and a prompt-injected agent can't read it.
The implementation uses a template plus clones. The template Box has pi installed, with the network allowlist, the key, and the customization (prompt, hook) configured when the template is built. Every conversation clones it in milliseconds and inherits everything. The service does three things: clone, start pi inside the Box, and forward pi's event stream to the user.
// The template is built once (pi installed inside it, then stopped and kept); every conversation is a clone of it.
const { box: template } = await runtime.getOrCreate({
image: "node:22-bookworm",
autoDelete: 0,
volumes: [{ hostPath: resolve("agent"), guestPath: "/agent", readOnly: true }],
network: { outbound: { mode: "enabled", allowNet: ["registry.npmjs.org", "api.anthropic.com"] } },
secrets: [{
name: "anthropic",
value: process.env.ANTHROPIC_API_KEY, // stays on the host
hosts: [process.env.ANTHROPIC_API_URL],
placeholder: "pi-agent-key",
headers: { "x-api-key": "{value}" }, // the host proxy swaps the real key in
}],
}, "pi-agent-template");
const name = `conversation-${id}`;
const box = await template.cloneBox(null, name);
const execution = await box.exec("pi", [
"--mode", "json", "-p", "--no-session", "--offline",
"--tools", "read,bash", // plus the custom prompt and hook, passed as flags
`Task: ${task}`,
], [
["ANTHROPIC_API_KEY", "pi-agent-key"], // the placeholder, never the real key
["NODE_EXTRA_CA_CERTS", "/etc/ssl/certs/ca-certificates.crt"], // Node has to trust the proxy that swaps it
], false, null, 600, "/workspace");
await (await execution.stdin()).close(); // pi waits on stdin otherwise
// pi prints one JSON event per line; forward them to the user as they arrive.
const stdout = await execution.stdout();
let rest = "";
for (let chunk = await stdout.next(); chunk !== null; chunk = await stdout.next()) {
const lines = (rest + chunk).split("\n");
rest = lines.pop();
for (const line of lines) if (line.trim()) onEvent(JSON.parse(line));
}
await box.stop();
await runtime.remove(name, true); // clones inherit autoDelete: 0, so clean up explicitly
In tests, a clone takes about 20 ms, and a conversation goes from request received to pi's first event in 1 to 2 seconds.
The other pattern keeps the main agent on the host or a regular VM and puts only the high-risk tool inside a Box, such as running user-submitted scripts or processing untrusted files.
Two design points matter here. First, keep what goes back to the model light: each line the script prints streams back to the host, and the model ends up with just the exit code, the last few lines, and a file list. Second, keep heavy data out of the model context: large files the script produces go into a read-write mounted directory, the host picks them up when the job finishes, and the Box is destroyed right after. No extra upload API is needed.
This tool needs no outbound network, so networking is turned off and there's no allowlist to get wrong. It runs one Box per job, taking about 3 seconds from Box start to first line of output. It uses the low-level runtime's streaming interface, since SimpleBox.exec collects all output before returning.
const box = await runtime.create({
image: "python:slim",
autoRemove: true, // one box per job, gone after stop()
volumes: [{ hostPath: outDir, guestPath: "/out", readOnly: false }], // heavy output lands here
network: { outbound: { mode: "disabled" } },
});
const execution = await box.exec("sh", ["-c", params.script]);
const stdout = await execution.stdout();
const lines: string[] = [];
for (let line = await stdout.next(); line !== null; line = await stdout.next()) {
lines.push(line);
onUpdate?.({ content: [{ type: "text", text: line }], details: {} }); // light results stream back to the UI
}
const { exitCode } = await execution.wait();
const files = readdirSync(outDir); // heavy files stay on the host
// hand exitCode, the last 20 lines and `files` to the model
await box.stop();
Agents already do real work on real machines: installing dependencies, running scripts, editing files, and reading whatever users paste in. Giving them that freedom requires a hard boundary around every action.
An embedded sandbox puts that boundary inside the agent's own process. One npm install, and every command or conversation gets its own kernel, with no host access, no secrets inside the Box, and nothing left behind. Whether you put the whole agent in a Box or just one high-risk tool, it takes roughly a dozen to a few dozen lines of core code on hardware you already own.
BoxLite is open source (Apache-2.0) with SDKs for Python, Node, Go, Rust, and C. It's already an option in Pydantic AI, AgentScope Runtime, deer-flow, Databricks Omnigent, and the Kimi Agent SDK. You can run it locally on a laptop, self-host it on servers, or use managed BoxLite Cloud if you'd rather not operate it yourself.
原文来源:blog.boxlite.ai