liveBook · Manning

Model fallback

As explained in Build AI-Enhanced Web Apps

A model fallback is a mechanism that reroutes requests to an alternative AI model or provider when the primary one is unavailable or fails.

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

How Build AI-Enhanced Web Apps covers it

How deep it goes
Implements ordered models, error callbacks, reset intervals, and selective retry policies rather than merely defining fallback behavior.
Examples
Uses Gemini and GPT-3.5 Turbo through ai-fallback, with environment-based API keys and logged provider errors.
What it assumes
Assumes a web application calling AI providers and requires provider API keys supplied through environment variables.
What you can do after
Apply it via ai-fallback to configure multi-provider model availability and targeted retry behavior.
How it connects
Connects model fallbacks with token limits, rate limiting, retries, and error handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.