Skip to content
Docs

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.

You need a Vercel account, Vercel CLI, and Node.js 22 or later.

  1. Create a new directory for your project and initialize it:

    terminal
    mkdir openai-sandbox
    cd openai-sandbox
    npm init -y
  2. Install the OpenAI SDK and the Sandbox SDK:

    Terminal
    pnpm i openai @vercel/sandbox
    Terminal
    yarn add openai @vercel/sandbox
    Terminal
    npm i openai @vercel/sandbox
    Terminal
    bun add openai @vercel/sandbox
  3. Link the project to Vercel and pull your development environment. This creates a .env.local file with a VERCEL_OIDC_TOKEN, which authenticates both the Sandbox SDK and AI Gateway:

    terminal
    vercel link
    vercel env pull

    You can also use an AI Gateway API key by setting AI_GATEWAY_API_KEY. Outside Vercel and local development, authenticate the Sandbox SDK with an access token by setting VERCEL_TOKEN, VERCEL_TEAM_ID, and VERCEL_PROJECT_ID.

  4. Create a new file called index.ts with the following code:

    index.ts
    import 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 timeout value gives the sandbox up to five minutes to finish the function loop. The finally block 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_code function the model can call
    • Executes each function call with sandbox.runCommand() and returns the output as a function_call_output item
    • Uses previous_response_id to preserve the full response, including reasoning items, before the next call
    • Loops until the model responds without calling a function
  5. Run your application with the environment variables from .env.local:

    terminal
    npx tsx --env-file=.env.local index.ts

    The model writes Node.js code, the sandbox executes it, and the final response reports the digest.

Last updated August 7, 2026

Was this helpful?

supported.