模型路由概述
在多模型环境中,不同类型的请求适合不同的模型。OpenClaw 的智能模型路由可以根据消息内容、用户属性和频道设置自动选择最合适的模型,实现性能与成本的最优平衡。
基础路由配置
{
"routing": {
"enabled": true,
"defaultModel": "fast",
"rules": [
{
"name": "code-tasks",
"match": {"content": "代码|编程|bug|函数|调试|Python|JavaScript"},
"model": "code-expert"
},
{
"name": "complex-reasoning",
"match": {"content": "分析|比较|推理|规划|策略"},
"model": "smart"
},
{
"name": "translation",
"match": {"content": "翻译|translate|英译中|中译英"},
"model": "translator"
}
]
}
}
匹配条件
按内容匹配
{
"match": {
"content": "关键词1|关键词2|关键词3"
}
}
支持正则表达式:
{
"match": {
"content": "\\b(def|class|function|import)\\b"
}
}
按用户匹配
{
"match": {
"user": ["user001", "user002"]
}
}
按频道匹配
{
"match": {
"channel": "discord-dev"
}
}
按消息长度匹配
{
"match": {
"contentLength": {
"min": 500
}
}
}
组合条件
{
"match": {
"content": "代码|编程",
"channel": "discord-dev",
"user": ["dev-user1", "dev-user2"]
}
}
多个条件之间是 AND 关系。
完整路由配置示例
{
"routing": {
"enabled": true,
"defaultModel": "fast",
"rules": [
{
"name": "vip-users",
"priority": 100,
"match": {"user": ["vip001", "vip002"]},
"model": "smart"
},
{
"name": "coding-channel",
"priority": 90,
"match": {"channel": "discord-dev"},
"model": "code-expert"
},
{
"name": "long-content",
"priority": 80,
"match": {"contentLength": {"min": 500}},
"model": "smart"
},
{
"name": "code-keywords",
"priority": 70,
"match": {"content": "\\b(code|bug|function|class)\\b"},
"model": "code-expert"
},
{
"name": "default",
"priority": 0,
"match": {"content": ".*"},
"model": "fast"
}
]
}
}
规则按 priority 从高到低匹配,第一个匹配的规则生效。
AI 分类路由
使用轻量级模型进行意图分类,再路由到合适的模型:
{
"routing": {
"classifier": {
"enabled": true,
"model": "fast",
"categories": {
"coding": {"model": "code-expert"},
"creative": {"model": "smart"},
"translation": {"model": "translator"},
"general": {"model": "fast"}
}
}
}
}
分类器会先用轻量模型判断意图类别,再将请求路由到对应模型。
成本路由
按成本预算进行路由:
{
"routing": {
"costAware": {
"enabled": true,
"dailyBudget": 20.00,
"thresholds": [
{"usage": 0.8, "model": "fast"},
{"usage": 0.5, "model": "smart"},
{"usage": 0.0, "model": "premium"}
]
}
}
}
当日消费低于预算 50% 时使用最好的模型,超过 80% 后自动降级到经济模型。
查看路由统计
openclaw routing stats
Routing Statistics (last 24h):
Rule Matches Model Avg Latency
─────────────────────────────────────────────────────
vip-users 120 smart 2.1s
coding-channel 85 code-expert 1.8s
code-keywords 200 code-expert 1.7s
default 1,500 fast 0.8s
测试路由规则
openclaw routing test "帮我写一个Python排序函数"
Message: "帮我写一个Python排序函数"
Matched rule: code-keywords (priority: 70)
Selected model: code-expert
Reason: Content matches pattern "\\b(code|bug|function|class)\\b"
总结
智能模型路由让 OpenClaw 能够在多模型环境中做出最优选择。通过基于内容、用户和预算的路由规则,可以在保证服务质量的同时最大化资源利用效率。