Model fallback reroutes requests across AI models or providers when the primary service becomes unavailable or fails. Treat it separately from ordinary error handling because it has distinct recovery behavior. section 8.2.2
A fallback model wraps an ordered model list, records provider failures, and resets model availability after a configured interval. Environment variables supply provider credentials, while retry rules can restrict fallback attempts to statuses such as 429 and 500. section 8.2.2
Listing 8.3 Creating a fallback model
import { createFallback } from 'ai-fallback'; #1
import { createOpenAI } from '@ai-sdk/openai';
import { createGoogleGenerativeAI } from '@ai-sdk/google';
const openAPIKey = process.env.OPENAI_API_KEY;
const googleAPIKey = process.env.GEMINI_API_KEY;
// Create a fallback model instance
const supportedModel = createFallback({ #2
models: [
createGoogleGenerativeAI(
{ apiKey: googleAPIKey })('models/gemini-2.0-flash'),
createOpenAI({ apiKey: openAPIKey })('gpt-3.5-turbo'),
],
onError: (error, modelId) => {
console.error(`Error with model ${modelId}:`, error);
},
modelResetInterval: 60000, #3
});
Creates a Google Gemini primary model with an OpenAI fallback, logs model errors, and resets fallback state after 60 seconds. section 8.2.2
