Anthropic SDK
The Anthropic SDK supports tool use through the Messages API. This guide shows how to define a tool that executes code in Vercel Sandbox, so model-generated code runs in an isolated microVM instead of on your host. Model requests route through AI Gateway, and one set of Vercel credentials covers both the model and the sandbox.
Create a new directory for your project and initialize it:
terminalmkdir anthropic-sandbox cd anthropic-sandbox npm init -yInstall the Anthropic SDK and the Sandbox SDK:
Terminalpnpm i @anthropic-ai/sdk @vercel/sandboxTerminalyarn add @anthropic-ai/sdk @vercel/sandboxTerminalnpm i @anthropic-ai/sdk @vercel/sandboxTerminalbun add @anthropic-ai/sdk @vercel/sandboxLink the project to Vercel and pull your development environment. This creates a
.env.localfile with aVERCEL_OIDC_TOKEN, which authenticates both the Sandbox SDK and AI Gateway:terminalvercel link vercel env pullCreate a new file called
index.tswith the following code:index.tsimport Anthropic from '@anthropic-ai/sdk'; import { Sandbox } from '@vercel/sandbox'; const gatewayApiKey = process.env.AI_GATEWAY_API_KEY; const anthropic = new Anthropic({ ...(gatewayApiKey ? { apiKey: gatewayApiKey } : { authToken: process.env.VERCEL_OIDC_TOKEN }), baseURL: 'https://ai-gateway.vercel.sh', }); const sandbox = await Sandbox.create({ timeout: 300_000 }); try { const tools: Anthropic.Tool[] = [ { name: 'run_code', description: 'Run Node.js code in an isolated Vercel Sandbox and return its output.', input_schema: { type: 'object', properties: { code: { type: 'string', description: 'Node.js code to execute' }, }, required: ['code'], }, }, ]; const messages: Anthropic.MessageParam[] = [ { role: 'user', content: "Compute the SHA-256 hex digest of the exact string 'vercel sandbox' with Node.js, then report the digest.", }, ]; let response = await anthropic.messages.create({ model: 'anthropic/claude-opus-5', max_tokens: 16000, tools, messages, }); while (response.stop_reason === 'tool_use') { messages.push({ role: 'assistant', content: response.content }); const toolResults: Anthropic.ToolResultBlockParam[] = []; for (const block of response.content) { if (block.type !== 'tool_use') continue; const { code } = block.input as { code: string }; const result = await sandbox.runCommand('node', ['-e', code]); const output = result.exitCode === 0 ? await result.stdout() : `Command failed: ${await result.stderr()}`; toolResults.push({ type: 'tool_result', tool_use_id: block.id, content: output, }); } messages.push({ role: 'user', content: toolResults }); response = await anthropic.messages.create({ model: 'anthropic/claude-opus-5', max_tokens: 16000, tools, messages, }); } for (const block of response.content) { if (block.type === 'text') console.log(block.text); } } finally { await sandbox.stop(); }The
timeoutvalue gives the sandbox up to five minutes to finish the tool loop. Thefinallyblock stops it sooner when the loop finishes or throws.The following code:
- Points the Anthropic SDK at AI Gateway, authenticated with your Vercel credentials
- Creates a sandbox that is reused across tool calls
- Declares a
run_codetool the model can call - Executes each tool call with
sandbox.runCommand()and returns the output as atool_resultblock - Loops while
stop_reasonistool_use, then prints the final answer
Run your application with the environment variables from
.env.local:terminalnpx tsx --env-file=.env.local index.tsClaude writes Node.js code, the sandbox executes it, and the final response reports the digest.
Was this helpful?