npx skills add ...
npx skills add openai/plugins --skill ai-gateway
Vercel AI Gateway expert guidance. Use when configuring model routing, provider failover, cost tracking, or managing multiple AI providers through a unified API.
npx skills add openai/plugins --skill ai-gateway
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:
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:
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.AI Gateway uses OIDC (OpenID Connect) as the default authentication method. No manual API keys needed.
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:
If you prefer a static key (e.g., for CI or non-Vercel environments):
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:
| 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:
| 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:
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:
Use tags to track spend by feature, team, and environment:
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:
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:
AI Gateway logs every request for compliance and debugging:
https://vercel.com/{team}/{project}/ai → Logs — filter by model, user, tag, status, date rangehttps://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:
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.
Gateway has a default timeout per provider. For long-running generations, use streaming:
Use this to decide whether to route through AI Gateway or call a provider SDK directly:
GPT-5.4 (added March 5, 2026) — agentic and reasoning leaps from GPT-5.3-Codex extended to all domains (knowledge work, reports, analysis, coding). Faster and more token-efficient than GPT-5.2.
| Model | Slug | Input | Output |
|---|---|---|---|
| GPT-5.4 | openai/gpt-5.4 | $2.50/M tokens | $15.00/M tokens |
| GPT-5.4 Pro | openai/gpt-5.4-pro | $30.00/M tokens | $180.00/M tokens |
GPT-5.4 Pro targets maximum performance on complex tasks. Use standard GPT-5.4 for most workloads.
Text and image generation both route through the gateway. For embeddings, use a direct provider SDK.
Default image model: google/gemini-3.1-flash-image-preview — fast multimodal image generation via gateway.
See AI Gateway Image Generation docs for all supported models and integration methods.
| Scenario | Use Gateway? |
|---|---|
| Production app with AI features | Yes — failover, cost tracking |
| Prototyping with single provider | Optional — direct provider works fine |
| Multi-provider setup | Yes — unified routing |
| Need provider-specific features | Use direct provider SDK + Gateway as fallback |
| Cost tracking and budgeting | Yes — user tracking and tags |
| Multi-tenant SaaS | Yes — per-user rate limiting and audit |
| Compliance requirements | Yes — audit logging and log drains |