@groundbox/ai-sdk
v0.1.1
Published
Groundbox sandboxes for the Vercel AI SDK: a Firecracker microVM Linux sandbox for your agent's tools and for harness agents like Claude Code and Codex.
Maintainers
Readme
@groundbox/ai-sdk
Groundbox sandboxes for the
Vercel AI SDK. Each sandbox is a Firecracker microVM with its own kernel
and cores: Ubuntu 24.04 with git, Node, Python and Docker, and user runner with passwordless
sudo. It implements the AI SDK's sandbox interface, the same one @ai-sdk/sandbox-vercel and
@e2b/ai-sdk-sandbox implement, so you can:
- hand it to your own tools with
experimental_sandboxingenerateText,streamTextor aToolLoopAgent, and - run harness agents such as Claude Code and Codex (
HarnessAgent) inside it.
Install
npm install @groundbox/ai-sdk aiNode 22.13 or newer. Set GROUNDBOX_API_KEY (create a key at
app.groundbox.dev/keys),
or pass apiKey.
Give your tools a sandbox
import { anthropic } from '@ai-sdk/anthropic';
import { createGroundboxNetworkSandboxSession } from '@groundbox/ai-sdk';
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
const sandbox = await createGroundboxNetworkSandboxSession({ spec: { cpus: 2 } });
const bash = tool({
description: 'Run a shell command in a Linux sandbox',
inputSchema: z.object({ command: z.string() }),
execute: ({ command }, { experimental_sandbox }) => experimental_sandbox!.run({ command }),
});
try {
const result = await generateText({
model: anthropic('claude-sonnet-5-5'),
tools: { bash },
stopWhen: isStepCount(10),
prompt: 'Write a Python script that prints the first 20 primes, then run it.',
// The restricted view: files and commands, no way to stop the VM or change its network.
experimental_sandbox: sandbox.restricted(),
});
console.log(result.text);
} finally {
await sandbox.destroy(); // deletes the VM; billing stops
}The session on its own:
const session = sandbox.restricted();
await session.writeTextFile({ path: 'hello.py', content: 'print("hi")' });
const { stdout } = await session.run({ command: 'python3 hello.py' }); // "hi\n"Run Claude Code (or Codex) in it
import { HarnessAgent } from '@ai-sdk/harness/agent';
import { createClaudeCode } from '@ai-sdk/harness-claude-code';
import { createGroundboxNetworkSandboxSession } from '@groundbox/ai-sdk';
const agent = new HarnessAgent({ harness: createClaudeCode(), id: 'coder' });
const sandboxSession = await createGroundboxNetworkSandboxSession({
spec: { cpus: 2, memory_gb: 4 },
ports: [4000],
template: await agent.getSandboxTemplate(),
});
const session = await agent.createSession({ sandboxSession });
try {
const result = await agent.generate({ session, prompt: 'Read README.md and summarise the goals.' });
console.log(result.text);
} finally {
await session.destroy();
await sandboxSession.destroy();
}The first run with a template installs the harness in a fresh sandbox and keeps it as a
Groundbox snapshot (labelled ai-sdk-harness-v1-...). Later runs with the same template and
machine start from that snapshot instead of installing again.
What maps to what
| AI SDK | Groundbox |
| --- | --- |
| createGroundboxNetworkSandboxSession() | Creates a sandbox from spec (anything create_sandbox takes: template, image, cpus, memory_gb, timeout_minutes, allow_hosts, env, metadata, ...) and waits until it runs. |
| sandboxId option | Kept in the sandbox's metadata (ai-sdk-sandbox-id), so you can resume by your own id. A second live sandbox with the same id is refused. |
| template option | Built once into a kept snapshot; later sessions start from it. |
| resumeGroundboxNetworkSandboxSession({ sandboxId }) | Finds the sandbox by your id or its sbx_... id. Resumes a paused one and boots a shut-down one; never creates one. |
| run / spawn | The exec stream. Commands run under bash -c (or sh -c) in the working directory, for up to a day; spawn with stdin: true keeps stdin open. Abort signals kill the command. |
| files | Groundbox's files API: read, write, stat (follows symlinks), list, delete. |
| ports, getPortEndpoint, setPorts | Preview URLs, https://...groundbox.page. Private by default: getPortEndpoint returns the token header to send. publicPorts: true makes them public. Servers must listen on 0.0.0.0. |
| setNetworkPolicy | The sandbox's egress rules: allow-all, deny-all, or hosts (with *. wildcards) and IPv4 networks. IPv6 networks are refused. |
| stop() | Shuts the VM down and keeps its disk (only storage is billed). resume... boots it again. |
| destroy() | Deletes the VM. |
| restricted() | The plain sandbox session for tools: files, run and spawn only. |
Already have a sandbox? createGroundboxSandboxSession({ sandboxId }) wraps it as a plain
session for experimental_sandbox, and createGroundboxNetworkSandboxSessionFromSandbox({ sandboxId })
as a network session for a harness. Pass your own @groundbox/sdk client as client for anything
this package does not wrap (fork, save points, secrets bound to hosts).
Tests
npm test # unit tests, no network
GROUNDBOX_API_KEY=gbx_... npm run test:live # real sandboxes, deleted afterwardsThe Claude Code live test sends a prompt only when ANTHROPIC_API_KEY is set too.
License
MIT
