Skip to content

Integrations

Vercel AI SDK

A run_command tool for generateText and streamText. The sandbox starts on the first call and is killed when the run ends.

Install

Terminal
npm i sandboxplease ai @ai-sdk/openai zod

Code

Copy it as vercel-ai.ts. The model is the only part you would change.

vercel-ai.ts
// Vercel AI SDK: a run_command tool backed by one sandbox per agent run.
// Run: SANDBOXPLEASE_API_KEY=... OPENAI_API_KEY=... npx tsx vercel-ai.ts
import { openai } from "@ai-sdk/openai";
import { generateText, type LanguageModel, stepCountIs, tool } from "ai";
import { type Sandbox, SandboxPlease } from "sandboxplease";
import { z } from "zod";

export function sandboxTools(sp = new SandboxPlease()) {
  let sandbox: Promise<Sandbox> | undefined;
  const run_command = tool({
    description: "Run a shell command in a Linux sandbox with Python 3 and Node. Returns exit code, stdout and stderr.",
    inputSchema: z.object({ command: z.string().describe("Shell command, e.g. python3 -c 'print(1+1)'") }),
    execute: async ({ command }) => {
      sandbox ??= sp.sandboxes.create({ timeout: 600 });
      const { exitCode, stdout, stderr } = await (await sandbox).exec(command);
      return { exitCode, stdout, stderr };
    },
  });
  return { tools: { run_command }, close: async () => (await sandbox)?.kill() };
}

export async function solve(model: LanguageModel, prompt: string) {
  const { tools, close } = sandboxTools();
  try {
    return await generateText({ model, tools, prompt, stopWhen: stepCountIs(5) });
  } finally {
    await close();
  }
}

if (import.meta.url === `file://${process.argv[1]}`) {
  const { text } = await solve(openai("gpt-5-mini"), "Compute 6*7 with Python in the sandbox.");
  console.log(text);
}

Run it

Terminal
export SANDBOXPLEASE_API_KEY=sp-v1-…
export OPENAI_API_KEY=…
npx tsx vercel-ai.ts

The agent runs Python in the sandbox and answers 42. Each sandbox shows up in Logs with its provider and cost.