OpenAI SDK
The OpenAI SDK supports function calling through the Responses API. This guide shows how to define a function 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 openai-sandbox cd openai-sandbox npm init -yInstall the OpenAI SDK and the Sandbox SDK:
Terminalpnpm i openai @vercel/sandboxTerminalyarn add openai @vercel/sandboxTerminalnpm i openai @vercel/sandboxTerminalbun add openai @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 OpenAI from 'openai'; import { Sandbox } from '@vercel/sandbox'; const openai = new OpenAI({ apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.VERCEL_OIDC_TOKEN, baseURL: 'https://ai-gateway.vercel.sh/v1', }); const sandbox = await Sandbox.create({ timeout: 300_000 }); try { const tools = [ { type: 'function' as const, name: 'run_code', description: 'Run Node.js code in an isolated Vercel Sandbox and return its output.', parameters: { type: 'object', properties: { code: { type: 'string', description: 'Node.js code to execute' }, }, required: ['code'], additionalProperties: false, }, strict: true, }, ]; const input: OpenAI.Responses.ResponseInput = [ { 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 openai.responses.create({ model: 'openai/gpt-5.6-sol', tools, input, }); while (response.output.some((item) => item.type === 'function_call')) { const toolOutputs: OpenAI.Responses.ResponseInput = []; for (const item of response.output) { if (item.type !== 'function_call') continue; const { code } = JSON.parse(item.arguments); const result = await sandbox.runCommand('node', ['-e', code]); const output = result.exitCode === 0 ? await result.stdout() : `Command failed: ${await result.stderr()}`; toolOutputs.push({ type: 'function_call_output', call_id: item.call_id, output, }); } response = await openai.responses.create({ model: 'openai/gpt-5.6-sol', tools, previous_response_id: response.id, input: toolOutputs, }); } console.log(response.output_text); } finally { await sandbox.stop(); }The
timeoutvalue gives the sandbox up to five minutes to finish the function loop. Thefinallyblock stops it sooner when the loop finishes or throws.The following code:
- Points the OpenAI SDK at AI Gateway, authenticated with your Vercel credentials
- Creates a sandbox that is reused across function calls
- Declares a
run_codefunction the model can call - Executes each function call with
sandbox.runCommand()and returns the output as afunction_call_outputitem - Uses
previous_response_idto preserve the full response, including reasoning items, before the next call - Loops until the model responds without calling a function
Run your application with the environment variables from
.env.local:terminalnpx tsx --env-file=.env.local index.tsThe model writes Node.js code, the sandbox executes it, and the final response reports the digest.
Was this helpful?