AI Gateway Provider Options
AI Gateway can route your AI model requests across multiple AI providers. Each provider offers different models, pricing, and performance characteristics. By default, AI Gateway will automatically choose providers for you to ensure fast and dependable responses.
With the Gateway Provider Options, you can control the routing order and fallback behavior of the models.
If you want to customize individual AI model provider settings rather than general AI Gateway behavior, please refer to the model-specific provider options in the AI SDK documentation.
You can use the order
array to specify the sequence in which providers should be attempted. Providers are specified using their slug
string. You can find the slugs in the table of available providers.
You can also copy the provider slug using the copy button next to a provider's name on a model's detail page. In the Vercel Dashboard:
- Click the AI Gateway tab,
- Then, click the Model List sub-tab on the left
- Click a model entry in the list.
The bottom section of the page lists the available providers for that model. The copy button next to a provider's name will copy their slug for pasting.
First, ensure you have the necessary package installed:
terminalpnpm install ai
Use the
providerOptions.gateway.order
configuration:app/api/chat/route.tsimport { streamText } from 'ai'; export async function POST(request: Request) { const { prompt } = await request.json(); const result = streamText({ model: 'anthropic/claude-4-sonnet', prompt, providerOptions: { gateway: { order: ['bedrock', 'anthropic'], // Try Amazon Bedrock first, then Anthropic }, }, }); return result.toUIMessageStreamResponse(); }
In this example:
- The gateway will first attempt to use Amazon Bedrock to serve the Claude 4 Sonnet model
- If Amazon Bedrock is unavailable or fails, it will fall back to Anthropic
- Other providers (like Vertex AI) are still available but will only be used after the specified providers
You can monitor which provider you used by checking the provider metadata in the response.
app/api/chat/route.tsimport { streamText } from 'ai'; export async function POST(request: Request) { const { prompt } = await request.json(); const result = streamText({ model: 'anthropic/claude-4-sonnet', prompt, providerOptions: { gateway: { order: ['bedrock', 'anthropic'], }, }, }); // Log which provider was actually used console.log(JSON.stringify(await result.providerMetadata, null, 2)); return result.toUIMessageStreamResponse(); }
{
"novita": {}, // final provider-specific metadata, if any -- can be empty
"gateway": {
// gateway-specific metadata
"routing": {
"originalModelId": "zai/glm-4.5",
"resolvedProvider": "novita",
"resolvedProviderApiModelId": "zai-org/glm-4.5",
"fallbacksAvailable": ["zai"],
"planningReasoning": "System credentials planned for: novita, zai. Total execution order: novita(system) → zai(system)",
"canonicalSlug": "zai/glm-4.5",
"finalProvider": "novita",
"attempts": [
{
"provider": "novita",
"providerApiModelId": "zai-org/glm-4.5",
"credentialType": "system",
"success": true,
"startTime": 1754638578812,
"endTime": 1754638579575
}
]
},
"cost": "0.0006766"
}
}
The gateway.cost
value is the amount debited from your AI Gateway Credits balance for this request. It is returned as a decimal string. For more on pricing see Pricing.
In cases where your request encounters issues with one or more providers or if your BYOK credentials fail, you'll find error detail in the attempts
field of the provider metadata:
"attempts": [
{
"provider": "novita",
"providerApiModelId": "zai-org/glm-4.5",
"credentialType": "byok",
"success": false,
"error": "Unauthorized",
"startTime": 1754639042520,
"endTime": 1754639042710
},
{
"provider": "novita",
"providerApiModelId": "zai-org/glm-4.5",
"credentialType": "system",
"success": true,
"startTime": 1754639042710,
"endTime": 1754639043353
}
]
Use the only
array to restrict routing to a specific subset of providers. Providers are specified by their slug and are matched against the model's available providers.
import { streamText } from 'ai';
export async function POST(request: Request) {
const { prompt } = await request.json();
const result = streamText({
model: 'anthropic/claude-4-sonnet',
prompt,
providerOptions: {
gateway: {
only: ['bedrock', 'anthropic'], // Only consider these providers.
// This model is also available via 'vertex', but it won't be considered.
},
},
});
return result.toUIMessageStreamResponse();
}
In this example:
- Restriction: Only
bedrock
andanthropic
will be considered for routing and fallbacks. - Error on mismatch: If none of the specified providers are available for the model, the request fails with an error indicating the allowed providers.
When both only
and order
are provided, the only
filter is applied first to define the allowed set, and then order
defines the priority within that filtered set. Practically, the end result is the same as taking your order
list and intersecting it with the only
list.
import { streamText } from 'ai';
export async function POST(request: Request) {
const { prompt } = await request.json();
const result = streamText({
model: 'anthropic/claude-4-sonnet',
prompt,
providerOptions: {
gateway: {
only: ['anthropic', 'vertex'],
order: ['vertex', 'bedrock', 'anthropic'],
},
},
});
return result.toUIMessageStreamResponse();
}
The final order will be vertex → anthropic
(providers listed in order
but not in only
are ignored).
You can combine AI Gateway provider options with provider-specific options. This allows you to control both the routing behavior and provider-specific settings in the same request:
import { streamText } from 'ai';
export async function POST(request: Request) {
const { prompt } = await request.json();
const result = streamText({
model: 'anthropic/claude-4-sonnet',
prompt,
providerOptions: {
anthropic: {
thinkingBudget: 0.001,
},
gateway: {
order: ['vertex'],
},
},
});
return result.toUIMessageStreamResponse();
}
In this example:
- We're using an Anthropic model (e.g. Claude 4 Sonnet) but accessing it through Vertex AI
- The Anthropic-specific options still apply to the model:
thinkingBudget
sets a cost limit of $0.001 per request for the Claude model
- You can read more about provider-specific options in the AI SDK documentation
You can view the available models for a provider in the Model List section under the AI Gateway tab in your Vercel dashboard.
Slug | Name | Website |
---|---|---|
anthropic | Anthropic | anthropic.com |
azure | Azure | ai.azure.com |
baseten | Baseten | baseten.co |
bedrock | Amazon Bedrock | aws.amazon.com/bedrock |
cerebras | Cerebras | cerebras.net |
cohere | Cohere | cohere.com |
deepinfra | DeepInfra | deepinfra.com |
deepseek | DeepSeek | deepseek.ai |
fireworks | Fireworks | fireworks.ai |
google | ai.google.dev | |
groq | Groq | groq.com |
inception | Inception | inceptionlabs.ai |
mistral | Mistral | mistral.ai |
moonshotai | Moonshot AI | moonshot.ai |
morph | Morph | morphllm.com |
novita | Novita | novita.ai |
openai | OpenAI | openai.com |
parasail | Parasail | parasail.com |
perplexity | Perplexity | perplexity.ai |
vercel | Vercel | v0.app |
vertex | Vertex AI | cloud.google.com/vertex-ai |
xai | xAI | x.ai |
zai | Z.ai | z.ai |
Provider availability may vary by model. Some models may only be available through specific providers or may have different capabilities depending on the provider used.
Was this helpful?