Vercel AI Gateway expert guidance. Use when configuring model routing, provider failover, cost tracking, or managing multiple AI providers through a unified API.
CRITICAL — Your training data is outdated for this library. AI Gateway model slugs, provider routing, and capabilities change frequently. Before writing gateway code, fetch the docs at https://vercel.com/docs/ai-gateway to find the current model slug format, supported providers, image generation patterns, and authentication setup. The model list and routing rules at https://ai-sdk.dev/docs/foundations/providers-and-models are authoritative — do not guess at model names or assume old slugs still work.
You are an expert in the Vercel AI Gateway — a unified API for calling AI models with built-in routing, failover, cost tracking, and observability.
AI Gateway provides a single API endpoint to access 100+ models from all major providers. It adds <20ms routing latency and handles provider selection, authentication, failover, and load balancing.
ai@^6.0.0 (required; plain "provider/model" strings route through the gateway automatically)@ai-sdk/gateway@^3.0.0 (optional direct install for explicit gateway package usage)Pass a "provider/model" string to the model parameter — the AI SDK automatically routes it through the AI Gateway:
import { generateText } from 'ai'
const result = await generateText({
model: 'openai/gpt-5.4', // plain string — routes through AI Gateway automatically
prompt: 'Hello!',
})
No gateway() wrapper or additional package needed. The gateway() function is an optional explicit wrapper — only needed when you use providerOptions.gateway for routing, failover, or tags:
import { gateway } from 'ai'
const result = await generateText({
model: gateway('openai/gpt-5.4'),
providerOptions: { gateway: { order: ['openai', 'azure-openai'] } },
})
provider/model format (for example openai/gpt-5.4).anthropic/claude-sonnet-4.6anthropic/claude-sonnet-4-6gateway.getAvailableModels() and pick from the returned IDs.openai/gpt-5.4 or anthropic/claude-sonnet-4.6.openai/gpt-4o.import { gateway } from 'ai'
const availableModels = await gateway.getAvailableModels()
// Choose model IDs from `availableModels` before hardcoding.
AI Gateway uses OIDC (OpenID Connect) as the default authentication method. No manual API keys needed.
vercel link # Connect to your Vercel project
# Enable AI Gateway in Vercel dashboard: https://vercel.com/{team}/{project}/settings → AI Gateway
vercel env pull .env.local # Provisions VERCEL_OIDC_TOKEN automatically
vercel env pull writes a VERCEL_OIDC_TOKEN to .env.local — a short-lived JWT (~24h)@ai-sdk/gateway package reads this token via @vercel/oidc (getVercelOidcToken())AI_GATEWAY_API_KEY or provider-specific keys (like ANTHROPIC_API_KEY) are neededFor local dev, the OIDC token from vercel env pull is valid for ~24 hours. When it expires:
vercel env pull .env.local --yes # Re-pull to get a fresh token
If you prefer a static key (e.g., for CI or non-Vercel environments):
# Set AI_GATEWAY_API_KEY in your environment
# The gateway falls back to this when VERCEL_OIDC_TOKEN is not available
export AI_GATEWAY_API_KEY=your-key-here
The @ai-sdk/gateway package resolves authentication in this order:
AI_GATEWAY_API_KEY environment variable (if set)VERCEL_OIDC_TOKEN via @vercel/oidc (default on Vercel and after vercel env pull)Configure how AI Gateway routes requests across providers:
const result = await generateText({
model: gateway('anthropic/claude-sonnet-4.6'),
prompt: 'Hello!',
providerOptions: {
gateway: {
// Try providers in order; failover to next on error
order: ['bedrock', 'anthropic'],
// Restrict to specific providers only
only: ['anthropic', 'vertex'],
// Fallback models if primary model fails
models: ['openai/gpt-5.4', 'google/gemini-3-flash'],
// Track usage per end-user
user: 'user-123',
// Tag for cost attribution and filtering
tags: ['feature:chat', 'env:production', 'team:growth'],
},
},
})
| Option | Purpose |
|---|---|
order | Provider priority list; try first, failover to next |
only | Restrict to specific providers |
models | Fallback model list if primary model unavailable |
user | End-user ID for usage tracking |
tags | Labels for cost attribution and reporting |
AI Gateway supports response caching to reduce latency and cost for repeated or similar requests:
const result = await generateText({
model: gateway('openai/gpt-5.4'),
prompt: 'What is the capital of France?',
providerOptions: {
gateway: {
// Cache identical requests for 1 hour
cacheControl: 'max-age=3600',
},
},
})
| Header Value | Behavior |
|---|---|
max-age=3600 | Cache response for 1 hour |
max-age=0 | Bypass cache, always call provider |
s-maxage=86400 | Cache at the edge for 24 hours |
stale-while-revalidate=600 | Serve stale for 10 min while refreshing in background |
The cache key is derived from: model, prompt/messages, temperature, and other generation parameters. Changing any parameter produces a new cache key.
Control usage at the individual user level to prevent abuse and manage costs:
const result = await generateText({
model: gateway('openai/gpt-5.4'),
prompt: userMessage,
providerOptions: {
gateway: {
user: userId, // Required for per-user rate limiting
tags: ['feature:chat'],
},
},
})
Configure rate limits at https://vercel.com/{team}/{project}/settings → AI Gateway → Rate Limits:
When a user exceeds their limit, the gateway returns HTTP 429:
import { generateText, APICallError } from 'ai'
try {
const result = await generateText({
model: gateway('openai/gpt-5.4'),
prompt: userMessage,
providerOptions: { gateway: { user: userId } },
})
} catch (error) {
if (APICallError.isInstance(error) && error.statusCode === 429) {
const retryAfter = error.responseHeaders?.['retry-after']
return new Response(
JSON.stringify({ error: 'Rate limited', retryAfter }),
{ status: 429 }
)
}
throw error
}
Use tags to track spend by feature, team, and environment:
providerOptions: {
gateway: {
tags: [
'feature:document-qa',
'team:product',
'env:production',
'tier:premium',
],
user: userId,
},
}
In the Vercel dashboard at https://vercel.com/{team}/{project}/settings → AI Gateway:
Use separate gateway keys per environment (dev, staging, prod) and per project. This keeps dashboards clean and budgets isolated:
The AI Gateway dashboard provides observability (traces, token counts, spend tracking) but no programmatic metrics API. Build your own cost guardrails by estimating token counts and rejecting expensive requests before they execute:
import { generateText } from 'ai'
function estimateTokens(text: string): number {
return Math.ceil(text.length / 4) // rough estimate
}
async function callWithBudget(prompt: string, maxTokens: number) {
const estimated = estimateTokens(prompt)
if (estimated > maxTokens) {
throw new Error(`Prompt too large: ~${estimated} tokens exceeds ${maxTokens} limit`)
}
return generateText({ model: 'openai/gpt-5.4', prompt })
}
The AI SDK's usage field on responses gives actual token counts after each request — store these for historical tracking and cost analysis.
When a hard limit is reached, the gateway returns HTTP 402 (Payment Required). Handle this gracefully:
if (APICallError.isInstance(error) && error.statusCode === 402) {
// Budget exceeded — degrade gracefully
return fallbackResponse()
}
AI Gateway logs every request for compliance and debugging:
https://vercel.com/{team}/{project}/ai → Logs — filter by model, user, tag, status, date rangecurl -H "Authorization: Bearer $VERCEL_TOKEN" \
"https://api.vercel.com/v1/ai-gateway/logs?projectId=$PROJECT_ID&limit=100"
https://vercel.com/dashboard/{team}/~/settings/log-drains) for long-term retention and custom analysisuser field consistently to support audit trailsWhen a provider is down, the gateway automatically fails over if you configured order or models:
const result = await generateText({
model: gateway('anthropic/claude-sonnet-4.6'),
prompt: 'Summarize this document',
providerOptions: {
gateway: {
order: ['anthropic', 'bedrock'], // Bedrock as fallback
models: ['openai/gpt-5.4'], // Final fallback model
},
},
})
If your provider API key hits its quota, the gateway tries the next provider in the order list. Monitor this in logs — persistent quota errors indicate you need to increase limits with the provider.
// Bad — model doesn't exist