Give a Vercel AI SDK agent a sandbox
vercelAiTools(box) returns a tool set for generateText and streamText.
The AI SDK runs the multi-step loop; each tool call runs in the sandbox.
Install
npm install ai @ai-sdk/anthropic @tangle-network/sandboxAny @ai-sdk/* provider works. Set that provider’s key, such as
ANTHROPIC_API_KEY for @ai-sdk/anthropic, and TANGLE_API_KEY for the
sandbox.
Generate text with sandbox tools
import { anthropic } from "@ai-sdk/anthropic";
import { generateText, stepCountIs } from "ai";
import { Sandbox } from "@tangle-network/sandbox";
import { vercelAiTools } from "@tangle-network/sandbox/agent";
const client = new Sandbox({
apiKey: process.env.TANGLE_API_KEY!,
baseUrl: "https://sandbox.tangle.tools",
});
const box = await client.create();
try {
const tools = await vercelAiTools(box, { sessionId: "chat-1" });
const { text } = await generateText({
model: anthropic("claude-opus-5-5"),
tools,
stopWhen: stepCountIs(6),
prompt: "Plot sin(x) for x in [0, 2π] and describe the plot.",
});
console.log(text);
} finally {
await box.delete();
}Use any model id your provider lists. stopWhen: stepCountIs(n) lets the loop
take up to n steps; without it, the AI SDK stops after the first step and
text is empty when that step was a tool call.
vercelAiTools is async because it loads the ai package at runtime; the
sandbox SDK does not bundle it.
Stream
import { streamText, stepCountIs } from "ai";
const { textStream } = streamText({
model: anthropic("claude-opus-5-5"),
tools,
stopWhen: stepCountIs(12),
prompt: "Train a linear model on the iris dataset, then plot the residuals.",
});
for await (const delta of textStream) process.stdout.write(delta);Tool calls run as the model emits them, and their results go back into the conversation.
Choose the tools
const tools = await vercelAiTools(box, {
sessionId: "chat-1",
allow: ["sandbox_run_code", "sandbox_read"],
});The default set is sandbox_run_code, sandbox_exec, sandbox_read,
sandbox_write, sandbox_list and sandbox_search. Each tool’s input schema
is JSON Schema wrapped with the AI SDK’s jsonSchema().
vercelAiFleetTools(client) gives the model tools to spawn and run fleets; see
Integrations. For retries and duplicate tool calls,
see Retries and duplicate work.