Semantic Model Router
Smart LLM router that saves up to 99% on inference costs by routing each request to the cheapest model that can handle it. Powered by a pre-trained ML classifier and semantic embeddings — no external calls, no API keys needed.
Install
CODEBLOCK0
Quick Start
CODEBLOCK1
How Routing Works
Queries are classified into three tiers through a 3-stage pipeline:
- 1. ML Classifier (primary): A Logistic Regression model trained on 6,000+ labeled queries. Runs in <1ms from embedded weights in
model_weights.py. - Semantic Embeddings (fallback): Cosine similarity to tier intent vectors via
sentence-transformers. - Keyword Rules (last resort): Pattern matching with no dependencies.
| Tier | Default Model | Typical Workload | Cost/1M | vs Baseline |
|---|
| BASIC | INLINECODE2 | Greetings, simple Q&A, chit-chat | $0.14 | 99% saved |
| BALANCED |
openai/gpt-4o-mini | Summaries, translations, explanations | $0.15 |
99% saved |
|
ELITE |
anthropic/claude-sonnet-4-6 | Complex coding, architecture, security | $3.00 |
70% saved |
Supported Models (17 total, verified Feb 2026)
Anthropic
| Model | Input /1M | Output /1M |
|---|
| INLINECODE5 | $3.00 | $15.00 ★ ELITE default |
| INLINECODE6 |
$5.00 | $25.00 |
|
anthropic/claude-haiku-4-5 | $0.80 | $4.00 |
OpenAI
| Model | Input /1M | Output /1M |
|---|
| INLINECODE8 | $1.25 | $10.00 |
| INLINECODE9 |
$2.50 | $10.00 |
|
openai/gpt-4o-mini | $0.15 | $0.60 ★ BALANCED default |
|
openai/o3 | $2.00 | $8.00 |
|
openai/o4-mini | $1.10 | $4.40 |
Google
| Model | Input /1M | Output /1M |
|---|
| INLINECODE13 | $1.25 | $10.00 |
| INLINECODE14 |
$1.25 | $10.00 |
|
google/gemini-2.5-flash | $0.30 | $2.50 |
|
google/gemini-2.5-flash-lite | $0.10 | $0.40 |
DeepSeek
| Model | Input /1M | Output /1M |
|---|
| INLINECODE17 (V3.2) | $0.28 | $0.42 ★ BASIC default |
| INLINECODE18 (V3.2) |
$0.28 | $0.42 |
xAI (Grok)
| Model | Input /1M | Output /1M |
|---|
| INLINECODE19 | $3.00 | $15.00 |
| INLINECODE20 |
$0.30 | $0.50 |
Pricing source: Official API docs of each provider, verified Feb 2026.
Override Models at Runtime
CODEBLOCK2
CODEBLOCK3
List All Available Models (CLI)
CODEBLOCK4
CLI Usage
CODEBLOCK5
Dynamic Keyword Expansion
CODEBLOCK6
Example Output
CODEBLOCK7
Security & Privacy
- - Zero external calls: All classification runs locally.
- No API keys: The router itself needs none.
- Transparent weights: All model parameters live in
scripts/model_weights.py — fully auditable.
Save costs, route smarter. Built for the OpenClaw community.
语义模型路由器
智能LLM路由器,通过将每个请求路由到能够处理该请求的最便宜模型,可节省高达99%的推理成本。由预训练的机器学习分类器和语义嵌入驱动——无需外部调用,无需API密钥。
安装
bash
openclaw plugins install @rayray1218/semantic-model-router
快速开始
python
from scripts.model_router import ModelRouter
router = ModelRouter()
res = router.route(为金融科技平台设计一个分布式缓存层。)
print(res[report])
[ClawRouter] anthropic/claude-sonnet-4-6 (ELITE, ml, conf=0.97)
成本: $3.0/M | 基准: $10.0/M | 节省: 70.0%
路由工作原理
查询通过3阶段流水线分为三个层级:
- 1. 机器学习分类器(主要):基于6000+标注查询训练的Logistic回归模型。从model_weights.py中的嵌入式权重运行,耗时<1ms。
- 语义嵌入(备用):通过sentence-transformers计算与层级意图向量的余弦相似度。
- 关键词规则(最后手段):无依赖的模式匹配。
| 层级 | 默认模型 | 典型工作负载 | 成本/1M | 与基准对比 |
|---|
| 基础 | deepseek/deepseek-chat | 问候、简单问答、闲聊 | $0.14 | 节省99% |
| 均衡 |
openai/gpt-4o-mini | 摘要、翻译、解释 | $0.15 |
节省99% |
|
精英 | anthropic/claude-sonnet-4-6 | 复杂编码、架构、安全 | $3.00 |
节省70% |
支持的模型(共17个,2026年2月验证)
Anthropic
| 模型 | 输入/1M | 输出/1M |
|---|
| anthropic/claude-sonnet-4-6 | $3.00 | $15.00 ★ 精英默认 |
| anthropic/claude-opus-4-5 |
$5.00 | $25.00 |
| anthropic/claude-haiku-4-5 | $0.80 | $4.00 |
OpenAI
| 模型 | 输入/1M | 输出/1M |
|---|
| openai/gpt-5 | $1.25 | $10.00 |
| openai/gpt-4o |
$2.50 | $10.00 |
| openai/gpt-4o-mini | $0.15 | $0.60 ★ 均衡默认 |
| openai/o3 | $2.00 | $8.00 |
| openai/o4-mini | $1.10 | $4.40 |
Google
| 模型 | 输入/1M | 输出/1M |
|---|
| google/gemini-3.0-pro | $1.25 | $10.00 |
| google/gemini-2.5-pro |
$1.25 | $10.00 |
| google/gemini-2.5-flash | $0.30 | $2.50 |
| google/gemini-2.5-flash-lite | $0.10 | $0.40 |
DeepSeek
| 模型 | 输入/1M | 输出/1M |
|---|
| deepseek/deepseek-chat (V3.2) | $0.28 | $0.42 ★ 基础默认 |
| deepseek/deepseek-reasoner (V3.2) |
$0.28 | $0.42 |
xAI (Grok)
| 模型 | 输入/1M | 输出/1M |
|---|
| xai/grok-3 | $3.00 | $15.00 |
| xai/grok-3-mini |
$0.30 | $0.50 |
定价来源:各提供商的官方API文档,2026年2月验证。
运行时覆盖模型
python
为精英层使用GPT-5.2,为基础层使用Gemini Flash Lite
router = ModelRouter(
elite_model=openai/gpt-5.2,
balanced_model=google/gemini-2.5-flash,
basic_model=google/gemini-2.5-flash-lite,
)
python
切换层级模型,无需重新创建路由器
router.set_model(ELITE, anthropic/claude-opus-4-5)
列出所有可用模型(命令行)
bash
python3 scripts/model_router.py --list-models
命令行使用
bash
路由单个查询
python3 scripts/model_router.py 从头实现AES加密
覆盖精英模型
python3 scripts/model_router.py --elite openai/gpt-5.2 编写一个编译器
运行完整冒烟测试
python3 scripts/model_router.py
动态关键词扩展
python
router.add_keywords(ELITE, [密码学证明, 零知识])
示例输出
查询 预测 期望 ✓ 成本信息
────────────────────────────────────────────────────────────────────────────────────
你今天过得怎么样? 基础 基础 ✓ $0.14/M 节省98.6%
用三个要点总结这篇文章。 均衡 均衡 ✓ $0.15/M 节省98.5%
在Python中实现线程安全的LRU缓存。 精英 精英 ✓ $3.0/M 节省70.0%
安全与隐私
- - 零外部调用:所有分类均在本地运行。
- 无需API密钥:路由器本身不需要任何密钥。
- 透明权重:所有模型参数位于scripts/model_weights.py中——完全可审计。
节省成本,更智能地路由。为OpenClaw社区构建。