Skip to main content
Glama

Model Router โ€” Cost-Aware Multi-LLM Routing with Free-First Fallback Chain

Route every task to the best model automatically โ€” free providers first, paid providers as fallback.

License: MIT Python 3.8+ Tests CI

๐ŸŽฏ Why Model Router?

Most LLM integration code hard-codes a single model or uses a single API provider. This means:

  • โŒ Expensive: every request (even trivial ones) goes to a paid frontier model

  • โŒ Fragile: one provider outage = total failure

  • โŒ Inflexible: no way to match model capability to task difficulty

Model Router solves all three with a simple, dependency-light design:

  • โœ… Task Difficulty Classification: 5 levels (vision / long / complex / medium / simple) via keyword + content-length heuristics (zero cost, no LLM involved)

  • โœ… Free-First Fallback Chain: each route level defines an ordered candidate chain โ€” free providers (Zhipu, SiliconFlow, OpenRouter free models) are tried first; on any failure, the next candidate (eventually paid DeepSeek/Qwen/GLM/Kimi) is tried automatically

  • โœ… Zero Third-Party Dependencies: core library only needs requests; the MCP server is pure stdlib (JSON-RPC 2.0 over stdio)

  • โœ… Multiple Interfaces: Python API ยท CLI ยท MCP server (any MCP client: Claude, Qoder, Cursorโ€ฆ) ยท File-watch daemon

๐Ÿ— Architecture

                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚              router_core.py                 โ”‚
                โ”‚                                             โ”‚
   task_desc โ”€โ”€โ–ถโ”‚  classify_task()  โ†’  5 difficulty levels   โ”‚
   content   โ”€โ”€โ–ถโ”‚  _get_candidates() โ†’ ordered provider chainโ”‚
   image โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚  route_and_call()  โ†’ free-first, fallback  โ”‚
                โ”‚                                             โ”‚
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ–ผ                โ–ผ                   โ–ผ
        auto_router.py    mcp_server.py       Python API
        (CLI + daemon)    (MCP tools)         (import router_core)

Routing Levels

Level

Trigger

Typical Models

vision

image/screenshot/OCR

Qwen-VL, GLM-4V

long

content > 2000 chars, full documents

128K-context models

complex

analysis/code/data/stats/reasoning

DeepSeek, Qwen3-32B

medium

writing/translation/polish

GLM, Qwen

simple

daily chat / quick queries

small free models

Fallback semantics: candidates are tried in order; the response reports tier (free/paid), attempts, fallback_used, and per-provider errors for full observability.

๐Ÿš€ Quick Start

# 1. Install (only requests is required)
pip install requests

# 2. Configure
cp config.example.json config.json
#    โ†’ fill in your API keys

# 3. CLI โ€” analyze only (zero cost, no model call)
python router_core.py analyze "ๅˆ†ๆž่ฟ™ไปฝๆฐ”่ฑกๆ•ฐๆฎ"

# 4. CLI โ€” route and call
python router_core.py call "็ฟป่ฏ‘ไปฅไธ‹ๆฎต่ฝ" --content "Hello world" --system "ไฝ ๆ˜ฏไธ“ไธš็ฟป่ฏ‘"

# 5. Python API
from router_core import route_and_call, analyze_task
result = analyze_task("ๅ†™ไธ€ไปฝ่ฎบๆ–‡ๆ‘˜่ฆ", content_len=300)
print(result["primary"])           # first candidate
print(result["candidates"])        # full fallback chain
text = route_and_call("ๆ€ป็ป“่ฆ็‚น", "long text...")["content"]

๐Ÿ–ฅ MCP Server (for any MCP client)

python mcp_server.py

Tool

Description

smart_call

Auto-route + call (free-first, fallback on failure)

route_analyze

Analyze difficulty + return candidate chain (zero cost)

list_models

List all configured providers, models and chains

Register in your MCP client (e.g. Claude Desktop claude_desktop_config.json):

{
  "mcpServers": {
    "model-router": {
      "command": "python",
      "args": ["/path/to/model-router/mcp_server.py"],
      "env": {"PYTHONIOENCODING": "utf-8"}
    }
  }
}

โฑ Task-Triggered Daemon (file-watch mode)

# One-shot
python auto_router.py "ไปปๅŠกๆ่ฟฐ" -c "ๅ†…ๅฎน" --image photo.png

# Daemon: drop task files into inbox/, results appear in outbox/
python auto_router.py --watch --dir ./tasks

๐Ÿ”ง Configuration

config.json structure (see config.example.json):

  • providers: OpenAI-compatible endpoints with tier (free / paid) and optional enabled: false

  • routing: per-level ordered candidates chains โ€” free first, paid as safety net

  • levels: human-readable descriptions per level

Add or remove providers freely โ€” the router is fully data-driven.

๐Ÿ“„ License

MIT โ€” free for personal and commercial use. See LICENSE.


Inspired by real-world cost optimization: ~90% of everyday tasks can be served by free tier models, while hard tasks still get frontier-model quality through automatic fallback.

-
license - not tested
-
quality - not tested
B
maintenance

Maintenance

โ€“Maintainers
โ€“Response time
โ€“Release cycle
โ€“Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

  • Hosted MCP server for LLM cost estimation, model comparison, and budget-aware routing.

  • Agent Cost Allocator MCP โ€” multi-tenant LLM cost attribution for chargeback billing. Companion to

  • MCP server providing access to the Scorecard API to evaluate and optimize LLM systems.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yaowanxiang/model-router'

If you have feedback or need assistance with the MCP directory API, please join our Discord server