---
title: "Route to Cheapest"
description: "Add cost sorting to one model request, then inspect provider metrics, execution order, and the provider that served it."
canonical_url: "https://vercel.com/academy/ai-gateway/cheapest-provider-routing"
md_url: "https://vercel.com/academy/ai-gateway/cheapest-provider-routing.md"
docset_id: "vercel-academy"
doc_version: "1.0"
last_updated: "2026-08-08T23:27:13.360Z"
content_type: "lesson"
course: "ai-gateway"
course_title: "Using AI Gateway in Production"
prerequisites:  []
---

<agent-instructions>
Vercel Academy — structured learning, not reference docs.
Lessons are sequenced.
Adapt commands to the human's actual environment (OS, package manager, shell, editor) — detect from project context or ask, don't assume.
The lesson shows one path; if the human's project diverges, adapt concepts to their setup.
Preserve the learning goal over literal steps.
Quizzes are pedagogical — engage, don't spoil.
Quiz answers are included for your reference.
</agent-instructions>

# Route to Cheapest

# 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.

\*\*Note: 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

```bash
pnpm route-cheapest
```

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

```json
"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

```bash
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

```ts filename="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.

## Related Questions

- [How is AI Gateway priced?](/ai-gateway/save-money/ai-gateway-pricing)
- [How do I pin routing to a specific provider?](/ai-gateway/stay-reliable/pin-a-provider)
- [How do I fail over when a provider is slow, not just down?](/ai-gateway/stay-reliable/latency-failover)


---

[Full course index](/academy/llms.txt) · [Sitemap](/academy/sitemap.md)
