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.
Outcome
Route a request with cost sorting and read the provider ranking from the response metadata.
Fast Track
- Add
providerOptions: { gateway: { sort: 'cost' } }to a call - Run it
- Print
finalStep.providerMetadataand findrouting.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.tsbased ondescribe-special.ts - Add
sort: 'cost'underproviderOptions.gateway - After the call, print
routing.finalProviderand the fullrouting.sortobject fromresult.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-cheapestAlong 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.tsruns withsort: '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
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.
Related Questions
Was this helpful?