MCP Hub
Back to servers

ypollak2/llm-router

Routes your AI tasks to the best available model across 20+ providers — automatically selecting based on task type, budget, and subscription pressure. Supports text, image, video, and audio with built-in cost optimization and fallback chains.

glama
Stars
4
Updated
Apr 4, 2026
Validated
Apr 6, 2026

LLM Router

LLM Router

One MCP server. Every AI model. Smart routing.

Route text, image, video, and audio tasks to 20+ AI providers — automatically picking the best model for the job based on your budget and active profile.

Quick StartHow It WorksProvidersToolsConfigurationProvider Setup

Tests MIT License Python MCP Providers PyPI


The Problem

You use Claude Code. You also have GPT-4o, Gemini, Perplexity, DALL-E, Runway, ElevenLabs — but switching between them is manual, slow, and expensive.

LLM Router gives your AI assistant one unified interface to all of them — and automatically picks the right one based on what you're doing and what you can afford.

You:     "Research the latest AI funding rounds"
Router:  → Perplexity Sonar Pro (search-augmented, best for current facts)

You:     "Generate a hero image for the landing page"
Router:  → Flux Pro via fal.ai (best quality/cost for images)

You:     "Write unit tests for the auth module"
Router:  → Claude Sonnet (top coding model, within budget)

You:     "Create a 5-second product demo clip"
Router:  → Kling 2.0 via fal.ai (best value for short video)

How It Saves You Money

Not every task needs the same model. Without a router, everything goes to the same expensive model — like hiring a surgeon to change a lightbulb.

"What does os.path.join do?"     → Gemini Flash    ($0.000001 — literally free)
"Refactor the auth module"       → Claude Sonnet   ($0.003)
"Design the full system arch"    → Claude Opus     ($0.015)
Task typeWithout RouterWith RouterSavings
Simple queries (60% of work)Opus — $0.015Haiku/Gemini Flash — $0.000199%
Moderate tasks (30% of work)Opus — $0.015Sonnet — $0.00380%
Complex tasks (10% of work)Opus — $0.015Opus — $0.0150%
Blended monthly estimate~$50/mo~$8–15/mo70–85%

💡 With Ollama: simple tasks route to a free local model — those 60% of queries cost $0.


Quick Start

Option A: PyPI (Recommended)

pip install claude-code-llm-router

Option B: Claude Code Plugin

claude plugin add ypollak2/llm-router

Option C: Manual Install

git clone https://github.com/ypollak2/llm-router.git
cd llm-router
uv sync

Enable Global Auto-Routing

Make the router evaluate every prompt across all projects:

# From the MCP tool:
llm_setup(action='install_hooks')

# Or from the CLI:
llm-router install

This installs hooks + rules to ~/.claude/ so every Claude Code session auto-routes tasks to the optimal model.

Start for free: Google's Gemini API has a free tier with 1M tokens/day. Groq also offers a generous free tier with ultra-fast inference.

What You Get

  • 30 MCP tools — smart routing, text/code, image/video/audio, streaming, orchestration, usage monitoring, web dashboard
  • Auto-route hook — intercepts every prompt before your top-tier model sees it; heuristic → Ollama → cheap API classifier chain, hooks self-update on pip upgrade
  • Claude subscription mode — routes entirely within your CC subscription; Codex (free) before paid externals; external only when quota exhausted
  • Anthropic prompt caching — auto-injects cache_control breakpoints on long system prompts; up to 90% savings on repeated context
  • Semantic dedup cache — Ollama embeddings + cosine similarity skip identical-intent calls at zero cost
  • Web dashboardllm-router dashboardlocalhost:7337; cost trends, model distribution, recent decisions
  • Hard spend capsLLM_ROUTER_DAILY_SPEND_LIMIT and LLM_ROUTER_MONTHLY_BUDGET raise before any call
  • Prompt classification cache — SHA-256 LRU cache for instant repeat classifications
  • Circuit breaker + health — catches 429s, marks unhealthy providers, auto-recovers
  • Quality logging — records every routing decision; llm_quality_report shows accuracy, savings, downshift rate
  • Cross-platform — macOS, Linux, Windows (desktop notifications, background processes, path handling)

