---
title: "Why AI Gateway?"
description: "Call models from two providers with the same AI SDK code and one Gateway key, then inspect the cost and routing metadata returned with each response."
canonical_url: "https://vercel.com/academy/ai-gateway/why-ai-gateway"
md_url: "https://vercel.com/academy/ai-gateway/why-ai-gateway.md"
docset_id: "vercel-academy"
doc_version: "1.0"
last_updated: "2026-08-08T23:27:13.205Z"
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>

# Why AI Gateway?

# Why Use AI Gateway Instead of Calling Providers Directly?

Calling a provider directly is a reasonable place to start. Production gets more complicated when an application needs another model, a fallback provider, or a cost record for each request.

We can see what the Gateway adds with one short script.

\*\*Note: Quick Answer\*\*

AI Gateway gives an application one key and one interface for models from multiple providers. The same request path adds provider routing, failover, and request-level cost metadata with zero token markup. Test failure behavior before relying on failover in production.

## Outcome

Call models from two providers with the same code and inspect the cost and routing metadata on each response.

## Fast Track

1. Run `pnpm one-key`
2. Note both models answered with no provider SDKs installed
3. Find `fallbacksAvailable` and `cost` in the printed metadata

## Hands-on exercise

The Taco Tuesday assistant uses an OpenAI model. We want to compare it with Claude without adding another provider SDK or changing the request code.

Let's make it a for-loop instead.

Requirements:

- A `scripts/one-key.ts` that asks the same question of `openai/gpt-5.4-mini` and `anthropic/claude-sonnet-4.6`
- Same `generateText` call for both; only the model string changes
- For each response, print the answer, the actual cost (`gateway.cost`), and the `fallbacksAvailable` list from the routing metadata

`fallbacksAvailable` shows which alternate providers were eligible for the request. A healthy response exposes the routing plan, but it does not prove that a fallback will behave correctly during an outage.

## Try It

```bash
pnpm one-key
```

```
=== openai/gpt-5.4-mini ===
"The Birria Eclipse isn't dinner, it's an event with a dipping sauce."
Cost:      $0.0000689
Fallbacks: none needed — served by openai

=== anthropic/claude-sonnet-4.6 ===
"Braised-beef tacos with consommé: napkins mandatory, regrets impossible."
Cost:      $0.0021340
Fallbacks: bedrock, vertex were standing by
```

Two issues you may encounter:

**One of the models returns `429`.** Free-tier limits apply per model. Wait, then run the script again, or choose another currently eligible model.

**Both requests succeed on the first provider.** Good. This run proves the shared interface and metadata. Use the outage drill in lesson 1.4 to inspect failure behavior separately.

## Commit

```bash
git commit -m "feat(gateway): add one-key script comparing models across providers"
```

## Done-When

- [ ] Both models answered through one API key with no provider SDKs in `package.json`
- [ ] You found `fallbacksAvailable` in a response you didn't configure failover for
- [ ] You can explain the additional hop in one sentence, including its zero token markup and request metadata

## Solution

```ts filename="scripts/one-key.ts"
import { generateText } from "ai";

const models = ["openai/gpt-5.4-mini", "anthropic/claude-sonnet-4.6"];

for (const model of models) {
  const result = await generateText({
    model,
    prompt:
      "In one sentence, describe the Birria Eclipse: slow-braised beef birria " +
      "tacos with consommé for dipping. Make it irresistible.",
  });

  const gateway = result.finalStep.providerMetadata?.gateway;
  const routing = gateway?.routing as any;
  const fallbacks = routing?.fallbacksAvailable ?? [];

  console.log(`=== ${model} ===`);
  console.log(`"${result.text.trim()}"`);
  console.log(`Cost:      $${gateway?.cost}`);
  console.log(
    fallbacks.length
      ? `Fallbacks: ${fallbacks.join(", ")} were standing by`
      : `Fallbacks: none needed — served by ${routing?.finalProvider}`
  );
  console.log();
}
```

Both providers used the same application code. The response metadata tells us what the Gateway did around each model call.

## Related Questions

- [Will AI Gateway work with my existing AI stack?](/ai-gateway/stay-reliable/existing-ai-stack)
- [How is AI Gateway priced?](/ai-gateway/save-money/ai-gateway-pricing)
- [How do I survive a provider outage?](/ai-gateway/stay-reliable/provider-outage)


---

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