"我见过一个Agent,每次交互消耗50K tokens,月账单$3000。优化后降到8K tokens,月账单$480。省下来的钱够买一台不错的GPU了。Token优化不是抠门,是工程素养。"
Token成本一览
| 模型 | 输入$/1M tokens | 输出$/1M tokens | 适合场景 |
|---|---|---|---|
| GPT-4o | $2.50 | $10.00 | 复杂推理 |
| GPT-4o-mini | $0.15 | $0.60 | 日常对话 |
| Claude 3.5 Sonnet | $3.00 | $15.00 | 长文本分析 |
| Claude 3 Haiku | $0.25 | $1.25 | 快速响应 |
| Llama 3 (本地) | $0 | $0 | 隐私/离线 |
八大优化策略
策略1:模型路由(Model Routing)
不同任务用不同模型,便宜够用就行:
# 智能模型路由配置
model_routing:
rules:
# 简单问题用便宜模型
- match:
intent: ["greeting", "simple_qa"]
complexity: "low"
model: "gpt-4o-mini" # $0.15/$0.60
# 中等复杂度用中等模型
- match:
intent: ["summarize", "translate"]
complexity: "medium"
model: "claude-3-haiku" # $0.25/$1.25
# 复杂任务用强模型
- match:
intent: ["code_review", "analysis"]
complexity: "high"
model: "gpt-4o" # $2.50/$10.00
# 长文档用长上下文模型
- match:
context_length: "> 50000"
model: "claude-3.5-sonnet" # 200K上下文
# 默认模型
default: "gpt-4o-mini"
# 节省比例:约60-70%
策略2:语义缓存(Semantic Cache)
# 相似问题命中缓存
cache:
enabled: true
type: "semantic" # 语义缓存(不只是精确匹配)
embedding_model: "text-embedding-3-small"
similarity_threshold: 0.95 # 相似度>95%命中缓存
ttl: 3600 # 缓存1小时
# 命中统计
stats:
hit_rate: "~25-40%" # 典型缓存命中率
savings: "~30-50%" # 对应的成本节省
策略3:Prompt精简
# ❌ 冗长Prompt(~2000 tokens)
"""
你是一个专业的AI编程助手。你的任务是帮助用户解决编程问题。
当用户向你提出问题时,你应该:
1. 仔细分析用户的问题描述
2. 理解用户的真实意图
3. 提供清晰的解决方案
4. 如果需要,提供代码示例
5. 解释你的解决方案
请确保你的回答准确、完整、有帮助。
如果不确定,请明确说明。
"""
# ✅ 精简Prompt(~300 tokens)
"""
你是编程助手。分析问题→给方案→附代码→做解释。
不确定就说不确定。简洁准确。
"""
# 节省:1700 tokens/请求 × 100次/天 = 170K tokens/天
# 按$2.50/1M计算:每天省$0.425,每月省$12.75
策略4:工具定义优化
# 只加载需要的工具(每个工具~200-500 tokens)
tool_management:
# ❌ 一次加载所有30个工具 = 6000-15000 tokens
# ✅ 按需加载,最多10个 = 2000-5000 tokens
max_concurrent: 10
lazy_loading: true
# 工具描述精简
descriptions:
verbose: true # 完整描述(首次加载)
compact: true # 精简描述(后续请求)
策略5:对话历史管理
# 对话历史Token优化
conversation:
# 策略1:滑动窗口
max_turns: 10 # 保留最近10轮
# 策略2:Token预算
max_history_tokens: 8000 # 历史最多8K tokens
# 策略3:摘要压缩
summarization:
trigger_after: 8 # 8轮后开始摘要
method: "incremental" # 增量摘要(只摘要旧消息)
# 策略4:关键信息提取
key_extraction:
preserve: ["user_preference", "decision", "todo"]
discard: ["small_talk", "acknowledgment"]
策略6:输出长度控制
# 控制输出Token数
output_control:
# 设置max_tokens
max_tokens: 1000 # 默认最多1000 tokens输出
# 按场景调整
scenarios:
greeting: 50 # 打招呼50字够用
summary: 500 # 总结500字
code: 1500 # 代码可能需要更多
explanation: 300 # 解释300字
# Prompt中暗示
prompt_suffix: "回答控制在200字以内。"
策略7:批量处理
# 多个小请求合并为一个大请求
batching:
enabled: true
window: 30 # 30秒内的小请求合并
max_batch_size: 5 # 最多合并5个请求
# 单独请求:5次 × 300 tokens = 1500 tokens(含5次系统提示)
# 批量请求:1次 × 1000 tokens = 1000 tokens(1次系统提示)
# 节省:~33%
策略8:流式输出裁剪
# 用户中断时停止生成
streaming:
enabled: true
early_stop:
# 检测到用户可能的"够了"信号时停止
signals: ["以上就是", "总结来说", "---"]
check_interval: 100 # 每100 tokens检查一次
💡 省钱公式:模型路由(省60%)+ 语义缓存(省30%)+ Prompt精简(省20%)+ 输出控制(省15%)= 综合节省约 70-80%。一个原来$100/月的Agent可以优化到$20-30/月。
成本监控
# 设置预算告警
budget:
daily_limit: 5 # 每日$5上限
monthly_limit: 100 # 每月$100上限
alerts:
- threshold: 80% # 用到80%时警告
channel: "feishu"
- threshold: 100% # 到100%时停止
action: "rate_limit"
# 成本追踪
tracking:
per_agent: true
per_session: true
per_tool: true
report: "daily" # 每日成本报告
最佳实践
- 先测量再优化 - 不知道在哪花钱就不知道怎么省
- 80/20原则 - 80%的请求可以用便宜模型处理
- 缓存优先 - 重复问题不要重复计算
- 设置预算 - 无限预算是成本失控的开始
- 定期审查 - 每周检查哪些请求消耗最多
- 考虑本地模型 - 隐私敏感场景用Llama零成本