Dashboard

The built-in web dashboard (llm_dashboard or llm-router dashboard) gives you a live view of routing decisions, cost trends, and subscription pressure.

OverviewPerformance
OverviewPerformance
Logs & Analysis
Logs

Design: Liquid Glass dark theme — Inter + JetBrains Mono, Material Symbols, Tailwind CSS. Auto-refreshes every 30 s.


How It Works

Auto-Route Hook — Every Prompt, Cheaper Model First

The UserPromptSubmit hook intercepts all prompts before your top-tier model sees them.

PromptClassified asModel used
why doesn't the router work?analyze/moderateHaiku
how does benchmarks.py work?query/simpleOllama / Haiku
fix the bug in profiles.pycode/moderateHaiku / Sonnet
implement a distributed cachecode/complexSonnet / Opus
write a blog post about LLMsgenerate/moderateHaiku / Gemini Flash
git status (raw shell command)(skipped — terminal op)

Classification chain (stops at first success):

1. Heuristic scoring    instant, free   → high-confidence patterns route immediately
2. Ollama local LLM     free, ~1s       → catches what heuristics miss
3. Cheap API            ~$0.0001        → Gemini Flash / GPT-4o-mini fallback
4. Query catch-all      instant, free   → any remaining question → Haiku

Hook scripts are versioned and self-update — existing users get improvements automatically after pip install --upgrade.

Claude Code Subscription Mode

If you use Claude Code Pro/Max, you already pay for Haiku, Sonnet, and Opus. Enable subscription mode and the router routes within your subscription first — Codex (free via OpenAI subscription) before any paid API call, external only when quota is exhausted.

# In .env
LLM_ROUTER_CLAUDE_SUBSCRIPTION=true

Default Routing (No Pressure)

