ai-sdk-tool-code-execution
Execute Python code in a sandboxed environment using Vercel Sandbox. Run calculations, data processing, and other computational tasks safely in an isolated environment with Python 3.13.
Module not found "https://esm.sh/node:sqlite?target=denonext".
at [0m[36mhttps://esm.sh/undici@7.16.0?target=denonext[0m:[0m[33m2[0m:[0m[33m8[0mLast checked: 12/29/2025, 3:52:23 AM
Test ai-sdk-tool-code-execution (executeCode) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install ai-sdk-tool-code-executionpnpm add ai-sdk-tool-code-executionimport { executeCode } from 'ai-sdk-tool-code-execution';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { executeCode } from 'ai-sdk-tool-code-execution';
const result = await generateText({
model: openai('gpt-4o'),
tools: { executeCode },
prompt: 'Your prompt here...',
});
console.log(result.text);How AI agents can use this tool
Use when you need to perform calculations, data processing, or execute Python code safely
Python 3.13 only. No network access. 30 second execution timeout.
Available configuration options
codestringPython code to execute in the sandbox
Schema extracted: 12/17/2025, 10:50:54 AM
Try to auto-extract schema from the package
A TypeScript package that provides a code execution tool for the AI SDK. Execute Python code in a sandboxed environment using Vercel Sandbox.
pnpm add ai-sdk-tool-code-execution
From your project directory, link to a new or existing Vercel project:
vercel link
Download your Vercel OIDC token:
vercel env pull
This creates a .env.local file with your VERCEL_OIDC_TOKEN that the SDK uses to authenticate with Vercel Sandbox.
Note: Development tokens expire after 12 hours. Run vercel env pull again when your token expires.
If using Vercel AI Gateway, add your API key to .env.local:
AI_GATEWAY_API_KEY=your_api_key_here
import { executeCode } from "ai-sdk-tool-code-execution"; import { generateText, gateway } from "ai"; const result = await generateText({ model: gateway("openai/gpt-4o-mini"), prompt: "What is 5 + 5 minus 84 cubed?", tools: { executeCode: executeCode(), }, }); console.log(result.text);
The executeCode tool allows your AI agent to run Python 3.13 code in a Vercel Sandbox environment. The agent can perform calculations, data processing, and other computational tasks safely in an isolated environment.
Configure the tool with optional parameters:
type CodeExecutionToolOptions = { debug?: boolean; };
Example with debug enabled:
const result = await generateText({ model: gateway("openai/gpt-4o-mini"), prompt: "Calculate the factorial of 10", tools: { executeCode: executeCode({ debug: true }), }, });
When debug is enabled, you'll see detailed logs of code execution in your terminal.
This package uses Vercel Sandbox to execute Python code in an ephemeral, isolated environment. Each code execution:
python3 -cpython3.13 imageprint() to see output. Bare expressions produce no outputIf you cannot use VERCEL_OIDC_TOKEN, you can authenticate with access tokens. Set these environment variables:
VERCEL_TEAM_ID=your_team_id_here VERCEL_PROJECT_ID=your_project_id_here VERCEL_TOKEN=your_access_token_here
Find your team ID, project ID, and create an access token in your Vercel dashboard.
Test the tool with the included test script:
pnpm test
Build the package:
pnpm build
Update the version in package.json, then publish:
pnpm publish
The package automatically builds before publishing.
.
├── src/
│ ├── tools/
│ │ └── execute-code.ts # Code execution tool implementation
│ ├── index.ts # Tool exports
│ └── test.ts # Test script
├── dist/ # Build output (generated)
├── package.json
├── tsconfig.json
└── README.md
Track your sandbox usage in the Vercel dashboard:
View compute usage across all projects in the Usage tab of your dashboard.
ISC
Downloads/month
694
GitHub Stars
0
Quality Score