Smart LLM request router — route prompts to the right model based on cost, speed, quality or complexity
Project description
routerllm
Smart LLM request router — automatically route prompts to the right model based on cost, speed, quality or complexity.
pip install routerllm
The Problem
You have access to multiple LLMs — cheap ones, fast ones, powerful ones. But you use the same model for everything. Simple questions go to GPT-4o (overkill, expensive). Complex analysis goes to GPT-4o-mini (not enough, bad output).
routerllm fixes this automatically.
Quick Start
from routerllm import Router
router = Router(strategy="complexity")
router.add("openai", "gpt-4o-mini", api_key="sk-...") # cheap, fast
router.add("openai", "gpt-4o", api_key="sk-...") # powerful, expensive
# Simple → gpt-4o-mini
result = router.complete("What is 2+2?")
print(result.model) # "gpt-4o-mini"
print(result.cost_usd) # ~$0.000002
# Complex → gpt-4o
result = router.complete(
"Analyze the long-term macroeconomic impact of AI on developing nations, "
"comparing Keynesian and neoclassical perspectives."
)
print(result.model) # "gpt-4o"
print(result.routing.reason) # "Complex prompt (score: 0.78) → most powerful model"
Strategies
| Strategy | Description |
|---|---|
complexity |
Analyze prompt complexity → route accordingly (default) |
cost |
Always use cheapest model |
quality |
Always use most powerful model |
speed |
Always use fastest model |
# Set default strategy
router = Router(strategy="cost")
# Override per call
result = router.complete("Explain quantum entanglement", strategy="quality")
Dry Run (no API call)
decision = router.dry_run("What is machine learning?")
print(decision)
# {
# "would_use": "openai/gpt-4o-mini",
# "strategy": "complexity",
# "reason": "Simple prompt (score: 0.28) → cheapest model",
# "complexity_score": 0.28,
# "estimated_cost_per_1k": 0.00075
# }
Multi-Provider
router = Router(strategy="complexity")
router.add("openai", "gpt-4o-mini", api_key="sk-...")
router.add("openai", "gpt-4o", api_key="sk-...")
router.add("anthropic", "claude-haiku-4", api_key="sk-ant-...")
router.add("anthropic", "claude-opus-4", api_key="sk-ant-...")
router.add("gemini", "gemini-2.5-flash", api_key="AIza...")
RouterResult
result.output # LLM response text
result.model # Model that was used
result.provider # Provider that was used
result.cost_usd # Cost in USD
result.tokens_in # Input tokens
result.tokens_out # Output tokens
result.latency_ms # Response time in ms
result.success # True if no error
result.routing.reason # Why this model was chosen
result.routing.complexity_score # 0.0-1.0
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 routerllm-0.1.0.tar.gz.
File metadata
- Download URL: routerllm-0.1.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc6e7ca780ca12e90bc04d0fc20038a57df59458d435d692290777fca4457698
|
|
| MD5 |
c23188b23259529da7b6329fd383e3f6
|
|
| BLAKE2b-256 |
78b195053b387b52fa98ac6ba23690efed17071bbb054315d3022892b8ed6031
|
File details
Details for the file routerllm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: routerllm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed1a9c5a873b49796edfcfa42b6ae01588890673d9879338e2551460c4d18c1d
|
|
| MD5 |
2738176888db03abc787d59f9430ae2d
|
|
| BLAKE2b-256 |
fb2be092d550310d9c591812e17a97e933e952e114f4bce9d0c92f3a3b8428aa
|