llm-fallback-chain
Multi-provider failover for LLM calls. Try provider A; on failure, try B; on B failure, try C. Stop on the first one that returns Ok.
This is the cross-provider failover piece. Not retry-with-backoff against one provider (use llm-retry for that) and not a circuit breaker (use llm-circuit-breaker). All three compose.
Install
[]
= "0.1"
Async variant gated behind a feature:
[]
= { = "0.1", = ["tokio"] }
Sync
use ;
let chain = new?;
let result = chain.call?;
assert_eq!;
assert_eq!;
assert_eq!; // anthropic failed before openai won
Skip predicate
Skip a provider entirely when something else (an open circuit breaker, a feature flag, an outage signal) tells you not to try it:
let chain = new?
.with_skip?;
Custom should-fall-back predicate
Default: any error causes fallback. Restrict to specific error types:
let chain = new?
.with_should_fall_back;
A non-matching error re-raises as-is instead of triggering fallback.
Audit hook
let chain = chain.with_on_fallback;
The hook fires after each failure and before the next provider is tried. It does not fire for the last provider because there is no next.
Async (feature = "tokio")
use ;
let chain = new?;
let result = chain.call.await?;
All-failed error
When every provider in the chain fails, call returns a DynError containing an AllProvidersFailed with one Attempt per provider:
let err = chain.call.unwrap_err;
let failed = err..unwrap;
for attempt in &failed.attempts
Optional serde
Enable serde to get AttemptView, a lossy view of Attempt that serializes (the boxed dyn Error becomes its Display string):
= { = "0.1", = ["serde"] }
License
MIT.