LLMRouter ๐
Intelligent routing, load balancing, and failover for LLM providers
LLMRouter is a Python package that acts as a routing layer between your application and LLM providers (OpenAI, Groq, Together AI, etc.). It handles provider selection, API key rotation, automatic failover, retries, and metricsโso you don't have to.
# Instead of managing providers manually:
client = OpenAI(api_key="sk-...")
response = client.chat.completions.create(model="gpt-4", messages=[...])
# Use LLMRouter for intelligent routing:
router = LLMRouter(providers=[openai_provider, groq_provider])
response = await router.chat(prompt="Hello!")
# Router automatically picks the best provider, rotates keys, retries on failure, tracks metrics
โจ Features
Core Capabilities (v0.1+)
- ๐ Automatic Provider Failover โ Switch to backup provider if primary fails
- ๐ API Key Pooling โ Rotate across multiple keys to avoid rate limits
- โฑ๏ธ Intelligent Scheduling โ Pick the best client (least busy, round-robin, random, weighted, priority)
- ๐ Automatic Retries โ Exponential backoff with jitter for transient failures
- ๐ Metrics Collection โ Track requests, errors, latency, throughput, success rates
- ๐ Extensible Design โ Custom schedulers, retry policies, middleware
- ๐ Health Checks โ Monitor provider availability in real-time
- ๐ก Streaming Support โ Stream responses from any provider
- ๐ก๏ธ Error Handling โ Graceful degradation with meaningful exceptions
Planned Features (v0.2+)
- ๐ฐ Cost-aware routing (pick cheapest provider)
- โก Latency-aware routing (pick fastest provider)
- ๐ Region-aware routing
- ๐พ Response caching
- ๐ Authentication gateway
- ๐ Prometheus metrics export
๐ฆ Installation
From PyPI
pip install llmrouterx
From Source (Development)
git clone https://github.com/amar8737/LLMRouter.git
cd LLMRouter
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -e .
Requirements
- Python 3.10+ (tested on 3.10, 3.11, 3.12)
- No external LLM SDKs required (bring your own: OpenAI, Groq, Together AI, etc.)
๐ Quick Start (5 Minutes)
1. Install the package
pip install llmrouterx
2. Basic example with OpenAI
import asyncio
from openai import AsyncOpenAI
from llmrouterx import LLMRouter
from llmrouterx.client import ClientNode
from llmrouterx.providers import ProviderRouter, CompositeRouter
async def main():
# Create an OpenAI client
client = AsyncOpenAI(api_key="sk-your-key-here")
# Wrap it in a ClientNode (tracks identity and health)
node = ClientNode("key-1", client)
# Create a provider (represents a service like OpenAI)
provider = ProviderRouter("openai", [node])
# Create a composite router (aggregates providers)
composite = CompositeRouter([provider])
# Create the router
router = LLMRouter(composite)
# Make a request
response = await router.chat(prompt="Hello, what's 2+2?")
print(response)
if __name__ == "__main__":
asyncio.run(main())
3. Run tests to verify installation
pip install pytest pytest-asyncio
pytest tests/ -v
๐ก Usage Examples
Example 1: Single Provider with One Key
import asyncio
from openai import AsyncOpenAI
from llmrouterx import LLMRouter
from llmrouterx.client import ClientNode
from llmrouterx.providers import ProviderRouter, CompositeRouter
async def main():
client = AsyncOpenAI(api_key="sk-...")
node = ClientNode("sk-...", client)
provider = ProviderRouter("openai", [node])
composite = CompositeRouter([provider])
router = LLMRouter(composite)
response = await router.chat(prompt="Hello!")
print(response)
asyncio.run(main())
Example 2: Multiple API Keys (Rate Limit Protection)
import asyncio
from openai import AsyncOpenAI
from llmrouterx import LLMRouter
from llmrouterx.client import ClientNode
from llmrouterx.providers import ProviderRouter, CompositeRouter
from llmrouterx.scheduler import LeastBusyScheduler
async def main():
keys = ["sk-key1", "sk-key2", "sk-key3"]
nodes = [ClientNode(key, AsyncOpenAI(api_key=key)) for key in keys]
# LeastBusyScheduler rotates across keys, avoiding rate limits
provider = ProviderRouter("openai", nodes, scheduler=LeastBusyScheduler())
composite = CompositeRouter([provider])
router = LLMRouter(composite)
response = await router.chat(prompt="Hello!")
print(response)
asyncio.run(main())
Example 3: Multiple Providers with Automatic Failover
import asyncio
from openai import AsyncOpenAI
from groq import AsyncGroq
from llmrouterx import LLMRouter
from llmrouterx.client import ClientNode
from llmrouterx.providers import ProviderRouter, CompositeRouter
from llmrouterx.scheduler import LeastBusyScheduler
async def main():
# Primary: OpenAI
openai_node = ClientNode("sk-...", AsyncOpenAI(api_key="sk-..."))
openai_provider = ProviderRouter("openai", [openai_node], scheduler=LeastBusyScheduler())
# Fallback: Groq
groq_node = ClientNode("gsk-...", AsyncGroq(api_key="gsk-..."))
groq_provider = ProviderRouter("groq", [groq_node], scheduler=LeastBusyScheduler())
# CompositeRouter tries providers in order; if first fails, tries next
composite = CompositeRouter([openai_provider, groq_provider])
router = LLMRouter(composite)
# If OpenAI fails, automatically falls back to Groq
response = await router.chat(prompt="Hello!")
print(response)
asyncio.run(main())
Example 4: Streaming Responses
import asyncio
from llmrouterx import LLMRouter
async def main():
router = LLMRouter(composite) # from previous examples
# Stream response chunks
async for chunk in router.stream(prompt="Tell me a story"):
print(chunk, end="", flush=True)
print()
asyncio.run(main())
Example 5: Concurrent Requests
import asyncio
from llmrouterx import LLMRouter
async def main():
router = LLMRouter(composite)
# Make 10 concurrent requests
prompts = [f"Request {i}: tell me a fact" for i in range(10)]
responses = await asyncio.gather(*[router.chat(prompt=p) for p in prompts])
print(f"Got {len(responses)} responses")
for i, resp in enumerate(responses):
print(f"{i}: {resp[:100]}...")
asyncio.run(main())
Example 6: Custom Retry Policy
from llmrouterx.retry import ExponentialRetry
from llmrouterx import LLMRouter
# Retry with exponential backoff
retry = ExponentialRetry(
max_retries=5, # Try up to 5 times
base=0.5, # Start with 0.5 second wait
factor=2.0, # Double wait time each retry
max_backoff=30.0, # Cap wait at 30 seconds
)
router = LLMRouter(composite, retry=retry)
Example 7: Monitor Metrics
import asyncio
from llmrouterx import LLMRouter
async def main():
router = LLMRouter(composite)
# Make some requests
for i in range(10):
await router.chat(prompt=f"Request {i}")
# View metrics
metrics = router.metrics.get()
print("Total requests:", metrics["counters"].get("total_requests", 0))
print("Total errors:", metrics["counters"].get("total_errors", 0))
print(
"Average latency:",
sum(metrics["timings"]) / len(metrics["timings"]) if metrics["timings"] else 0,
)
asyncio.run(main())
Example 8: Custom Middleware for Logging
import logging
from llmrouterx.middleware import BaseMiddleware
from llmrouterx import LLMRouter
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class LoggingMiddleware(BaseMiddleware):
async def before_request(self, op, payload):
logger.info(f"โ {op}: {payload}")
return payload
async def after_response(self, op, payload, response):
logger.info(f"โ {op}: response received")
return response
# Use it
router = LLMRouter(composite, middleware=[LoggingMiddleware()])
Example 9: All Available Schedulers
from llmrouterx.scheduler import (
LeastBusyScheduler, # Pick client with fewest active requests
RoundRobinScheduler, # Rotate through clients sequentially
RandomScheduler, # Pick random client
WeightedScheduler, # Pick by node.weight property
PriorityScheduler, # Pick by node.priority property
)
# Use any scheduler
provider = ProviderRouter(
"name",
nodes,
scheduler=LeastBusyScheduler(), # or any scheduler above
)
Example 10: Check Provider Health
async def check_health():
composite = CompositeRouter([provider1, provider2])
for provider in composite.providers:
is_healthy = await provider.is_healthy()
status = "โ Up" if is_healthy else "โ Down"
print(f"{provider.name}: {status}")
Example 11: Error Handling
import asyncio
from llmrouterx import LLMRouter
from llmrouterx.exceptions import NoHealthyClientError
async def main():
router = LLMRouter(composite)
try:
response = await router.chat(prompt="Hello!")
except NoHealthyClientError as e:
print(f"All providers are down: {e}")
except Exception as e:
print(f"Request failed: {e}")
asyncio.run(main())
๐ Core Concepts
ClientNode
Represents a single LLM client (e.g., one OpenAI API key). Tracks health, active requests, and metadata.
node = ClientNode("identifier", client_instance)
node.weight = 2 # Optional: used by WeightedScheduler
node.priority = 10 # Optional: used by PriorityScheduler
ProviderRouter
Aggregates multiple ClientNodes for a single provider (e.g., OpenAI with 3 keys). Selects the best node using a scheduler.
provider = ProviderRouter(
"openai", # Provider name
[node1, node2, node3], # List of nodes
scheduler=LeastBusyScheduler(), # How to select among nodes
)
CompositeRouter
Aggregates multiple ProviderRouters. Tries providers in order; if primary fails, tries next.
composite = CompositeRouter([openai_provider, groq_provider, fallback_provider])
LLMRouter
Main router. Orchestrates composite router, retry logic, middleware, and metrics.
router = LLMRouter(
composite_router,
retry=ExponentialRetry(),
middleware=[LoggingMiddleware()],
)
๐ Metrics
Track performance across your LLM infrastructure:
metrics = router.metrics.get()
# Counters
print(metrics["counters"]["total_requests"])
print(metrics["counters"]["total_errors"])
print(metrics["counters"]["total_successes"])
# Timings (list of request latencies in seconds)
print(metrics["timings"]) # [0.45, 0.52, 0.38, ...]
๐ Extensibility
Custom Scheduler
from llmrouterx.scheduler import BaseScheduler
class MyScheduler(BaseScheduler):
async def select(self, provider_router):
candidates = [c for c in provider_router.clients if await c.is_healthy()]
if not candidates:
return None
# Your logic here
return candidates[0]
provider = ProviderRouter("name", nodes, scheduler=MyScheduler())
Custom Middleware
from llmrouterx.middleware import BaseMiddleware
class MyMiddleware(BaseMiddleware):
async def before_request(self, op, payload):
# Modify request before sending
return payload
async def after_response(self, op, payload, response):
# Transform response after receiving
return response
router = LLMRouter(composite, middleware=[MyMiddleware()])
Custom Retry Policy
from llmrouterx.retry import BaseRetry
class MyRetry(BaseRetry):
async def should_retry(self, error, attempt):
# Your logic to decide if we should retry
return attempt < 3
router = LLMRouter(composite, retry=MyRetry())
๐งช Testing
Run All Tests
pytest tests/ -v
Run Specific Test
pytest tests/test_streaming.py -v
Run Smoke Test
python tests/run_tests.py
Write Your Own Test
import pytest
from llmrouterx import LLMRouter
from llmrouterx.providers import StubClient, ProviderRouter, CompositeRouter
from llmrouterx.client import ClientNode
@pytest.mark.asyncio
async def test_basic_chat():
stub = StubClient("test")
node = ClientNode("test", stub)
provider = ProviderRouter("test", [node])
composite = CompositeRouter([provider])
router = LLMRouter(composite)
response = await router.chat(prompt="Hello")
assert response is not None
๐ง Configuration
Minimal Setup
from llmrouterx import LLMRouter
from llmrouterx.providers import CompositeRouter, ProviderRouter
from llmrouterx.client import ClientNode
node = ClientNode("key", client)
provider = ProviderRouter("openai", [node])
composite = CompositeRouter([provider])
router = LLMRouter(composite)
Full Setup with All Options
from llmrouterx import LLMRouter
from llmrouterx.retry import ExponentialRetry
from llmrouterx.middleware import BaseMiddleware
class LogMiddleware(BaseMiddleware):
async def before_request(self, op, payload):
print(f"Sending: {op}")
return payload
retry = ExponentialRetry(max_retries=5, base=1.0, factor=2.0)
router = LLMRouter(
composite,
retry=retry,
middleware=[LogMiddleware()],
# Additional config options as needed
)
๐ Troubleshooting
| Problem | Solution |
|---|---|
| "No healthy providers" | Check if providers are up, verify API keys are valid |
| Requests are slow | Use LeastBusyScheduler, check metrics for latency outliers |
| Same key always used | Check that scheduler is set to RoundRobin or LeastBusy |
| Errors not retrying | Check should_retry() logic in retry policy, some errors are permanent |
| ModuleNotFoundError: llmrouterx | Run pip install -e . if developing from source |
| Tests hang | Update setuptools>=45 and run pip install -e . again |
๐ API Reference
LLMRouter
class LLMRouter:
def __init__(self, composite, retry=None, middleware=None):
"""Initialize router with composite, retry policy, and middleware."""
async def chat(self, prompt: str, **kwargs) -> str:
"""Send chat request to best available provider."""
async def stream(self, prompt: str, **kwargs):
"""Stream response chunks from best available provider."""
async def embeddings(self, text: str, **kwargs) -> list:
"""Get embeddings from best available provider."""
def get_metrics(self) -> dict:
"""Return metrics (counters and timings)."""
CompositeRouter
class CompositeRouter:
def __init__(self, providers: list):
"""Initialize with list of ProviderRouters."""
async def select(self) -> ProviderRouter:
"""Select best provider, trying in order."""
async def is_healthy(self) -> bool:
"""Check if any provider is healthy."""
ProviderRouter
class ProviderRouter:
def __init__(self, name: str, clients: list, scheduler=None):
"""Initialize provider with name, clients, and scheduler."""
async def select_client(self) -> ClientNode:
"""Select best client using scheduler."""
async def is_healthy(self) -> bool:
"""Check if any client is healthy."""
ClientNode
class ClientNode:
def __init__(self, identifier: str, client):
"""Initialize with identifier and client instance."""
async def is_healthy(self) -> bool:
"""Check if client is healthy."""
def increment_active(self) -> None:
"""Increment active request count."""
def decrement_active(self) -> None:
"""Decrement active request count."""
๐ค Contributing
We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Write tests for your changes
- Run tests:
pytest tests/ -v - Commit with clear messages:
git commit -m "feat: add my feature" - Push to your fork:
git push origin feature/my-feature - Open a PR with a clear description
Development Checklist
- Code follows style guidelines (use
blackorautopep8) - Tests pass (
pytest tests/ -v) - New features include tests
- Documentation is updated
- Commit messages are descriptive
Areas for Contribution
- โ Cost-aware routing
- โ Latency-aware routing
- โ Response caching
- โ Prometheus metrics export
- โ Additional provider support
- โ Performance optimizations
- โ Documentation and examples
๐ License
MIT License โ see LICENSE for details.
๐ Acknowledgments
Built with โค๏ธ for developers managing multiple LLM providers. Special thanks to the open-source community.
๐ Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: babuamar455@gmail.com
๐ Roadmap
v0.1 (Current) โ
- Basic routing and failover
- API key pooling
- Schedulers (round-robin, least-busy, random, weighted, priority)
- Retry logic
- Metrics collection
v0.2 (Planned)
- Cost-aware routing
- Latency-aware routing
- Response caching
- Prometheus metrics export
v0.3 (Future)
- Region-aware routing
- Batch request optimization
- Advanced analytics dashboard
Ready to get started? Check out the Quick Start section or explore the examples above!
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 llmrouterx-0.1.8.tar.gz.
File metadata
- Download URL: llmrouterx-0.1.8.tar.gz
- Upload date:
- Size: 39.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
754588186702dd141bf904113330a6c05fa96fd73931dac7a5b83bd2508f6a66
|
|
| MD5 |
8f58cae07a73f7f90dfc9720feb76605
|
|
| BLAKE2b-256 |
3f2dcbd088beda46b6b0936b6064559a1fe3c22a6f8899e5f64fa3d7b2a324c9
|
File details
Details for the file llmrouterx-0.1.8-py3-none-any.whl.
File metadata
- Download URL: llmrouterx-0.1.8-py3-none-any.whl
- Upload date:
- Size: 45.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a63f9fa42b552a8d5c1680e8d18c480fb0aed400033f3da8fb84302c661733a
|
|
| MD5 |
17e38aad4ad262378adab65e73cf6d84
|
|
| BLAKE2b-256 |
e5a309ef57a2a4f6466d001a902db5b7e5bcee3472c998706be20939fa765d60
|