LangEx.Middleware.ModelFallback (LangEx v0.12.0)

Copy Markdown View Source

Middleware that retries a failed model call against fallback models.

When the primary model call fails (provider outage, degradation, exhausted retries), the same request — messages and tools — is retried against an ordered list of fallback models, and the first success is returned. An incident-investigation agent is needed most exactly when infrastructure, possibly including its own LLM provider, is degraded.

A failed call surfaces as a raised exception: LangEx.LLM.ChatModel.node/1 crashes with a MatchError when the provider (or LangEx.LLM.Resilient after exhausting retries) returns {:error, reason}, and provider adapters can raise transport exceptions directly. When every fallback also fails, the original primary failure is re-raised with its stacktrace.

Options

  • :models — ordered fallback list; each entry is a model string (provider resolved via LangEx.LLM.Registry) or a {provider, model} tuple pinning the provider module explicitly. Default [], which makes the middleware a no-op.
  • :llm_opts — extra options merged into every fallback call (:resilient, :thinking, :api_key, ...). Default [].

Example

LangEx.Middleware.ModelFallback.new(
  models: ["claude-sonnet-5", {LangEx.LLM.OpenAI, "gpt-5"}],
  llm_opts: [resilient: true]
)

Summary

Functions

Builds a model-fallback middleware. See the module doc for options.

Types

model_spec()

@type model_spec() :: String.t() | {module(), String.t()}

Functions

new(opts \\ [])

@spec new(keyword()) :: LangEx.Middleware.t()

Builds a model-fallback middleware. See the module doc for options.