Quick Start
1
One-line resilience
2
Cross-provider chain
Use LiteLLM-style prefixes when mixing providers:
3
Notice the switch
Subscribe to
HookEvent.MODEL_FALLBACK to react the moment the primary model is swapped out:How It Works
On transient errors (503, timeout, model overloaded), the agent retries the same turn against the next model infallback_models. Successful calls stay on the primary model.
Failover fires on retryable errors classified by the LLM error classifier and covers every turn shape — non-streaming, streaming, tool-iteration turns, reflection turns, and their async equivalents. A 503 on a streaming chunk pushes the same turn to the next model in fallback_models; the user sees continuous output, not a failure.
Observing the Switch
Subscribe toHookEvent.MODEL_FALLBACK (or the MODEL_FALLBACK stream event) to react the moment the primary model is swapped out — for alerts, metrics, a UI notice, or a per-user notification.
MODEL_FALLBACK is dispatched through the agent-scoped hook registry passed via
Agent(hooks=...). Register on the agent’s registry (as shown below) so the hook fires on both sync and async runs. Hooks registered only on the global default registry may be skipped on the async path.ModelFallbackInput fields
Notification only — the turn already continued on
to_model. Provider internals are redacted; only the failure class reaches your hook. Zero overhead when unsubscribed. Errors inside the hook never break the fallback path.Hook subscription
Stream event
When a stream callback is active, the same swap emits aStreamEventType.MODEL_FALLBACK event carrying the four fields in metadata, plus agent_id, session_id, and run_id.
The stream event honours the run’s
emit_events flag: when events are suppressed, StreamEventType.MODEL_FALLBACK is skipped, but HookEvent.MODEL_FALLBACK still fires. Use the hook when you need a guaranteed signal regardless of stream configuration.Sync and async paths
Both call sites emit — the sync_chat_completion path and the async _handle_async_llm_error path — so the hook fires whether you run agent.start(...) or await agent.astart(...).
- Sync path
- Async path
Async hooks are awaited (not fire-and-forget). The fallback retry does not proceed until the hook returns, so long-running notifiers should keep their work non-blocking or offload it.
Configuration Options
Model Fallback is configured throughLLMConfig — set model for the primary and fallback_models for the ordered backup chain.
The decision diagram below shows which chain shape fits which scenario.
Streaming, tool-iteration and reflection turns route through the same failover engine as plain single-shot calls (unified in PraisonAI PR #2665). You don’t need a separate
fallback_models= setting for streaming paths.LLMConfig API Reference
Full
LLMConfig surface is covered by the auto-generated SDK reference — see Hook Events for the paired MODEL_FALLBACK hook.Best Practices
Put a cheap same-provider fallback last
Put a cheap same-provider fallback last
Useful for rate limits, not full provider outages — a cheap model on the same API may still fail if the provider is down.
Order by latency and cost
Order by latency and cost
Fallback runs the same prompt; a much weaker model may return a worse answer, not a missing one.
Limit chain length to 2–3
Limit chain length to 2–3
Longer chains delay user-visible errors without improving success rates much.
Use provider prefixes when mixing
Use provider prefixes when mixing
LiteLLM-style names (
anthropic/..., openai/...) route credentials correctly across providers.Related
LLM Configuration
Endpoints, API keys, and auth headers.
Models
Choosing models for agents.
Model Router
Dynamic model selection policies.
Rate Limiter
Throttle requests before they fail.

