Skip to main content
Failover automatically switches between LLM providers when one fails, keeping agents running during API outages or rate limits.
The user sends a message; if the primary LLM profile fails, the agent tries the next profile in the failover chain.
Failover integrates with LLM Error Classification via FailoverDecision, which coordinates profile rotation with typed error handling.

Quick Start

1

Run an agent with failover

2

Add more profiles

3

Monitor provider health


How failover activates during retries

Failover now drives LLM retries through direct integration with the retry mechanism:
  • On every LLM call, the system first gets the current profile via get_next_profile() and applies its api_key, base_url, and model settings
  • On success, mark_success(profile) is called to track the working provider
  • On failure, mark_failure(profile, error, is_rate_limit=...) marks the provider as failed, then get_next_profile() fetches the next available provider
  • Profile switching overrides non-retryable classification—one extra attempt is always granted after switching providers
  • The LLM automatically updates request parameters (api_key, base_url, model) when switching between profiles

How It Works


Configuration Options

FailoverManager

Manager class reference

AuthProfile

Provider credential profile

Auth Profiles

Configure credentials for each provider:

Common Patterns


Failover Callbacks

React to failover events:
Sync + async parity (from PraisonAI #2386). The retry callback now fires on both the sync (agent.chat) and async (await agent.achat) LLM paths. Older versions silently skipped the callback on async calls — upgrade to get retry observability for gateway bots, async tools, and any await-based agent code.

Failover callbacks run outside the pool lock

Your on_failover callbacks are invoked after FailoverManager releases its internal lock (PraisonAI PR #3574). This means:
  • Blocking work in a callback (network I/O, logging, metrics) does not stall other agents queuing on the pool.
  • A callback that acquires an external lock will not deadlock against the pool’s lock.
  • Callbacks still run in the thread that triggered the failover — if you care about ordering, that is preserved.

Provider Status

Monitor provider health:

Thread safety

FailoverManager is safe to share across concurrent agents and threads — it is the process-wide credential pool (PraisonAI PR #3574). Concurrent agents hitting a rate limit at the same time cannot observe torn reads or corrupt the pool: every read and mutation of the profile list and each profile’s status is serialised internally. Wire one FailoverManager into many agents and let them share the same credential pool — no external locking required. See Thread Safety for how this fits with the rest of the SDK’s concurrency model.

Best Practices

Always have at least 2-3 providers configured. This ensures availability even during major outages.
Enable exponential_backoff=True to avoid hammering providers during issues. This helps you stay within rate limits.
Order providers by cost and reliability. Put cheaper/faster providers first, with premium providers as fallback.
Use the on_failover callback to track when failovers occur. This helps identify provider issues early. The callback fires on both sync and async LLM paths — works the same whether your agent runs via agent.chat() or await agent.achat().
Pair failover with LLM Error Classification so FailoverDecision coordinates profile rotation with typed errors.
Load keys from environment variables or a secrets manager — never commit credentials to version control.

LLM Error Classification

Typed errors that drive failover decisions

Providers

Supported LLM providers