AI Gateway
AI Gateway is available in Beta on all plans and your use is subject to Vercel's Public Beta Agreement and AI Product Terms.
The AI Gateway is a proxy service from Vercel that routes model requests to various AI providers. It offers a unified API to multiple providers and gives you the ability to set budgets, monitor usage, load-balance requests, and manage fallbacks.
It is designed to work seamlessly with AI SDK 5 and can be used as a provider in your applications.
Some of the key features of the AI Gateway include:
- Unified API: helps you switch between providers and models with minimal code changes
- High Reliability: automatically retries requests to other providers if one fails
- Spend Monitoring: monitor your spending across different providers
- Load Balancing: distribute requests across multiple providers to optimize performance
- Bring Your Own Key (BYOK): use your own provider API keys
- Provider Routing: control how requests are distributed across different AI providers and customize fallback behavior using provider options
- OpenAI-Compatible API: use existing OpenAI client libraries and tools with the AI Gateway
Start using the AI Gateway by:
- Creating and using an API key
- Using an OIDC token associated with your Vercel project
- Configuring your own provider keys (BYOK)
- Learning about models, creators, and providers
- Configuring provider routing options
- Using the OpenAI-Compatible API
Let's create a Next.js app and install the AI SDK and AI Gateway packages:
terminalnpx create-next-app@latest my-gateway-app cd my-gateway-app pnpm install ai
Next, let's create an API key in the AI Gateway tab of the Vercel dashboard:
- From the Vercel dashboard, click the AI Gateway tab
- Click API keys on the left side bar
- Click Create key and proceed with Create key from the Dialog
Once you have the API key, save it to
.env.local
at the root of your project so you can use it to authenticate your requests to the AI Gateway..env.localAI_GATEWAY_API_KEY=your_api_key_here
Then in the Next.js app, add a
GET
route handler that creates a gateway provider instance. It uses the API key and calls thegenerateText
function to generate text based on a prompt passed as a query parameter.When you specify a model id as a plain string, the AI SDK will use the Vercel AI Gateway provider to route the request.
The AI Gateway provider looks for the API key in the
AI_GATEWAY_API_KEY
environment variable by default. You can also specify the API key directly in thecreateGateway
function, described more in the As part of an AI SDK function call section.app/api/chat/route.tsimport { generateText } from 'ai'; export async function GET() { const result = await generateText({ model: 'xai/grok-3', prompt: 'Why is the sky blue?', }); return Response.json(result); }
Now run your Next.js app using
pnpm dev
and open your browser tohttp://localhost:3000/api/chat
. You should see a response with the generated text.
The Vercel OIDC token is a way to authenticate your requests to the AI Gateway without needing to manage an API key. Vercel automatically generates the OIDC token that it associates with your Vercel project.
Vercel OIDC tokens are only valid for 12 hours, so you will need to refresh
them periodically during local development. You can do this by running vercel env pull
again.
Before you can use the OIDC token during local development, ensure that you link your application to a Vercel project and that you pull the environment variables from Vercel. You can do this by running the following command in your terminal:
terminalvercel link vercel env pull
Then in your
GET
route handler you can directly use the gateway provider without needing to obtain an API key or set it in an environment variable:app/api/chat/route.tsimport { generateText } from 'ai'; export async function GET() { const result = await generateText({ model: 'xai/grok-3', prompt: 'Why is the sky blue?', }); return Response.json(result); }
Now run your Next.js app using
pnpm dev
and open your browser tohttp://localhost:3000/api/chat
. You should see a response with the generated text.
The AI Gateway supports using your own API key with an external AI provider to authenticate requests the Gateway makes on your behalf. This can be useful for utilizing credits they've given you or executing AI queries that have access to private cloud data. If a query made with your API key fails, AI Gateway will attempt to fulfill your query again with AI Gateway system credentials, aiming to provide improved availability for your service.
Integrating an API key like this with AI Gateway is sometimes referred to as Bring-Your-Own-Key, or BYOK. In the Vercel dashboard this feature is found in the AI Gateway tab under the Integrations section in the sidebar.
Provider keys are scoped to be available throughout your Vercel team, so you can use the same key across multiple projects.
First, retrieve an API key from your AI provider. This key will be used first to authenticate requests made to that provider through the AI Gateway. If a query made with your API key fails, AI Gateway will re-attempt with system credentials, aiming to provide improved availability.
- Go to the AI Gateway tab in your Vercel dashboard.
- Click on the Integrations section on the left sidebar.
- Find your provider from the list and click Add.
- In the dialog that appears, enter the key you retrieved from the provider.
- Ensure that the Enabled toggle is turned on so that the key is active.
- Click Add Key to complete the flow.
Once the key is added, it will automatically be included in your requests to the AI Gateway. You can now use this key to authenticate your requests.
The AI Gateway provides OpenAI-compatible API endpoints that allow you to use existing OpenAI client libraries and tools with the AI Gateway.
The OpenAI-compatible API includes:
- Model Management: List and retrieve the available models
- Chat Completions: Create chat completions that support streaming, images, and file attachments
- Tool Calls: Call functions with automatic or explicit tool selection
- Existing Tool Integration: Use your existing OpenAI client libraries and tools without needing modifications
Learn more about the OpenAI-Compatible API.
Was this helpful?