Vercel Logo

How Do I Route to the Cheapest Provider Automatically?

The same model can be served by several providers at different prices. For example, Claude may be available through Anthropic, Amazon Bedrock, and Google Vertex.

If an application always uses one provider, it cannot take advantage of those price differences. Cost-based routing makes that choice per request.

Quick Answer

Set sort: "cost" in providerOptions.gateway. AI Gateway ranks eligible providers by estimated request cost and tries them in that order. Routing metadata records the provider metrics, execution order, and providers deprioritized for health.

Outcome

Route a request with cost sorting and read the provider ranking from the response metadata.

Fast Track

  1. Add providerOptions: { gateway: { sort: 'cost' } } to a call
  2. Run it
  3. Print finalStep.providerMetadata and find routing.sort.executionOrder

Hands-on exercise

The Taco Tuesday sidewalk sign uses an Anthropic model for its daily special. That gives us a useful multi-provider request to inspect.

Let's configure cost-based routing and inspect the provider ranking in the response metadata.

Requirements:

  • A scripts/route-cheapest.ts based on describe-special.ts
  • Add sort: 'cost' under providerOptions.gateway
  • After the call, print routing.finalProvider and the full routing.sort object from result.finalStep.providerMetadata.gateway
  • Print the request cost from gateway.cost

You could pin the current lowest-cost provider with order: ['bedrock'], but that configuration becomes stale when prices or provider health change. Cost sorting evaluates eligible providers on every request and routes around unhealthy providers automatically.

Try It

pnpm route-cheapest

Along with the announcement, you should see the routing decision:

"sort": {
  "option": "cost",
  "executionOrder": ["bedrock", "anthropic", "vertex"],
  "metrics": {
    "bedrock": 0.003,
    "anthropic": 0.003,
    "vertex": 0.005
  },
  "deprioritizedProviders": []
}

The metadata shows each provider's estimated price, the execution order, and any providers deprioritized for health. finalProvider identifies the provider that served the request.

Two issues you may encounter:

executionOrder has only one provider. The selected model may have one eligible provider. Check the provider list on the model's detail page and choose a current multi-provider model for this exercise.

A metrics value is null. The Gateway has no recent cost data to display for that provider. Run the script again later and inspect the updated routing metadata.

Commit

git commit -m "feat(routing): sort providers by cost for the daily special"

Done-When

  • scripts/route-cheapest.ts runs with sort: 'cost' and prints the sort metadata
  • You can name which provider served the request and why it won
  • You can explain why an unhealthy provider is deprioritized regardless of price

Solution

scripts/route-cheapest.ts
import { generateText } from "ai";
import { MENU } from "../lib/menu";
 
const result = await generateText({
  model: "anthropic/claude-sonnet-4.6",
  prompt:
    "You write the sidewalk sign for the Taco Tuesday truck. Today's special " +
    "is the Birria Eclipse. Write a three-sentence announcement that makes " +
    "people cross the street. Reference the menu for tone, and do not invent prices.\n\n" +
    MENU,
  providerOptions: {
    gateway: {
      sort: "cost",
    },
  },
});
 
const gateway = result.finalStep.providerMetadata?.gateway;
const routing = gateway?.routing as any;
 
console.log(result.text);
console.log("---");
console.log(`Served by:  ${routing?.finalProvider}`);
console.log(`Cost:       $${gateway?.cost}`);
console.log(JSON.stringify(routing?.sort, null, 2));

The request now carries a cost-routing policy, and its metadata records how the provider was selected.

Was this helpful?

supported.