VercelLogotypeVercelLogotype
LoginSign Up

OpenAI Agents SDK with FastAPI

Learn how to build an agent in Python with the OpenAI Agents SDK and Vercel Sandbox.

DeployView Demo

OpenAI Agents SDK FastAPI Starter with Vercel Sandbox

Minimal FastAPI app that runs the OpenAI Agents SDK with Vercel Sandbox on Vercel's Python runtime. Each request spins up an isolated microVM, gives the agent shell access to analyze data, and tears it down when done.

Prerequisites

  • Python 3.12+ (Vercel default is 3.12).
  • A Vercel account and Vercel CLI (npm i -g vercel).
  • An OpenAI or Vercel AI Gateway API key.

Environment variables

VariableRequiredDescription
OPENAI_API_KEYYesOpenAI or Vercel AI Gateway API key.
VERCEL_TOKENYesVercel access token. Create one at https://vercel.com/account/tokens.
VERCEL_TEAM_IDYesYour Vercel team ID (starts with team_). Found under Team Settings > General.
VERCEL_PROJECT_IDYesYour Vercel project ID (starts with prj_). Found under Project Settings > General.
OPENAI_DEFAULT_MODELNoDefault model when the request body omits model. Falls back to gpt-4.1-mini.
OPENAI_BASE_URLNoWhen using AI Gateway, set this to https://ai-gateway.vercel.sh/v1

Setup

Copy the example env file and fill in your values:

cp .env.example .env.local

Then edit .env.local with your keys:

OPENAI_API_KEY=sk-...
VERCEL_TOKEN=your_access_token
VERCEL_TEAM_ID=team_xxx
VERCEL_PROJECT_ID=prj_xxx

Local development

uv sync
uv run uvicorn app:app --reload --host 127.0.0.1 --port 8000

Open http://127.0.0.1:8000 to use the interactive demo. The agent has shell access to a sandbox with sample sales data (sales.csv).

API endpoints:

  • GET /api/health returns {"status": "ok", "openai_configured": true}
  • POST /api/run with {"input": "Which region grew the most?"} runs the sandbox agent

Deploy to Vercel

vercel

Vercel detects app.py and the app ASGI instance. Dependencies come from pyproject.toml [blocked].

After deploying, make sure OPENAI_API_KEY, VERCEL_TOKEN, VERCEL_TEAM_ID, and VERCEL_PROJECT_ID are set under Project Settings > Environment Variables.

Timeouts

Sandbox creation and agent runs can take several seconds. Heavy workloads may need Fluid Compute or Vercel Workflow for durable steps.

How it works

  1. Each POST /api/run creates a fresh Vercel Sandbox microVM with sample data.
  2. A SandboxAgent with Shell capability receives the user's prompt.
  3. The agent writes and runs shell commands inside the sandbox to answer the question.
  4. The sandbox is torn down after the response is returned.

AI Gateway

If you want to use the Vercel AI Gateway with the OpenAI Agents SDK, there are three simple changes that are needed.

  1. Create a new AI Gateway API key, and set it as your OPENAI_API_KEY environment variable under Project Settings > Environment Variables.
  2. Set the OPENAI_BASE_URL environment variable to https://ai-gateway.vercel.sh/v1 under Project Settings > Environment Variables.
  3. Ensure that the provider prefix on your model ID is not being stripped by setting the model_provider=MultiProvider(openai_prefix_mode="model_id") in your RunConfig as shown in the example.

License

