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.
Outcome
Call models from two providers with the same code and inspect the cost and routing metadata on each response.
Fast Track
- Run
pnpm one-key - Note both models answered with no provider SDKs installed
- Find
fallbacksAvailableandcostin 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.tsthat asks the same question ofopenai/gpt-5.4-miniandanthropic/claude-sonnet-4.6 - Same
generateTextcall for both; only the model string changes - For each response, print the answer, the actual cost (
gateway.cost), and thefallbacksAvailablelist 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
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
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
fallbacksAvailablein 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
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
Was this helpful?