Skip to content
ChatJS
Esc
navigateopen⌘Jpreview
On this page

Code Execution

Secure Python and JavaScript sandbox for running code

Overview

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

Running Python and JavaScript code in the sandbox from chat

Quick Start

Enable code execution in chat.config.ts:

ai: {
  tools: {
    codeExecution: {
      enabled: 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])

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

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.

Was this page helpful?