ComplexityModelCost
simpleClaude Haiku 4.5free (subscription)
moderateSonnet (passthrough)free (you're already using it)
complexClaude Opus 4.6free (subscription)
researchPerplexity Sonar Pro~$0.005/query

Pressure Cascade

Conditionsimplemoderatecomplex
session < 95%, sonnet < 95%Haiku (sub)Sonnet (sub)Opus (sub)
sonnet ≥ 95%Codex → externalCodex → externalOpus (sub)
weekly ≥ 95% or session ≥ 95%Codex → externalCodex → externalCodex → external

Run llm_check_usage at session start to populate accurate pressure data. Hooks flag ⚠️ STALE when usage data is >30 minutes old.

External Fallback Chains (free-first)

TierChain
BUDGET (simple)Ollama → Codex/gpt-5.4 → Codex/o3 → Gemini Flash → Groq → GPT-4o-mini
BALANCED (moderate)Ollama → Codex/gpt-5.4 → Codex/o3 → GPT-4o → Gemini Pro → DeepSeek
PREMIUM (complex)Ollama → Codex/gpt-5.4 → Codex/o3 → o3 → Gemini Pro

Live subscription status:

+----------------------------------------------------------+
|                Claude Subscription (Live)                |
+----------------------------------------------------------+
|   Session      [====........]  35%  resets in 3h 7m      |
|   Weekly (all) [===.........]  23%  resets Fri 01:00 PM  |
|   Sonnet only  [===.........]  26%  resets Wed 10:00 AM  |
+----------------------------------------------------------+
|   OK 35% pressure -- full model selection                |
+----------------------------------------------------------+

Providers

ProviderModelsFree TierBest For
🦙 OllamaAny local modelYes (free forever)Privacy, zero cost, offline
Google Gemini2.5 Pro, 2.5 FlashYes (1M tokens/day)Generation, long context
GroqLlama 3.3, MixtralYesUltra-fast inference
OpenAIGPT-4o, GPT-4o-mini, o3NoCode, analysis, reasoning
PerplexitySonar, Sonar ProNoResearch, current events
AnthropicClaude Sonnet, HaikuNoNuanced writing, safety
DeepseekV3, ReasonerYes (limited)Cost-effective reasoning
MistralLarge, SmallYes (limited)Multilingual
TogetherLlama 3, CodeLlamaYes (limited)Open-source models
xAIGrok 3NoReal-time information
CohereCommand R+Yes (trial)RAG, enterprise search

Image, video, and audio providers (fal.ai, Runway, Stability AI, ElevenLabs, etc.) — see docs/PROVIDERS.md for full setup guides.

🦙 Ollama runs models locally — no API key, no cost, no data sent externally. Setup guide →


MCP Tools

Once installed, Claude Code gets these 29 tools:

ToolWhat It Does
Smart Routing
llm_classifyClassify complexity + recommend model with time-aware budget pressure
llm_routeAuto-classify, then route to the best external LLM
llm_track_usageReport Claude Code token usage for budget tracking
llm_streamStream LLM responses for long-running tasks
Text & Code
llm_queryGeneral questions — auto-routed to the best text LLM
llm_researchSearch-augmented answers via Perplexity
llm_generateCreative content — writing, summaries, brainstorming
llm_analyzeDeep reasoning — analysis, debugging, problem decomposition
llm_codeCoding tasks — generation, refactoring, algorithms
llm_editRoute code-edit reasoning to a cheap model → returns exact {file, old_string, new_string} pairs
Media
llm_imageImage generation — Gemini Imagen, DALL-E, Flux, or SD
llm_videoVideo generation — Gemini Veo, Runway, Kling, etc.
llm_audioVoice/audio — TTS via ElevenLabs or OpenAI
Orchestration
llm_orchestrateMulti-step pipelines across multiple models
llm_pipeline_templatesList available orchestration templates
Monitoring & Setup
llm_check_usageCheck live Claude subscription usage (session %, weekly %)
llm_update_usageFeed live usage data from claude.ai into the router
llm_refresh_claude_usageForce-refresh Claude subscription data via OAuth
llm_codexRoute tasks to local Codex desktop agent (free, uses OpenAI sub)
llm_setupDiscover API keys, add providers, validate keys, install global hooks
llm_rateRate last response (👍/👎) — stored in routing_decisions for quality tracking
llm_quality_reportRouting accuracy, classifier stats, savings metrics, downshift rate
llm_set_profileSwitch routing profile (budget / balanced / premium)
llm_usageUnified dashboard — Claude sub, Codex, APIs, savings in one view
llm_healthCheck provider availability and circuit breaker status
llm_providersList all supported and configured providers
llm_cache_statsView cache hit rate, entries, memory estimate, evictions
llm_cache_clearClear the classification cache
Session Memory
llm_save_sessionSummarize + persist current session for cross-session context injection

Context injection: text tools (llm_query, llm_research, llm_generate, llm_analyze, llm_code) automatically prepend recent conversation history to every external call — GPT-4o, Gemini, and Perplexity receive the same context you have. Controlled by LLM_ROUTER_CONTEXT_ENABLED (default: on).


Routing Profiles

Three built-in profiles map to task complexity. Switch anytime:

llm_set_profile("budget")    # Development, drafts, exploration
llm_set_profile("balanced")  # Production work, client deliverables
llm_set_profile("premium")   # Critical tasks, maximum quality
Budget (simple)Balanced (moderate)Premium (complex)
TextOllama → Haiku → Gemini FlashSonnet → GPT-4o → DeepSeekOpus → Sonnet → o3
CodeOllama → Codex → DeepSeek → HaikuCodex → Sonnet → GPT-4oCodex → Opus → o3
ResearchPerplexity SonarPerplexity Sonar ProPerplexity Sonar Pro
ImageFlux Dev, Imagen FastFlux Pro, Imagen 3, DALL-E 3Imagen 3, DALL-E 3
Videominimax, Veo 2Kling, Veo 2, Runway TurboVeo 2, Runway Gen-3
AudioOpenAI TTSElevenLabsElevenLabs

Model order is pressure-aware — as Claude quota is consumed, chains reorder to preserve remaining budget. See BENCHMARKS.md for how model quality scores drive rankings.


Budget Control

# In .env
LLM_ROUTER_MONTHLY_BUDGET=50   # USD, 0 = unlimited

The router tracks real-time spend across all providers in SQLite and blocks requests when the monthly budget is reached.

llm_usage("month")
→ Calls: 142 | Tokens: 320,000 | Cost: $3.42 | Budget: 6.8% of $50

Per-provider budgets: LLM_ROUTER_BUDGET_OPENAI=10.00, LLM_ROUTER_BUDGET_GEMINI=5.00.


Multi-Step Orchestration

Chain tasks across models in a pipeline:

llm_orchestrate("Research AI trends and write a report", template="research_report")
TemplatePipeline
research_reportResearch → Analyze → Write
competitive_analysisMulti-source research → SWOT → Report
content_pipelineResearch → Draft → Review → Polish
code_review_fixReview → Fix → Test

Configuration

# Required: at least one provider
GEMINI_API_KEY=AIza...         # Free tier! https://aistudio.google.com/apikey
OPENAI_API_KEY=sk-proj-...
PERPLEXITY_API_KEY=pplx-...

# Optional: more providers
ANTHROPIC_API_KEY=sk-ant-...
DEEPSEEK_API_KEY=...
GROQ_API_KEY=gsk_...
FAL_KEY=...
ELEVENLABS_API_KEY=...

# Router config
LLM_ROUTER_PROFILE=balanced               # budget | balanced | premium
LLM_ROUTER_MONTHLY_BUDGET=0              # USD, 0 = unlimited
LLM_ROUTER_CLAUDE_SUBSCRIPTION=false     # true = Claude Code Pro/Max user

# Ollama (two independent roles — classifier and task answerer)
LLM_ROUTER_OLLAMA_URL=http://localhost:11434    # hook classifier
LLM_ROUTER_OLLAMA_MODEL=qwen3.5:latest
OLLAMA_BASE_URL=http://localhost:11434          # router answerer
OLLAMA_BUDGET_MODELS=llama3.2,qwen2.5-coder:7b

# Smart routing (Claude Code model selection)
QUALITY_MODE=balanced          # best | balanced | conserve
MIN_MODEL=haiku                # floor: haiku | sonnet | opus

See .env.example for the full list.

Ollama note: LLM_ROUTER_OLLAMA_URL is for the hook classifier (classifying complexity); OLLAMA_BASE_URL is for the router answerer (actually answering tasks). Configuring one does not enable the other. See docs/PROVIDERS.md for the full local-first setup.


Development

uv sync --extra dev
uv run pytest tests/ -q --ignore=tests/test_integration.py
uv run ruff check src/ tests/
llm-router install   # deploy hooks to ~/.claude/

See CLAUDE.md for architecture, module layout, and contribution guidelines.


Roadmap

See CHANGELOG.md for what's been shipped. Coming next:

VersionThemeHeadline features
v1.3Observability✅ Web dashboard, prompt caching, semantic dedup cache, hard daily cap, cross-platform notifications
v1.4Routing IntelligenceTask-aware model preferences, reasoning model tier, learned routing
v1.5Agentic & TeamAgent-tree budget tracking, multi-user profiles, YAML pipelines
v2.0Learning RouterSelf-improving classifier trained on your own routing history

See ROADMAP.md for design notes and competitive context.


Contributing

See CONTRIBUTING.md. Key areas: new provider integrations, routing intelligence, MCP client testing, documentation.


License

MIT — use it however you want.


Built with LiteLLM and MCP

Reviews

No reviews yet

Sign in to write a review