RouteLayer (Python)
Unified LLM routing with automatic fallback, cost-aware model selection, and semantic caching.
RouteLayer is a lightweight, zero-dependency (other than the provider SDKs) middleware that sits between your application and your LLM providers. It solves the "Inference Cost Crisis" by intelligently routing requests based on priority, cost, and semantic similarity.
Why RouteLayer?
LiteLLM is the 800-pound gorilla in this space, but it requires running a proxy server and comes with a mountain of configuration. RouteLayer takes a different approach:
- Zero config -- add your providers and call
generate(). No proxy, no YAML files, no Docker containers. - Simpler mental model -- RouteLayer is a library, not a platform. Import it, use it, done.
- Built-in semantic caching -- LiteLLM charges extra for caching via their proxy. RouteLayer includes it for free, in-process, with zero dependencies.
If you need 100+ provider adapters and team management, use LiteLLM. If you need a lightweight router that just works, use RouteLayer.
Features
- Unified API: Call OpenAI, Anthropic, and Gemini through a single, standardized interface.
- Zero-Downtime Fallback: Automatically failover to a secondary provider if the primary provider times out or goes down.
- Cost-Aware Routing: Automatically select the cheapest available model for a given request.
- Built-in Semantic Caching: Stop paying for the same answer twice. RouteLayer includes a lightweight, local, zero-dependency semantic cache that returns cached responses for semantically similar prompts.
Installation
Install RouteLayer with the providers you intend to use:
# Install with all providers
pip install "routelayer[all]"
# Or install specific providers
pip install "routelayer[openai,anthropic]"
Quick Start
import os
from routelayer import RouteLayer
# 1. Initialize the router with your preferred models
rl = RouteLayer(
providers=[
{
"name": "openai",
"model": "gpt-4o-mini",
"api_key": os.environ.get("OPENAI_API_KEY"),
"priority": 0, # Try first
"cost_per_1k_tokens": 0.0006,
},
{
"name": "anthropic",
"model": "claude-haiku-4-5-20251001",
"api_key": os.environ.get("ANTHROPIC_API_KEY"),
"priority": 1, # Fallback
"cost_per_1k_tokens": 0.001,
}
],
strategy="priority", # Or "cheapest"
verbose=True
)
# 2. Generate a response
response = rl.generate("Explain quantum computing in one sentence.")
# 3. Inspect the standardized response
print(response.text)
print(f"Served by: {response.provider} ({response.model})")
print(f"Cost: ${response.cost_usd:.6f}")
print(f"Cached: {response.cached}")
Routing Strategies
priority(Default): Tries providers in ascending order of theirpriorityvalue. Great for setting up a primary model and a reliable fallback.cheapest: Ignores priority and always attempts the provider with the lowestcost_per_1k_tokens.
Semantic Caching
RouteLayer includes a built-in semantic cache. If a user asks "What is your refund policy?" and later asks "How do I get a refund?", the cache will intercept the second request and return the cached answer without hitting the LLM API, saving you money and reducing latency to zero.
from routelayer import RouteLayer, SemanticCache
# Configure the cache (defaults are usually fine)
cache = SemanticCache(
threshold=0.92, # Similarity threshold (0.0 to 1.0)
ttl_seconds=3600, # Time to live (1 hour)
max_size=1000 # Max entries in memory
)
rl = RouteLayer(providers=[...], cache=cache)
License
MIT License. Built by Freedom Engineers -- routelayer.io
Metadata
Release files for routelayer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| routelayer-0.1.0.tar.gz | 13.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| routelayer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.1 kB
Release files / routelayer-0.1.0.tar.gz
| Download URL | routelayer-0.1.0.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
794dec5d88206b0a2657d2f503421f8371deef1a99c68b89cd0067b942a03892
|
|
BLAKE2b-256 checksum How to use checksums |
9087b3382866fb0c0ef71a032a737b1b94000bacf8daa311a9a67d1135f2a76f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 26, 2026.
Transparency logRelease files / routelayer-0.1.0-py3-none-any.whl
| Download URL | routelayer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8474b370ba8255a56914a1289b77b01895bb8ed29cca8a45e267d1bf2fd43fb4
|
|
BLAKE2b-256 checksum How to use checksums |
14df6fb7c63164b8c9830359a2406352c28db266deddeb68799c2d0009cc48e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 26, 2026.
Transparency log