Building a Resilient Multi-Provider AI Routing Layer with Vercel AI SDK
How to build a production-grade LLM routing layer with failovers, fallback models, and structured Zod outputs using Vercel AI SDK.

When deploying Large Language Models (LLMs) to production, reliability is often the largest bottleneck. Relying on a single model provider introduces single-point-of-failure risks: API rate limits (HTTP 429), transient service downtime (HTTP 500/503), or sudden latency spikes.
To guarantee uptime and cost efficiency, teams need a dynamic routing layer that can automatically failover to fallback models or switch providers based on input parameters.
In this guide, we will implement a resilient, type-safe multi-provider routing layer using the Vercel AI SDK and Zod.
The Architecture of LLM Resiliency
A resilient routing layer operates on three core principles:
- Sequential Failovers: If the primary provider fails, the system automatically retries the request using a secondary provider.
- Dynamic Selection: Short prompts go to fast, low-cost models, while larger inputs route to high-context models.
- Type-Safe Validation: Regardless of which model handles the request, the output schema remains identical and validated.
+-------------------+
| Incoming Request |
+-------------------+
|
v
+-----------------------+
| Dynamic Route Selector|
+-----------------------+
/ \
(Short Input) (Long Input)
/ \
v v
+-----------------+ +-----------------+
| Primary Model | | Primary Model |
| (OpenAI Luna) | | (Gemini-3.1-Pro)|
+-----------------+ +-----------------+
| |
(On Failure) (On Failure)
v v
+-----------------+ +-----------------+
| Fallback Model | | Fallback Model |
| (Gemini-3.5-Fl) | | (Claude-Sonnet5)|
+-----------------+ +-----------------+
\ /
\ /
v v
+-------------------------+
| Zod Schema Validation |
+-------------------------+
|
v
+-------------------+
| Validated Output |
+-------------------+Implementing Fallbacks with Vercel AI SDK
The Vercel AI SDK provides a built-in fallback wrapper that allows you to specify a prioritized list of models. If a call to the first model fails, the SDK automatically retries using the subsequent models in the array.
Here is how you can initialize a resilient model with fallback providers:
import { fallback } from 'ai';
import { openai } from '@ai-sdk/openai';
import { google } from '@ai-sdk/google';
import { anthropic } from '@ai-sdk/anthropic';
// Initialize a resilient model that crosses three distinct providers
export const resilientModel = fallback([
openai('terra'),
anthropic('claude-sonnet-5'),
google('gemini-3.1-pro')
]);When you call generateText or generateObject with resilientModel, the SDK automatically handles errors, timeouts, and retries.
Local Fallbacks with Ollama
For offline availability, hybrid environments, or zero-cost execution during local testing, you can integrate locally hosted models via Ollama. The Vercel AI SDK treats Ollama models as first-class providers, allowing you to transition directly from cloud APIs to local silicon.
Here is how you configure a hybrid cloud-to-local fallback:
import { fallback } from 'ai';
import { openai } from '@ai-sdk/openai';
import { ollama } from '@ai-sdk/ollama';
// Fallback to a locally hosted Llama 4 Scout model if the OpenAI API is unreachable
export const hybridModel = fallback([
openai('luna'),
ollama('llama4-scout')
]);Type-Safe Structured Data Extraction
One of the challenges of multi-provider routing is ensuring that different models adhere to the exact same output structure. Using Zod with the generateObject API resolves this.
Let's define a schema to extract structured metadata from raw developer profiles:
import { generateObject } from 'ai';
import { z } from 'zod';
import { resilientModel } from './models';
const ProfileSchema = z.object({
fullName: z.string(),
skills: z.array(z.string()),
yearsOfExperience: z.number(),
certifications: z.array(
z.object({
name: z.string(),
issuer: z.string()
})
)
});
export async function extractProfile(text: string) {
try {
const { object } = await generateObject({
model: resilientModel,
schema: ProfileSchema,
prompt: `Extract structured profile details from the following resume text:\n\n${text}`
});
return object;
} catch (error) {
console.error('Failed to extract profile after exhausting all fallbacks:', error);
throw new Error('Profile extraction failed');
}
}Because the output is parsed and validated by Zod at runtime, your application code can confidently rely on the returned structure, regardless of which model in the fallback chain succeeded.
Dynamic Routing Based on Context Size
While sequential fallbacks protect against outright downtime, they do not optimize for cost or token limitations. For instance, sending a 100,000-token document to a model with a 128,000-token limit is fine, but sending it to a model with a 16,000-token limit will trigger an immediate error.
We can build a dynamic router that inspects the input size before choosing the model chain:
import { LanguageModel, generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { google } from '@ai-sdk/google';
import { anthropic } from '@ai-sdk/anthropic';
// Define specialized model chains
const standardChain = fallback([
openai('luna'),
google('gemini-3.5-flash')
]);
const highContextChain = fallback([
google('gemini-3.1-pro'),
anthropic('claude-sonnet-5')
]);
// Estimate token count (simple word-count approximation for demo)
function estimateTokens(text: string): number {
return text.trim().split(/\s+/).length * 1.3;
}
export async function smartRouteRequest(promptText: string) {
const tokenEstimate = estimateTokens(promptText);
// Route to the appropriate chain based on input size
const model = tokenEstimate > 50000 ? highContextChain : standardChain;
return generateText({
model,
prompt: promptText
});
}This pattern keeps costs low by routing standard tasks to inexpensive, high-speed models while reserving premium models with larger context windows for heavy payloads.
Telemetry and Observability
To monitor how often your routing layer triggers fallback models, you can hook into Vercel AI SDK's experimental telemetry features. This compiles metrics on token usage, latency, and provider success rates:
import { generateText } from 'ai';
import { resilientModel } from './models';
const response = await generateText({
model: resilientModel,
prompt: 'Analyze this log output...',
experimental_telemetry: {
isEnabled: true,
functionId: 'analyze-logs',
metadata: {
environment: 'production'
}
}
});These traces compile into standard OpenTelemetry collectors, giving your team complete visibility into failovers and provider health.
Conclusion and Trade-offs
A multi-provider routing layer increases reliability. However, it is important to consider the trade-offs:
- Latent Mismatches: Fallback models can exhibit different writing styles, formatting nuances, or compliance characteristics.
- Cold Starts: Retrying a failed request against a secondary model adds latency to the user experience.
- Provider API Keys: Your infrastructure must configure and maintain API keys, rate limit tiers, and billing accounts across multiple vendors.
By using the Vercel AI SDK's unified API interfaces, you can build a highly resilient routing architecture that protects your production applications from individual provider downtime.