VercelVercel
Menu

Provider Filtering & Ordering

Last updated March 7, 2026

By default, AI Gateway dynamically chooses providers based on recent uptime and latency. You can override this behavior to control which providers handle your requests and in what order using order and only in providerOptions.gateway.

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:

Through the Vercel Dashboard:

  1. Click the AI Gateway tab
  2. Click Model List on the left
  3. Click a model entry in the list

Through the AI Gateway site:

Visit a model's page on the AI Gateway models page (e.g., Claude Sonnet 4.5).

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.

  1. First, ensure you have the necessary package installed:

    Terminal
    pnpm install ai
  2. Use the providerOptions.gateway.order configuration:

    app/api/chat/route.ts
    import { streamText } from 'ai';
     
    export async function POST(request: Request) {
      const { prompt } = await request.json();
     
      const result = streamText({
        model: 'anthropic/claude-sonnet-4.6',
        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
  3. You can monitor which provider you used by checking the provider metadata in the response.

    app/api/chat/route.ts
    import { streamText } from 'ai';
     
    export async function POST(request: Request) {
      const { prompt } = await request.json();
     
      const result = streamText({
        model: 'anthropic/claude-sonnet-4.6',
        prompt,
        providerOptions: {
          gateway: {
            order: ['bedrock', 'anthropic'],
          },
        },
      });
     
      // Log which provider was actually used
      console.log(JSON.stringify(await result.providerMetadata, null, 2));
     
      return result.toUIMessageStreamResponse();
    }
{
  "anthropic": {},
  "gateway": {
    "routing": {
      "originalModelId": "anthropic/claude-sonnet-4.6",
      "resolvedProvider": "anthropic",
      "resolvedProviderApiModelId": "claude-sonnet-4.6",
      "internalResolvedModelId": "anthropic:claude-sonnet-4.6",
      "fallbacksAvailable": ["bedrock", "vertex"],
      "internalReasoning": "Selected anthropic as preferred provider for claude-sonnet-4.6. 2 fallback(s) available: bedrock, vertex",
      "planningReasoning": "System credentials planned for: anthropic. Total execution order: anthropic(system)",
      "canonicalSlug": "anthropic/claude-sonnet-4.6",
      "finalProvider": "anthropic",
      "attempts": [
        {
          "provider": "anthropic",
          "internalModelId": "anthropic:claude-sonnet-4.6",
          "providerApiModelId": "claude-sonnet-4.6",
          "credentialType": "system",
          "success": true,
          "startTime": 458753.407267,
          "endTime": 459891.705775
        }
      ],
      "modelAttemptCount": 1,
      "modelAttempts": [
        {
          "modelId": "anthropic/claude-sonnet-4.6",
          "canonicalSlug": "anthropic/claude-sonnet-4.6",
          "success": true,
          "providerAttemptCount": 1,
          "providerAttempts": [
            {
              "provider": "anthropic",
              "internalModelId": "anthropic:claude-sonnet-4.6",
              "providerApiModelId": "claude-sonnet-4.6",
              "credentialType": "system",
              "success": true,
              "startTime": 458753.407267,
              "endTime": 459891.705775
            }
          ]
        }
      ],
      "totalProviderAttemptCount": 1
    },
    "cost": "0.0045405",
    "marketCost": "0.0045405",
    "generationId": "gen_01K8KPJ0FZA7172X6CSGNZGDWY"
  }
}

The gateway.cost value is the amount debited from your AI Gateway Credits balance for this request. It is returned as a decimal string. The gateway.marketCost represents the market rate cost for the request. The gateway.generationId is a unique identifier for this generation that can be used with the Generation Lookup API. 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",
    "internalModelId": "novita:zai-org/glm-4.5",
    "providerApiModelId": "zai-org/glm-4.5",
    "credentialType": "byok",
    "success": false,
    "error": "Unauthorized",
    "startTime": 1754639042520,
    "endTime": 1754639042710
  },
  {
    "provider": "novita",
    "internalModelId": "novita:zai-org/glm-4.5",
    "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.

app/api/chat/route.ts
import { streamText } from 'ai';
 
export async function POST(request: Request) {
  const { prompt } = await request.json();
 
  const result = streamText({
    model: 'anthropic/claude-sonnet-4.6',
    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 and anthropic 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.

app/api/chat/route.ts
import { streamText } from 'ai';
 
export async function POST(request: Request) {
  const { prompt } = await request.json();
 
  const result = streamText({
    model: 'anthropic/claude-sonnet-4.6',
    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).

OptionTypeDescription
orderstring[]Provider slugs in the order they should be attempted
onlystring[]Restrict routing to only these provider slugs

Both options are set under providerOptions.gateway in the AI SDK or providerOptions in the REST API. See Available Providers for the full list of provider slugs.


Was this helpful?

supported.