Python
The AI Gateway supports Python through two official SDKs.
OpenAI-compatible API - Use the OpenAI Python SDK with AI Gateway, including support for:
- Chat completions and streaming
- Tool calls and structured outputs
- Image attachments and PDFs
- Embeddings and prompt caching
- Reasoning and provider routing
Anthropic-compatible API - Use the Anthropic Python SDK with AI Gateway, including support for:
- Messages and streaming
- Tool calls and extended thinking
- Web search and file attachments
- Claude Code integration
Install your preferred SDK:
# OpenAI SDK (for OpenAI-compatible API)
pip install openai
# Anthropic SDK (for Anthropic-compatible API)
pip install anthropicimport 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)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)
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
VercelProviderfor structured outputs and type validation - LlamaIndex - Build knowledge assistants with the
llama-index-llms-vercel-ai-gatewaypackage - LiteLLM - Unified LLM interface with native
vercel_ai_gateway/model prefix
See Framework Integrations for the full list.
Was this helpful?