A sandbox tool for generateText and streamText
npm install ai @ai-sdk/openai-compatible zod @cpuos/sdkimport { createOpenAICompatible } from "@ai-sdk/openai-compatible"import { Sandbox } from "@cpuos/sdk"import { generateText, stepCountIs, tool } from "ai"import { z } from "zod"const gpuos = createOpenAICompatible({ name: "gpuos", baseURL: "https://gpuos.si/v1", apiKey: process.env.GPUOS_API_KEY,})const sbx = await Sandbox.create({ template: "python", timeout: "30m" })const runPython = tool({ description: "Run a Python 3 script in an isolated sandbox. Files live in /work.", inputSchema: z.object({ code: z.string() }), execute: async ({ code }) => { await sbx.files.write("/work/main.py", code) const run = await sbx.exec("cd /work && python main.py", { timeout: "2m" }) return { exitCode: run.exitCode, stdout: run.stdout.slice(-4000), stderr: run.stderr.slice(-2000), } },})const { text } = await generateText({ model: gpuos("qwen3-32b"), tools: { runPython }, stopWhen: stepCountIs(8), prompt: "Simulate 10,000 rolls of two dice and report the most common sum.",})await sbx.pause()The examples point the model at gpuOS, which serves open models on your own GPUs at https://gpuos.si/v1 with an OpenAI-compatible API. Any other OpenAI-compatible provider works the same way: change the base URL, the key and the model name.
cpuos is in early access: @cpuos/sdk (npm) and cpuos (PyPI) ship to early-access teams first, and read the API key from CPUOS_API_KEY. The calls on this page show the current API shape.
In a Next.js route handler
Create or resume the sandbox inside the route handler, keyed by the chat id, and use streamText instead of generateText. Return result.toUIMessageStreamResponse() to stream tool calls and text into useChat. Keep both API keys in server-only environment variables.
For previews, have a tool start a dev server in the node template with sbx.exec("pnpm dev --port 3000", { background: true }) and return sbx.url(3000). The user sees the app the agent built, running in its sandbox.
Official documentation: ai-sdk.dev