Skip to main content

Overview

FallbackProvider tries configured providers in order and tracks a circuit breaker for each. Use it when another provider can satisfy the same request after an eligible failure. Ensure every candidate supports the required tools, input media, and output format.

Quick Start

This host function takes configured models and returns an Agent. The caller supplies credentials, runs the Agent, and closes it afterward.
Choose the order deliberately. A backup can have different quality, cost, latency, or regional availability. Test those differences with representative evaluations.

Circuit Breaker

Configuration

This example uses the default classifier and custom thresholds. The object is independent of any live provider.
Defaults are five failures, a 30-second cooldown, and two half-open successes. halfOpenMaxAttempts controls successful recovery probes; it is not a concurrency semaphore. Use a separate host limiter to bound concurrent work.

Error Classification

The default classifier recognizes common HTTP status codes, network codes, and error-message patterns: 429/5xx and network failures generally retry; 401/403/404 cascade; recognized content-policy errors are fatal. Unknown errors default to retry. Supply classifyError(error) when your provider’s error shape requires a different policy. “Retry” here selects another provider; it does not guarantee repeated attempts against the same provider or replay of the entire Agent run.

FallbackProvider

Callbacks

Fallback does not emit EventBus events. Use onFallback and getBreaker(providerKey)?.state. The provider key is ${provider.providerId}:${provider.modelId}; derive it from the configured instance so a model override does not leave the lookup stale.

Streaming boundary

A stream may switch providers only before any chunk has been emitted. After output begins, a provider failure propagates to the caller. Starting another provider mid-stream would mix partial answers and tool-call state. Handle the incomplete response at the transport boundary; do not display it as a completed result.

Best Practices

  • Verify every fallback model supports your schema, tools, and media.
  • Exercise a failing first provider and a successful backup with fixtures before live testing.
  • Track fallback frequency and answer quality separately; successful delivery alone does not prove equivalent behavior.
  • Keep external effects idempotent. A model fallback does not make retrying an entire application operation safe.
Continue with model patterns, operating limits, and recovery contracts.