Skip to main content

Overview

Execute Python code in a secure sandboxed environment directly from chat. The sandbox runs on Vercel infrastructure with a 5-minute timeout and 2 vCPUs.

Quick Start

Enable code execution in chat.config.ts:
integrations: {
  sandbox: true, // Vercel-native, no key needed
}

Pre-installed Packages

The following packages are available out of the box:
  • matplotlib - Plotting and visualization
  • pandas - Data analysis
  • numpy - Numerical computing
  • sympy - Symbolic mathematics
  • yfinance - Yahoo Finance market data

Installing Additional Packages

Add !pip install lines at the top of your code. They are automatically stripped before execution:
!pip install requests beautifulsoup4

import requests
from bs4 import BeautifulSoup

response = requests.get("https://example.com")
print(response.status_code)

Output

There are two ways to return output: 1. Use print()
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.head())
2. Assign to result or results The sandbox automatically prints these variables if they exist:
import numpy as np
result = np.mean([1, 2, 3, 4, 5])
Implicit REPL output (last expression value) is not captured. Always use print() or assign to result.

Charts

The sandbox supports matplotlib charts. No need to call plt.show(). The chart is saved automatically:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(10, 6))
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("sin(x)")
Supported chart types:
  • Line charts
  • Scatter plots
  • Bar charts
Charts are rendered as PNG images. Complex chart types beyond line, scatter, and bar may not render correctly.

Customization

Sandbox Runtime

Set the Python version via environment variable:
VERCEL_SANDBOX_RUNTIME=python3.13

Non-Vercel Deployments

When deploying outside Vercel (Docker, Railway, Fly.io, etc.), you need to authenticate with an access token since VERCEL_OIDC_TOKEN is unavailable.
  1. Copy your Team ID from team settings
  2. Copy your Project ID from project settings
  3. Create a token in your Vercel account settings scoped to your team
  4. Set these environment variables:
VERCEL_TEAM_ID=team_xxx
VERCEL_PROJECT_ID=prj_xxx
VERCEL_TOKEN=your_token_here
The sandbox automatically uses these credentials when running outside Vercel infrastructure.

Tool Definition

The tool is defined in lib/ai/tools/code-execution.ts. You can modify the description to adjust how the AI uses it, or change the base packages installed in the sandbox.