model-router
Model Router — 成本感知的多 LLM 路由与免费优先回退链
自动将每个任务路由到最佳模型——免费提供商优先,付费提供商作为回退。
🎯 为什么选择 Model Router?
大多数 LLM 集成代码硬编码单个模型或使用单个 API 提供商。这意味着:
❌ 昂贵:每个请求(即使是琐碎的请求)都会发送到付费前沿模型
❌ 脆弱:一个提供商停机 = 完全失败
❌ 不灵活:无法根据任务难度匹配模型能力
Model Router 通过一个简单、依赖轻量级的设计解决了这三个问题:
✅ 任务难度分类:通过关键字 + 内容长度启发式规则划分 5 个级别(
vision/long/complex/medium/simple)(零成本,不涉及 LLM)✅ 免费优先回退链:每个路由级别定义一个有序候选链 — 优先尝试免费提供商(Zhipu、SiliconFlow、OpenRouter 免费模型);任何失败时,自动尝试下一个候选(最终为付费的 DeepSeek/Qwen/GLM/Kimi)
✅ 零第三方依赖:核心库只需要
requests;MCP 服务器是纯标准库(通过 stdio 的 JSON-RPC 2.0)✅ 多接口:Python API · CLI · MCP 服务器(任何 MCP 客户端:Claude、Qoder、Cursor…)· 文件监视守护进程
🏗 架构
┌─────────────────────────────────────────────┐
│ 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)路由级别
级别 | 触发条件 | 典型模型 |
| 图像/截图/OCR | Qwen-VL, GLM-4V |
| 内容 > 2000 字符、完整文档 | 128K 上下文模型 |
| 分析/代码/数据/统计/推理 | DeepSeek, Qwen3-32B |
| 写作/翻译/润色 | GLM, Qwen |
| 日常聊天 / 快速查询 | 小型免费模型 |
回退语义:候选按顺序尝试;响应会报告 tier(免费/付费)、attempts、fallback_used 以及每个提供商的 errors,以实现完整可观测性。
🚀 快速开始
# 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 服务器(适用于任何 MCP 客户端)
python mcp_server.py工具 | 描述 |
| 自动路由 + 调用(免费优先,失败时回退) |
| 分析难度 + 返回候选链(零成本) |
| 列出所有已配置的提供商、模型和链 |
在你的 MCP 客户端中注册(例如 Claude Desktop claude_desktop_config.json):
{
"mcpServers": {
"model-router": {
"command": "python",
"args": ["/path/to/model-router/mcp_server.py"],
"env": {"PYTHONIOENCODING": "utf-8"}
}
}
}⏱ 任务触发守护进程(文件监视模式)
# 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🔧 配置
config.json 结构(参见 config.example.json):
providers:OpenAI 兼容端点,带有
tier(free / paid)和可选的enabled: falserouting:每个级别的有序
candidates链 — 免费优先,付费作为安全网levels:每个级别的人类可读描述
你可以自由添加或移除提供商 — 路由器完全由数据驱动。
📄 许可证
MIT — 免费供个人和商业使用。参见 LICENSE。
受真实世界成本优化启发:~90% 的日常任务可由免费层级模型处理,而困难任务仍可通过自动回退获得前沿模型质量。
This server cannot be installed
Maintenance
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.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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