langchain.js
    Preparing search index...

    Function modelFallbackMiddleware

    • Middleware that provides automatic model fallback on errors.

      This middleware attempts to retry failed model calls with alternative models in sequence. When a model call fails, it tries the next model in the fallback list until either a call succeeds or all models have been exhausted.

      Parameters

      • ...fallbackModels: any[]

        The fallback models to try, in order.

      Returns AgentMiddleware

      A middleware instance that handles model failures with fallbacks

      import { createAgent, modelFallbackMiddleware } from "langchain";

      // Create middleware with fallback models (not including primary)
      const fallback = modelFallbackMiddleware(
      "openai:gpt-4o-mini", // First fallback
      "anthropic:claude-sonnet-4-5-20250929", // Second fallback
      );

      const agent = createAgent({
      model: "openai:gpt-4o", // Primary model
      middleware: [fallback],
      tools: [],
      });

      // If gpt-4o fails, automatically tries gpt-4o-mini, then claude
      const result = await agent.invoke({
      messages: [{ role: "user", content: "Hello" }]
      });