Menu

Python

Last updated January 15, 2026

The AI Gateway supports Python through two official SDKs.

OpenAI-compatible API - Use the OpenAI Python SDK with AI Gateway, including support for:

Anthropic-compatible API - Use the Anthropic Python SDK with AI Gateway, including support for:

Install your preferred SDK:

# OpenAI SDK (for OpenAI-compatible API)
pip install openai
 
# Anthropic SDK (for Anthropic-compatible API)
pip install anthropic
openai-quickstart.py
import os
from openai import OpenAI
 
client = OpenAI(
    api_key=os.getenv('AI_GATEWAY_API_KEY'),
    base_url='https://ai-gateway.vercel.sh/v1'
)
 
response = client.chat.completions.create(
    model='anthropic/claude-sonnet-4.5',
    messages=[
        {'role': 'user', 'content': 'Hello, world!'}
    ]
)
 
print(response.choices[0].message.content)
anthropic-quickstart.py
import os
import anthropic
 
client = anthropic.Anthropic(
    api_key=os.getenv('AI_GATEWAY_API_KEY'),
    base_url='https://ai-gateway.vercel.sh'
)
 
message = client.messages.create(
    model='anthropic/claude-sonnet-4.5',
    max_tokens=1024,
    messages=[
        {'role': 'user', 'content': 'Hello, world!'}
    ]
)
 
print(message.content[0].text)

Both SDKs support the same authentication methods:

  • API key: Use your AI Gateway API key
  • OIDC token: Use your Vercel OIDC token (for deployments on Vercel)
authentication.py
import os
 
# Option 1: API key (recommended for local development)
api_key = os.getenv('AI_GATEWAY_API_KEY')
 
# Option 2: OIDC token (for Vercel deployments)
# api_key = os.getenv('VERCEL_OIDC_TOKEN')
 
# You can also use both with a fallback
api_key = os.getenv('AI_GATEWAY_API_KEY') or os.getenv('VERCEL_OIDC_TOKEN')

Several Python frameworks have dedicated AI Gateway integrations:

  • Pydantic AI - Agent framework with native VercelProvider for structured outputs and type validation
  • LlamaIndex - Build knowledge assistants with the llama-index-llms-vercel-ai-gateway package
  • LiteLLM - Unified LLM interface with native vercel_ai_gateway/ model prefix

See Framework Integrations for the full list.


Was this helpful?

supported.