Skip to main content

Unified LLM routing with automatic fallback, cost-aware model selection, and semantic caching.

Project description

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 their priority value. Great for setting up a primary model and a reliable fallback.
  • cheapest: Ignores priority and always attempts the provider with the lowest cost_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

routelayer-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

routelayer-0.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file routelayer-0.1.0.tar.gz.

File metadata

  • Download URL: routelayer-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for routelayer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 794dec5d88206b0a2657d2f503421f8371deef1a99c68b89cd0067b942a03892
MD5 761ede14a879e4e1b0cbe4fa6b8021ed
BLAKE2b-256 9087b3382866fb0c0ef71a032a737b1b94000bacf8daa311a9a67d1135f2a76f

See more details on using hashes here.

Provenance

The following attestation bundles were made for routelayer-0.1.0.tar.gz:

Publisher: publish.yml on carsonlabs/routelayer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file routelayer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: routelayer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for routelayer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8474b370ba8255a56914a1289b77b01895bb8ed29cca8a45e267d1bf2fd43fb4
MD5 3c6f13f82969faf8d15404f4ca2912ef
BLAKE2b-256 14df6fb7c63164b8c9830359a2406352c28db266deddeb68799c2d0009cc48e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for routelayer-0.1.0-py3-none-any.whl:

Publisher: publish.yml on carsonlabs/routelayer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page