Skip to main content

llm-fallback

Automatic failover between LLM providers. When OpenAI is down, seamlessly switch to Anthropic, Google, or any backup. Circuit breaker, retry logic, and latency-based routing built in.

The Pain

OpenAI goes down at 2 AM and your production chatbot returns 500s for 3 hours. Your users are furious. You could have fallen back to Claude or Gemini, but your code is hardwired to one provider.

Install

pip install llm-fallback

Quick Start

from llm_fallback import FallbackChain, Provider

chain = FallbackChain([
    Provider("openai", model="gpt-4o", api_key="sk-..."),
    Provider("anthropic", model="claude-3-5-sonnet-20241022", api_key="sk-ant-..."),
    Provider("openai", model="gpt-3.5-turbo", api_key="sk-..."),  # cheaper backup
])

# Tries providers in order until one succeeds
response = chain.chat("What is the capital of France?")
print(response.content)     # "The capital of France is Paris."
print(response.provider)    # "openai" (or whichever succeeded)
print(response.model)       # "gpt-4o"
print(response.latency_ms)  # 450

With Messages

response = chain.chat(
    messages=[
        {"role": "system", "content": "You are helpful."},
        {"role": "user", "content": "Explain quantum computing"},
    ],
    temperature=0.7,
    max_tokens=500,
)

Circuit Breaker

Automatically stops trying a provider that's failing:

chain = FallbackChain(
    providers=[...],
    circuit_breaker=True,       # Enable circuit breaker
    failure_threshold=3,        # Open after 3 consecutive failures
    recovery_timeout=60,        # Try again after 60 seconds
    timeout=30,                 # Per-request timeout
)

Latency-Based Routing

Route to the fastest provider instead of fixed order:

chain = FallbackChain(
    providers=[...],
    strategy="latency",  # "ordered" (default) or "latency"
)
# Tracks response times and prefers the fastest healthy provider

Provider Configuration

Provider(
    name="openai",          # Provider identifier
    model="gpt-4o",         # Model name
    api_key="sk-...",       # API key
    base_url=None,          # Custom endpoint (for proxies/self-hosted)
    timeout=30,             # Request timeout in seconds
    weight=1.0,             # Priority weight for routing
)

Supported Providers

Provider Name string Notes
OpenAI "openai" GPT-4o, GPT-3.5, etc.
Anthropic "anthropic" Claude 3/3.5/4
Google "google" Gemini (requires google-generativeai)
OpenAI-compatible "openai" with base_url Ollama, vLLM, Together, Groq, etc.

Callbacks

def on_failover(from_provider, to_provider, error):
    print(f"Failing over from {from_provider} to {to_provider}: {error}")
    slack_alert(f"LLM failover: {from_provider} -> {to_provider}")

chain = FallbackChain(
    providers=[...],
    on_failover=on_failover,
    on_success=lambda r: metrics.record(r.provider, r.latency_ms),
)

Features

  • Automatic failover — tries next provider on any error
  • Circuit breaker — stops hammering a dead provider
  • Latency routing — prefer the fastest healthy provider
  • Retry with backoff — configurable retry per provider
  • Timeout enforcement — per-request timeouts
  • Unified API — same interface regardless of provider
  • Callbacks — hook into failover events for alerting
  • Zero required deps — only needs the provider SDK you're already using

License

MIT

Metadata

Release files for llm-fallback 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-fallback 0.1.0
File Size Uploaded Provenance
llm_fallback-0.1.0.tar.gz 6.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-fallback 0.1.0
File Interpreter ABI Platform
llm_fallback-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / llm_fallback-0.1.0.tar.gz

Download URL llm_fallback-0.1.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d36cbea55bfe1529ba8986a47b1dc232ad9466f165574831de8f6c6a4c241e8f
BLAKE2b-256 checksum
How to use checksums
317be72293d3f6e62dd327fe2b36592d5d003ff1a0153323c78a1b76b0a18326
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Release files / llm_fallback-0.1.0-py3-none-any.whl

Download URL llm_fallback-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b414e6e301b6b384fe4766d8e7ea6ae677de9178de3e7425a175401de9477824
BLAKE2b-256 checksum
How to use checksums
e7885a2fa84d2930d5282a662ebedae8077cd78fd3235fc96a64cfa300279d3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Security for llm-fallback 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page