Fault-tolerant LLM provider rotation with circuit breaker, quotas, and model-first routing
Project description
llm-rotator
Fault-tolerant LLM provider rotation with circuit breaker, quotas, and model-first routing.
Features
- Model-First Routing — tries all keys for a model before downgrading
- Circuit Breaker — granular blocking per key+model with TTL
- Quota Management — token and request quotas with automatic reset
- Tier System — quality ceiling to control model selection
- Lifecycle Hooks — extensible before/after request callbacks
- Streaming — with mid-stream error recovery
- Tool Calling & Structured Output — unified format across all providers
- Structured Logging — full routing chain in one log line
- Quota Warnings — alerts when usage crosses a threshold
- LangChain Integration — drop-in
RotatorChatModelfor chains and agents - JSON Config — load config from file with env variable substitution
- Async-First — built on asyncio + httpx
Supported Providers
- OpenAI (and any OpenAI-compatible API: OpenRouter, Groq, etc.)
- Google Gemini
- Anthropic Claude
Installation
pip install llm-rotator
# With optional dependencies
pip install llm-rotator[redis] # Redis state backend
pip install llm-rotator[langchain] # LangChain integration
pip install llm-rotator[all] # Everything
Quick Start
Basic Usage
import asyncio
from llm_rotator import LLMRotator, OpenAIClient, RotatorConfig
config = RotatorConfig(
providers=[
{
"name": "openai",
"client_type": "openai",
"priority": 1,
"models": ["gpt-4o"],
"keys": [{"token": "sk-...", "alias": "main"}],
}
]
)
async def main():
rotator = LLMRotator(config, clients={"openai": OpenAIClient()})
response = await rotator.complete(
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.content)
print(f"Tokens used: {response.usage.total_tokens}")
asyncio.run(main())
JSON Config
Load configuration from a JSON file with env variable substitution for secrets:
{
"providers": [
{
"name": "openai",
"client_type": "openai",
"priority": 1,
"models": ["gpt-4o"],
"keys": [
{"token": "$OPENAI_API_KEY", "alias": "main"},
{"token": "${OPENAI_BACKUP_KEY:-}", "alias": "backup"}
]
}
]
}
rotator = LLMRotator.from_json("config.json", clients={"openai": OpenAIClient()})
Supported patterns: $VAR, ${VAR}, ${VAR:-default}.
Multi-Provider with Fallback
If the first provider fails (rate limit, server error), the rotator automatically tries the next one:
from llm_rotator import AnthropicClient, GeminiClient
config = RotatorConfig(
providers=[
{
"name": "openai",
"client_type": "openai",
"priority": 1,
"model_groups": [
{
"name": "flagship",
"tier": 1,
"models": ["gpt-4o"],
"token_quota": {"limit": 250_000, "reset": "daily_utc"},
},
{
"name": "mini",
"tier": 3,
"models": ["gpt-4o-mini"],
},
],
"keys": [
{"token": "sk-key1", "alias": "key1"},
{"token": "sk-key2", "alias": "key2"},
],
},
{
"name": "anthropic",
"client_type": "anthropic",
"priority": 2,
"models": ["claude-sonnet-4-20250514"],
"keys": [{"token": "sk-ant-...", "alias": "claude_key"}],
},
{
"name": "gemini",
"client_type": "gemini",
"priority": 3,
"models": ["gemini-2.0-flash"],
"keys": [{"token": "AIza...", "alias": "gemini_key"}],
},
]
)
rotator = LLMRotator(
config,
clients={
"openai": OpenAIClient(),
"anthropic": AnthropicClient(),
"gemini": GeminiClient(),
},
)
Streaming
async for chunk in rotator.stream(
messages=[{"role": "user", "content": "Tell me a story"}]
):
print(chunk.delta, end="", flush=True)
Mid-stream errors are handled automatically — the rotator retries from the beginning on the next available candidate.
Tool Calling
Unified OpenAI-compatible tool format across all providers:
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}
]
response = await rotator.complete(
messages=[{"role": "user", "content": "Weather in Paris?"}],
tools=tools,
)
if response.tool_calls:
for tc in response.tool_calls:
print(f"Call {tc.name}({tc.arguments})")
Tools are automatically translated to each provider's native format (Gemini functionDeclarations, Anthropic tools with input_schema).
Structured Output
# JSON mode
response = await rotator.complete(
messages=[{"role": "user", "content": "Return a JSON with name and age"}],
response_format={"type": "json_object"},
)
Tier-Based Routing
Control model quality per request using RoutingContext:
from llm_rotator import RoutingContext
# Simple task — use only economy models (tier >= 3)
result = await rotator.complete(
messages=[{"role": "user", "content": "Classify this text"}],
routing=RoutingContext(tier=3),
)
# Complex task — full access starting from flagship (tier >= 1, default)
result = await rotator.complete(
messages=[{"role": "user", "content": "Write a detailed analysis"}],
routing=RoutingContext(tier=1),
)
# Restrict to specific providers
result = await rotator.complete(
messages=messages,
routing=RoutingContext(allowed_providers=["gemini"]),
)
LangChain Integration
Drop-in replacement for any LangChain BaseChatModel:
from llm_rotator import RotatorChatModel
from langchain_core.messages import HumanMessage
model = RotatorChatModel(rotator=rotator)
# Use directly
response = await model.ainvoke([HumanMessage(content="Hello")])
# Use in chains
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
chain = ChatPromptTemplate.from_messages([
("system", "Answer concisely."),
("user", "{question}"),
]) | model | StrOutputParser()
result = await chain.ainvoke({"question": "Capital of France?"})
# With tools
bound = model.bind_tools([...])
result = await bound.ainvoke([HumanMessage(content="Search for Python")])
# With routing context
result = await model.ainvoke(
[HumanMessage(content="Simple task")],
routing=RoutingContext(tier=3),
)
Structured Logging
Full routing chain in one log line:
[req:abc123] [OpenAI/flagship] gpt-4o (main_key) → 429 RateLimit → gpt-4o (backup_key) → 200 OK (usage: 150 tokens)
[req:def456] [OpenAI/flagship] gpt-4o (key1) → 500 ServerError → [Gemini/_default] gemini-flash (google_key) → 200 OK (usage: 120 tokens)
Pass a custom logger:
import logging
my_logger = logging.getLogger("my_app.llm")
rotator = LLMRotator(config, clients=clients, logger=my_logger)
Quota Warnings
Get notified when usage approaches the limit:
from llm_rotator.quota import QuotaWarning
async def on_warning(w: QuotaWarning):
print(f"Quota {w.scope}: {w.percentage:.0%} ({w.current}/{w.limit})")
rotator = LLMRotator(
config,
clients=clients,
on_quota_warning=on_warning,
warning_threshold=0.8, # default: 80%
)
Lifecycle Hooks
Add custom logic without modifying the rotator:
class BudgetHook:
async def before_request(self, ctx, candidate):
"""Return False to skip a candidate."""
if ctx.tags.get("user_tier") == "free" and candidate.model_group == "flagship":
return False
return True
async def after_response(self, ctx, candidate, usage):
"""Called after a successful response."""
print(f"Used {usage.total_tokens} tokens on {candidate.model}")
async def on_fallback(self, ctx, from_candidate, to_candidate, error):
"""Called when switching to the next candidate."""
print(f"Fallback from {from_candidate.model}: {error}")
rotator.add_hook(BudgetHook())
result = await rotator.complete(
messages=messages,
routing=RoutingContext(tags={"user_tier": "free"}),
)
Redis Backend
For multi-instance deployments:
from llm_rotator import RedisBackend
backend = await RedisBackend.from_url("redis://localhost:6379/0")
rotator = LLMRotator(config, clients=clients, backend=backend)
OpenAI-Compatible Providers
Use any OpenAI-compatible API by setting base_url:
config = RotatorConfig(
providers=[
{
"name": "openrouter",
"client_type": "openai",
"priority": 1,
"base_url": "https://openrouter.ai/api/v1",
"models": ["meta-llama/llama-3.3-70b-instruct:free"],
"keys": [{"token": "sk-or-...", "alias": "openrouter"}],
},
{
"name": "groq",
"client_type": "openai",
"priority": 2,
"base_url": "https://api.groq.com/openai/v1",
"models": ["llama-3.3-70b-versatile"],
"keys": [{"token": "gsk_...", "alias": "groq"}],
},
]
)
How Rotation Works
- Providers are tried in priority order (lowest number first)
- Within each provider, model groups are sorted by tier (best quality first)
- For each model, all keys are tried before downgrading (Model-First)
- Circuit breaker blocks failed key+model combinations with TTL
- Quota manager skips exhausted candidates without making HTTP requests
- Hooks can filter candidates based on custom business logic
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_rotator-0.3.0.tar.gz.
File metadata
- Download URL: llm_rotator-0.3.0.tar.gz
- Upload date:
- Size: 167.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b28e48b210cce777be72e860fc46e247f4be5eb319436ab2ec0367e86f25acd0
|
|
| MD5 |
b85e84e0df8ad3682da53904d2c83350
|
|
| BLAKE2b-256 |
8733adf29c56e17da76edf4a7cc92ce90569a6b401d596e2280499b2e8c64acc
|
Provenance
The following attestation bundles were made for llm_rotator-0.3.0.tar.gz:
Publisher:
publish.yml on ivanHomeless/llm-rotator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_rotator-0.3.0.tar.gz -
Subject digest:
b28e48b210cce777be72e860fc46e247f4be5eb319436ab2ec0367e86f25acd0 - Sigstore transparency entry: 1004746969
- Sigstore integration time:
-
Permalink:
ivanHomeless/llm-rotator@b0d489687562ce311df5a3b1efac09f0f4b11078 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/ivanHomeless
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b0d489687562ce311df5a3b1efac09f0f4b11078 -
Trigger Event:
push
-
Statement type:
File details
Details for the file llm_rotator-0.3.0-py3-none-any.whl.
File metadata
- Download URL: llm_rotator-0.3.0-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e66659e336a214042072b5b8f6b3f325239b401760a4e42c33b09acadf17ee8b
|
|
| MD5 |
02c50921f371fc380e8e336928ce620b
|
|
| BLAKE2b-256 |
3face5218f8353d1cb0bfb1efa442478256e1b1a77813e4ac9e30ab30d695a2a
|
Provenance
The following attestation bundles were made for llm_rotator-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on ivanHomeless/llm-rotator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_rotator-0.3.0-py3-none-any.whl -
Subject digest:
e66659e336a214042072b5b8f6b3f325239b401760a4e42c33b09acadf17ee8b - Sigstore transparency entry: 1004746971
- Sigstore integration time:
-
Permalink:
ivanHomeless/llm-rotator@b0d489687562ce311df5a3b1efac09f0f4b11078 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/ivanHomeless
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b0d489687562ce311df5a3b1efac09f0f4b11078 -
Trigger Event:
push
-
Statement type: