适用于 OpenClaw v2026.2 | 本文假设你已完成基础配置,需要优化模型选择或成本控制。
TL;DR: OpenClaw 支持多模型:Claude Opus/Sonnet/Haiku、GPT-4o、Gemini、本地模型(Ollama)。配置 "agent": {"model": "anthropic/claude-opus-4-6"} 即可切换。Failover 自动切换备用模型:"fallback": [{"model": "claude-sonnet-4", "condition": "rate_limit"}]。推荐 Claude Opus 用于复杂任务,Haiku 用于高频简单任务。
支持的模型提供商
模型提供商概览
| 提供商 | 推荐模型 | 特点 | 价格参考 |
|---|---|---|---|
| Anthropic | Claude Opus 4.6 / Sonnet 4 | 长上下文、强推理、抗注入 | $15-75/1M tokens |
| OpenAI | GPT-4o / GPT-4-turbo | 生态完善、工具调用强 | $5-30/1M tokens |
| Gemini Pro / Ultra | 多模态、长上下文 | $1-7/1M tokens | |
| Groq | Llama 3 / Mixtral | 极速推理 | 免费/低价 |
| 本地 | Ollama / LM Studio | 隐私、无限制 | 免费(需硬件) |
模型能力对比
| 模型 | 上下文长度 | 工具调用 | 多模态 | 推理能力 | 成本 |
|---|---|---|---|---|---|
| Claude Opus 4.6 | 200K | ✅ | ✅ | ⭐⭐⭐⭐⭐ | 高 |
| Claude Sonnet 4 | 200K | ✅ | ✅ | ⭐⭐⭐⭐ | 中 |
| GPT-4o | 128K | ✅ | ✅ | ⭐⭐⭐⭐ | 中 |
| GPT-4-turbo | 128K | ✅ | ⚠️ | ⭐⭐⭐⭐ | 中高 |
| Gemini Pro | 1M | ✅ | ✅ | ⭐⭐⭐ | 低 |
| Llama 3.1 70B | 128K | ⚠️ | ⚠️ | ⭐⭐⭐ | 免费 |
基础模型配置
Anthropic Claude
{
"agent": {
"model": "anthropic/claude-opus-4-6",
"thinkingLevel": "high"
},
"models": {
"anthropic": {
"apiKey": "${ANTHROPIC_API_KEY}",
"baseUrl": "https://api.anthropic.com"
}
}
}
Claude 模型选择建议:
| 场景 | 推荐模型 | 原因 |
|---|---|---|
| 复杂推理、代码生成 | Opus 4.6 | 最强推理能力 |
| 日常对话、简单任务 | Sonnet 4 | 性价比高 |
| 高频调用、成本敏感 | Haiku 3.5 | 速度快、成本低 |
OpenAI GPT
{
"agent": {
"model": "openai/gpt-4o"
},
"models": {
"openai": {
"apiKey": "${OPENAI_API_KEY}",
"baseUrl": "https://api.openai.com/v1",
"organization": "org-xxx"
}
}
}
GPT 模型选择建议:
| 场景 | 推荐模型 | 原因 |
|---|---|---|
| 通用任务、工具调用 | GPT-4o | 平衡性能和成本 |
| 复杂推理 | GPT-4-turbo | 推理能力强 |
| 快速响应、简单任务 | GPT-4o-mini | 速度快、便宜 |
Google Gemini
{
"agent": {
"model": "google/gemini-2.0-flash"
},
"models": {
"google": {
"apiKey": "${GOOGLE_API_KEY}",
"baseUrl": "https://generativelanguage.googleapis.com/v1beta"
}
}
}
本地模型(Ollama)
{
"agent": {
"model": "ollama/llama3.1:70b"
},
"models": {
"ollama": {
"baseUrl": "http://localhost:11434"
}
}
}
Ollama 安装与启动:
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 下载模型
ollama pull llama3.1:70b
ollama pull codellama:34b
# 启动服务
ollama serve
第三方 API 中转
{
"agent": {
"model": "anthropic/claude-opus-4-6"
},
"models": {
"anthropic": {
"apiKey": "${PROVIDER_API_KEY}",
"baseUrl": "https://api.your-provider.com/v1"
}
}
}
模型格式说明
模型标识符格式
{provider}/{model-name}
| 示例 | 说明 |
|---|---|
anthropic/claude-opus-4-6 |
Anthropic Claude Opus 4.6 |
openai/gpt-4o |
OpenAI GPT-4o |
google/gemini-2.0-flash |
Google Gemini 2.0 Flash |
ollama/llama3.1:70b |
Ollama Llama 3.1 70B |
自定义模型别名
{
"models": {
"aliases": {
"smart": "anthropic/claude-opus-4-6",
"fast": "anthropic/claude-sonnet-4",
"cheap": "anthropic/claude-haiku-3.5",
"local": "ollama/llama3.1:70b"
}
},
"agent": {
"model": "smart"
}
}
使用别名:
# 在聊天中切换
/model fast
# 或使用完整名称
/model anthropic/claude-sonnet-4
Failover 策略
什么是 Failover?
当主模型不可用时(如达到速率限制、服务中断),自动切换到备用模型。
flowchart TB
REQ[请求到达] --> PRIMARY{主模型可用?}
PRIMARY -->|是| EXEC1[使用主模型]
PRIMARY -->|否| CHECK{Failover 条件}
CHECK -->|速率限制| SECONDARY1[备用模型 1]
CHECK -->|服务中断| SECONDARY2[备用模型 2]
CHECK -->|超时| SECONDARY3[备用模型 3]
SECONDARY1 --> EXEC2[执行请求]
SECONDARY2 --> EXEC2
SECONDARY3 --> EXEC2
EXEC1 --> RES[返回结果]
EXEC2 --> RES
配置 Failover
{
"agent": {
"model": "anthropic/claude-opus-4-6"
},
"models": {
"fallback": [
{
"model": "anthropic/claude-sonnet-4",
"condition": "rate_limit"
},
{
"model": "openai/gpt-4o",
"condition": "error"
},
{
"model": "ollama/llama3.1:70b",
"condition": "timeout"
}
]
}
}
Failover 条件
| 条件 | 说明 | 示例场景 |
|---|---|---|
rate_limit |
速率限制 | API 返回 429 错误 |
error |
任何错误 | 服务不可用、网络错误 |
timeout |
请求超时 | 响应时间过长 |
overloaded |
服务过载 | API 返回 503 错误 |
always |
总是使用 | 简单的轮询策略 |
高级 Failover 配置
{
"models": {
"fallback": [
{
"model": "anthropic/claude-sonnet-4",
"condition": "rate_limit",
"maxRetries": 3,
"retryDelay": 1000
},
{
"model": "openai/gpt-4o",
"condition": "error",
"maxRetries": 2
},
{
"model": "ollama/llama3.1:70b",
"condition": "always"
}
],
"fallbackPolicy": {
"retryPrimaryAfter": 60000,
"logFallbacks": true,
"notifyUser": true
}
}
}
场景化模型切换
按会话类型切换
不同类型的会话使用不同模型:
{
"agents": {
"defaults": {
"model": "anthropic/claude-sonnet-4"
},
"byChannel": {
"telegram": {
"model": "anthropic/claude-haiku-3.5"
},
"discord": {
"model": "anthropic/claude-sonnet-4"
}
},
"bySessionType": {
"main": {
"model": "anthropic/claude-opus-4-6"
},
"group": {
"model": "anthropic/claude-haiku-3.5"
}
}
}
}
按任务类型切换
{
"agents": {
"taskModels": {
"code": "anthropic/claude-opus-4-6",
"reasoning": "anthropic/claude-opus-4-6",
"chat": "anthropic/claude-sonnet-4",
"simple": "anthropic/claude-haiku-3.5"
}
}
}
动态模型选择
在运行时切换模型:
# CLI 切换
openclaw agent --model anthropic/claude-opus-4-6 --message "复杂任务"
# 聊天中切换
/model anthropic/claude-opus-4-6
现在使用 Claude Opus 4.6,适合复杂推理任务
/model anthropic/claude-haiku-3.5
切换到 Haiku 3.5,快速响应简单任务
成本优化策略
模型成本对比
| 模型 | 输入成本 | 输出成本 | 适用场景 |
|---|---|---|---|
| Claude Opus 4.6 | $15/1M | $75/1M | 复杂推理、重要决策 |
| Claude Sonnet 4 | $3/1M | $15/1M | 日常任务、平衡选择 |
| Claude Haiku 3.5 | $0.80/1M | $4/1M | 简单任务、高频调用 |
| GPT-4o | $5/1M | $15/1M | 工具调用、多模态 |
| GPT-4o-mini | $0.15/1M | $0.6/1M | 快速响应、简单任务 |
| Gemini Flash | $0.075/1M | $0.3/1M | 成本敏感、高频调用 |
成本优化配置
{
"agents": {
"defaults": {
"model": "anthropic/claude-sonnet-4",
"costOptimization": {
"enabled": true,
"rules": [
{
"condition": "context_length > 50000",
"model": "anthropic/claude-sonnet-4"
},
{
"condition": "simple_query",
"model": "anthropic/claude-haiku-3.5"
},
{
"condition": "code_generation",
"model": "anthropic/claude-opus-4-6"
}
]
}
}
}
}
Token 使用监控
# 查看当前会话使用量
/status
# 输出示例:
# Session: main
# Model: anthropic/claude-opus-4-6
# Tokens: 12,345 input / 2,456 output
# Cost: $0.21
# 开启使用量显示
/usage full
配置使用量显示
{
"usageTracking": {
"enabled": true,
"showInResponse": true,
"trackBySession": true,
"trackByChannel": true,
"alertThreshold": 10.00
}
}
思考级别配置
Thinking Level 说明
| 级别 | 说明 | Token 消耗 | 适用场景 |
|---|---|---|---|
off |
不显示思考 | 最低 | 简单任务 |
minimal |
最小思考 | 低 | 快速响应 |
low |
低思考 | 中低 | 日常对话 |
medium |
中等思考 | 中 | 平衡选择 |
high |
高度思考 | 高 | 复杂推理 |
xhigh |
极高思考 | 最高 | 最复杂任务 |
配置思考级别
{
"agent": {
"model": "anthropic/claude-opus-4-6",
"thinkingLevel": "high"
}
}
动态切换
# 聊天中切换
/think high
现在使用高思考模式,适合复杂推理
/think minimal
切换到最小思考,快速响应
本地模型配置详解
Ollama 配置
{
"agent": {
"model": "ollama/llama3.1:70b"
},
"models": {
"ollama": {
"baseUrl": "http://localhost:11434",
"options": {
"temperature": 0.7,
"num_ctx": 32768,
"num_predict": 4096
}
}
}
}
推荐本地模型
| 模型 | 参数量 | 内存需求 | 特点 |
|---|---|---|---|
| Llama 3.1 8B | 8B | 8GB | 轻量、快速 |
| Llama 3.1 70B | 70B | 48GB | 强推理 |
| CodeLlama 34B | 34B | 24GB | 代码专用 |
| Qwen 2.5 72B | 72B | 48GB | 多语言 |
| Mistral Large | 123B | 80GB | 最强开源 |
硬件建议
| 模型规模 | CPU | GPU | 内存 |
|---|---|---|---|
| 7B-8B | 8核+ | 8GB VRAM | 16GB |
| 13B-14B | 12核+ | 16GB VRAM | 32GB |
| 30B-34B | 16核+ | 24GB VRAM | 48GB |
| 70B+ | 32核+ | 48GB+ VRAM | 128GB |
多提供商负载均衡
轮询策略
{
"models": {
"loadBalancing": {
"strategy": "round_robin",
"providers": [
{
"model": "anthropic/claude-sonnet-4",
"weight": 1
},
{
"model": "openai/gpt-4o",
"weight": 1
}
]
}
}
}
加权策略
{
"models": {
"loadBalancing": {
"strategy": "weighted",
"providers": [
{
"model": "anthropic/claude-sonnet-4",
"weight": 70
},
{
"model": "openai/gpt-4o",
"weight": 20
},
{
"model": "ollama/llama3.1:70b",
"weight": 10
}
]
}
}
}
基于成本的策略
{
"models": {
"loadBalancing": {
"strategy": "cost_optimized",
"dailyBudget": 10.00,
"providers": [
{
"model": "anthropic/claude-opus-4-6",
"maxBudget": 5.00,
"useFor": ["complex", "reasoning"]
},
{
"model": "anthropic/claude-sonnet-4",
"maxBudget": 3.00,
"useFor": ["chat", "code"]
},
{
"model": "anthropic/claude-haiku-3.5",
"maxBudget": 2.00,
"useFor": ["simple", "high_frequency"]
}
]
}
}
}
模型配置最佳实践
1. 按场景分层
{
"agents": {
"defaults": {
"model": "anthropic/claude-sonnet-4",
"thinkingLevel": "medium"
},
"byUseCase": {
"coding": {
"model": "anthropic/claude-opus-4-6",
"thinkingLevel": "high"
},
"chat": {
"model": "anthropic/claude-haiku-3.5",
"thinkingLevel": "minimal"
},
"research": {
"model": "anthropic/claude-opus-4-6",
"thinkingLevel": "xhigh"
}
}
}
}
2. 设置合理预算
{
"budget": {
"daily": 20.00,
"weekly": 100.00,
"monthly": 400.00,
"alertThreshold": 0.8,
"hardLimit": true
}
}
3. 监控和分析
{
"analytics": {
"trackUsage": true,
"trackCosts": true,
"trackPerformance": true,
"exportPath": "~/.openclaw/analytics",
"retentionDays": 30
}
}
4. 测试和验证
# 测试模型连接
openclaw doctor --check models
# 测试特定模型
openclaw agent --model anthropic/claude-opus-4-6 --message "测试消息"
# 比较模型响应
openclaw benchmark --models "anthropic/claude-opus-4-6,openai/gpt-4o" --prompt "复杂问题"
故障排查
模型连接失败
# 检查 API Key
openclaw config get models.anthropic.apiKey
# 测试连接
curl -X POST https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "claude-sonnet-4", "max_tokens": 10, "messages": [{"role": "user", "content": "hi"}]}'
# 检查网络
ping api.anthropic.com
速率限制
# 查看使用量
openclaw usage --today
# 输出示例:
# Date: 2026-02-26
# Total tokens: 123,456
# Cost: $1.23
# Requests: 89
# 配置速率限制处理
openclaw config set models.anthropic.rateLimitHandling auto_retry
本地模型问题
# 检查 Ollama 服务
curl http://localhost:11434/api/tags
# 检查模型是否加载
ollama list
# 手动加载模型
ollama run llama3.1:70b
# 检查 GPU 使用
nvidia-smi
小结
多模型配置是 OpenClaw 的强大功能之一:
- 灵活切换:根据任务需求选择合适的模型
- Failover 保护:自动处理模型不可用情况
- 成本优化:合理分配预算,避免浪费
- 本地支持:隐私敏感场景使用本地模型
通过合理的模型配置,你可以在性能、成本、隐私之间找到最佳平衡点。
本篇小结:
- 掌握了多种模型提供商的配置方法
- 理解了 Failover 策略和配置
- 学会了按场景动态切换模型
- 了解了成本优化策略和监控方法
- 掌握了本地模型的配置和使用
更新记录:
- 2026-02-26:初版发布,基于 OpenClaw v2026.2
系列导航:
- ← 上一篇:Gateway 核心概念解析
- → 下一篇:Skills 技能系统详解