Loading...
Loading...
模型自动降级与故障切换。当主模型请求失败、超时、达到速率限制或配额耗尽时,自动切换到备用模型,确保服务连续性。支持多供应商、多优先级的智能模型选择,提供健康监控、自动重试和错误恢复机制。
npx skill4agent add aaaaqwq/claude-code-skills model-fallback优先级顺序:
1. anapi/opus-4.5 # 最强能力,最高优先级
2. zai/glm-4.7 # 中文优化,性价比高
3. openrouter-vip/gpt-5.2-codex # 编码专用
4. github-copilot/claude-sonnet-4-5 # 免费备用重试配置:
最大重试次数: 3
初始延迟: 1000ms
最大延迟: 10000ms
退避倍数: 2.0
使用备用模型: true第1次失败 → 等待 1s → 重试
第2次失败 → 等待 2s → 重试
第3次失败 → 等待 4s → 切换模型# 每5分钟检查一次
检查项目:
- API 端点连通性
- 响应时间
- 错误率
- 配额使用情况~/.openclaw/agents/main/agent/agent.json{
"model": "anapi/opus-4.5",
"modelFallback": [
"zai/glm-4.7",
"openrouter-vip/gpt-5.2-codex",
"github-copilot/claude-sonnet-4-5"
],
"retry": {
"maxAttempts": 3,
"initialDelayMs": 1000,
"maxDelayMs": 10000,
"backoffMultiplier": 2.0,
"useFallbackOnFailure": true
}
}# 启动后台监控
~/.openclaw/scripts/monitor-models.sh start
# 查看监控状态
~/.openclaw/scripts/monitor-models.sh status
# 停止监控
~/.openclaw/scripts/monitor-models.sh stop# 运行降级检查脚本
~/.openclaw/scripts/model-fallback.sh用户请求
↓
选择主模型 (anapi/opus-4.5)
↓
发送请求到 API
↓
成功 → 返回结果用户请求
↓
选择当前模型
↓
发送请求 → 失败/超时
↓
重试 (最多3次)
↓
仍然失败?
↓
切换到备用模型 (zai/glm-4.7)
↓
发送请求
↓
成功 → 返回结果并记录监控守护进程 (每5分钟)
↓
检查所有模型健康状态
↓
├─ 主模型健康 → 保持当前
└─ 主模型不健康 → 切换到最佳可用模型
↓
更新状态文件
↓
记录日志{
"timeout": {
"switchModel": true,
"retryCount": 2,
"timeoutMs": 60000,
"fallbackTo": "zai/glm-4.7"
}
}{
"rateLimit": {
"switchModel": true,
"cooldownMs": 60000,
"alert": true,
"fallbackTo": "zai/glm-4.7"
}
}{
"quotaExceeded": {
"switchModel": true,
"alert": true,
"fallbackTo": "zai/glm-4.7",
"checkInterval": 3600000
}
}{
"authenticationError": {
"switchModel": true,
"alert": true,
"disableModel": true
}
}{
"routing": {
"strategy": "priority-fallback",
"rules": [
{
"name": "coding-task",
"match": {
"contentContains": ["代码", "code", "编程", "函数"]
},
"preferModels": [
"openrouter-vip/gpt-5.2-codex",
"anapi/opus-4.5"
]
},
{
"name": "chinese-task",
"match": {
"language": "zh"
},
"preferModels": [
"zai/glm-4.7",
"anapi/opus-4.5"
]
},
{
"name": "vision-task",
"match": {
"hasImage": true
},
"preferModels": [
"anapi/opus-4.5"
]
}
]
}
}~/.openclaw/logs/model-fallback.log # 切换日志
~/.openclaw/logs/model-monitor.log # 监控日志
~/.openclaw/logs/model-status.json # 状态报告# 查看切换日志
tail -f ~/.openclaw/logs/model-fallback.log
# 查看监控日志
tail -f ~/.openclaw/logs/model-monitor.log
# 查看所有日志
tail -f ~/.openclaw/logs/*.log# 查看当前状态
~/.openclaw/scripts/monitor-models.sh status
# JSON 格式状态
cat ~/.openclaw/logs/model-status.json | python3 -m json.tool~/.openclaw/scripts/model-fallback.sh~/.openclaw/scripts/monitor-models.sh {start|stop|restart|status|check}~/clawd/scripts/test-model-fallback.sh{
"model": "anapi/opus-4.5",
"modelFallback": [
"zai/glm-4.7",
"openrouter-vip/gpt-5.2-codex",
"github-copilot/claude-sonnet-4-5"
],
"retry": {
"maxAttempts": 3,
"initialDelayMs": 1000,
"maxDelayMs": 10000,
"backoffMultiplier": 2.0,
"useFallbackOnFailure": true
},
"errorHandling": {
"rateLimit": {
"switchModel": true,
"cooldownMs": 60000,
"alert": true
},
"timeout": {
"switchModel": true,
"retryCount": 2,
"timeoutMs": 60000
},
"quotaExceeded": {
"switchModel": true,
"alert": true,
"fallbackTo": "zai/glm-4.7"
},
"authenticationError": {
"switchModel": true,
"alert": true,
"disableModel": true
}
},
"models": {
"anapi/opus-4.5": {
"provider": "anapi",
"alias": "opus45",
"maxTokens": 200000,
"timeoutMs": 60000,
"priority": 1,
"supports": ["vision", "tools", "long-context"],
"costFactor": "high"
},
"zai/glm-4.7": {
"provider": "zai",
"alias": "zai47",
"maxTokens": 200000,
"timeoutMs": 60000,
"priority": 2,
"supports": ["tools", "long-context"],
"costFactor": "medium",
"bestFor": ["chinese", "general-purpose"]
},
"openrouter-vip/gpt-5.2-codex": {
"provider": "openrouter-vip",
"alias": "codex52",
"maxTokens": 100000,
"timeoutMs": 30000,
"priority": 3,
"supports": ["coding"],
"costFactor": "low",
"bestFor": ["coding", "code-generation"]
},
"github-copilot/claude-sonnet-4-5": {
"provider": "github-copilot",
"alias": "sonnet",
"maxTokens": 200000,
"timeoutMs": 60000,
"priority": 4,
"supports": ["tools", "long-context"],
"costFactor": "free",
"bestFor": ["fallback", "general-purpose"]
}
},
"monitoring": {
"enabled": true,
"checkIntervalMs": 300000,
"logFile": "$HOME/.openclaw/logs/model-fallback.log",
"alertOnFailure": true
}
}# 查看配置文件
cat ~/.openclaw/agents/main/agent/agent.json | grep modelFallback
# 查看日志
tail -20 ~/.openclaw/logs/model-fallback.log
# 手动运行切换脚本
~/.openclaw/scripts/model-fallback.sh# 查看进程
ps aux | grep monitor-models
# 查看PID文件
cat ~/.openclaw/logs/model-monitor.pid
# 重启监控
~/.openclaw/scripts/monitor-models.sh restart# 查看状态报告
~/.openclaw/scripts/monitor-models.sh status
# 检查 API 密钥
cat ~/.openclaw/agents/main/agent/auth-profiles.json
# 测试网络连接
ping -c 3 anapi.9w7.cn
ping -c 3 open.bigmodel.cn{
"retry": {
"maxAttempts": 5, // 增加重试次数
"initialDelayMs": 2000 // 增加初始延迟
}
}{
"routing": {
"rules": [
{
"name": "quick-response",
"match": {
"priority": "speed"
},
"preferModels": [
"github-copilot/claude-sonnet-4-5", // 通常响应最快
"zai/glm-4.7"
]
}
]
}
}openclaw gateway restartopenclaw status | grep Modeljournalctl -u openclaw-gateway -f | grep model~/clawd/scripts/test-model-fallback.shgrep "切换模型" ~/.openclaw/logs/model-fallback.log | tail -10agent.json{
"modelFallback": [
"anapi/opus-4.5",
"zai/glm-4.7",
"new-model-here", // 新模型
"github-copilot/claude-sonnet-4-5"
]
}cp ~/.openclaw/agents/main/agent/agent.json \
~/.openclaw/agents/main/agent/agent.json.backup~/.openclaw/agents/main/agent/agent.json~/.openclaw/agents/main/agent/auth-profiles.json~/.openclaw/scripts/model-fallback.sh~/.openclaw/scripts/monitor-models.sh~/clawd/scripts/test-model-fallback.sh~/clawd/docs/model-fallback-strategy.md| 模型 | 供应商 | 优先级 | 最大Token | 特长 |
|---|---|---|---|---|
| opus-4.5 | anapi | 1 | 200k | 最强能力,视觉 |
| glm-4.7 | zai | 2 | 200k | 中文优化 |
| gpt-5.2-codex | openrouter-vip | 3 | 100k | 编码专用 |
| sonnet-4.5 | github-copilot | 4 | 200k | 免费备用 |