MIT (match your org's policy when publishing).

GitHub Repovercel-labs/openai-agents-python-template

Related Templates

Next.js Boilerplate

Get started with Next.js and React in seconds.
Next.js Boilerplate thumbnail

Image Gallery Starter

An image gallery built on Next.js and Vercel Blob.
Image Gallery Starter thumbnail

Chatbot

A full-featured, hackable Next.js AI chatbot built by Vercel
Chatbot thumbnail

Get Started

  • Templates
  • Supported frameworks
  • Marketplace
  • Domains

Build

  • Next.js on Vercel
  • Turborepo
  • v0

Scale

  • Content delivery network
  • Fluid compute
  • CI/CD
  • Observability
  • AI GatewayNew
  • Vercel AgentNew

Secure

  • Platform security
  • Web Application Firewall
  • Bot management
  • BotID
  • SandboxNew

Resources

  • Pricing
  • Customers
  • Enterprise
  • Articles
  • Startups
  • Solution partners

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Community

Frameworks

  • Next.js
  • Nuxt
  • Svelte
  • Nitro
  • Turbo

SDKs

  • AI SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDK
  • Streamdown AINew

Use Cases

  • Composable commerce
  • Multi-tenant platforms
  • Web apps
  • Marketing sites
  • Platform engineers
  • Design engineers

Company

  • About
  • Careers
  • Help
  • Press
  • Legal
  • Privacy Policy

Community

  • Open source program
  • Events
  • Shipped on Vercel
  • GitHub
  • LinkedIn
  • X
  • YouTube

Loading status…

Select a display theme:
    • AI Cloud
      • AI Gateway

        One endpoint, all your models

      • Sandbox

        Isolated, safe code execution

      • Vercel Agent

        An agent that knows your stack

      • AI SDK

        The AI Toolkit for TypeScript

      • v0

        Build applications with AI

    • Core Platform
      • CI/CD

        Helping teams ship 6× faster

      • Content Delivery

        Fast, scalable, and reliable

      • Fluid Compute

        Servers, in serverless form

      • Workflow

        Long-running workflows at scale

      • Observability

        Trace every step

    • Security
      • Bot Management

        Scalable bot protection

      • BotID

        Invisible CAPTCHA

      • Platform Security

        DDoS Protection, Firewall

      • Web Application Firewall

        Granular, custom protection

    • Company
      • Customers

        Trusted by the best teams

      • Blog

        The latest posts and changes

      • Changelog

        See what shipped

      • Press

        Read the latest news

      • Events

        Join us at an event

    • Learn
      • Docs

        Vercel documentation

      • Academy

        Linear courses to level up

      • Knowledge Base

        Find help quickly

      • Community

        Join the conversation

    • Open Source
      • Next.js

        The native Next.js platform

      • Nuxt

        The progressive web framework

      • Svelte

        The web’s efficient UI framework

      • Turborepo

        Speed with Enterprise scale

    • Use Cases
      • AI Apps

        Deploy at the speed of AI

      • Composable Commerce

        Power storefronts that convert

      • Marketing Sites

        Launch campaigns fast

      • Multi-tenant Platforms

        Scale apps with one codebase

      • Web Apps

        Ship features, not infrastructure

    • Tools
      • Marketplace

        Extend and automate workflows

      • Templates

        Jumpstart app development

      • Partner Finder

        Get help from solution partners

    • Users
      • Platform Engineers

        Automate away repetition

      • Design Engineers

        Deploy for every idea

  • Enterprise
  • Pricing
Log InContact
Sign Up
Sign Up
ContactDashboard
DeployView Demo
AI Gateway

One endpoint, all your models

Sandbox

Isolated, safe code execution

Vercel Agent

An agent that knows your stack

AI SDK

The AI Toolkit for TypeScript

v0

Build applications with AI

CI/CD

Helping teams ship 6× faster

Content Delivery

Fast, scalable, and reliable

Fluid Compute

Servers, in serverless form

Workflow

Long-running workflows at scale

Observability

Trace every step

Bot Management

Scalable bot protection

BotID

Invisible CAPTCHA

Platform Security

DDoS Protection, Firewall

Web Application Firewall

Granular, custom protection

Customers

Trusted by the best teams

Blog

The latest posts and changes

Changelog

See what shipped

Press

Read the latest news

Events

Join us at an event

Docs

Vercel documentation

Academy

Linear courses to level up

Knowledge Base

Find help quickly

Community

Join the conversation

Next.js

The native Next.js platform

Nuxt

The progressive web framework

Svelte

The web’s efficient UI framework

Turborepo

Speed with Enterprise scale

AI Apps

Deploy at the speed of AI

Composable Commerce

Power storefronts that convert

Marketing Sites

Launch campaigns fast

Multi-tenant Platforms

Scale apps with one codebase

Web Apps

Ship features, not infrastructure

Marketplace

Extend and automate workflows

Templates

Jumpstart app development

Partner Finder

Get help from solution partners

Platform Engineers

Automate away repetition

Design Engineers

Deploy for every idea