Skip to main content

优化策略概览

消息剪裁

减少不必要的上下文

模型选择

根据场景选择合适的模型

批量处理

合并多个请求减少开销

缓存策略

缓存常见问题的答案

1. 使用消息剪裁

启用消息剪裁减少 Token 消耗:
{
  "prune": true,
  "pruneOptions": {
    "targetTokens": 8000,
    "preserveRecentMessages": 3
  }
}
预期收益:Token 使用减少 40-70%

2. 选择合适的模型

根据任务复杂度选择模型:
任务类型推荐模型性价比
简单问答Qwen Plus⭐⭐⭐⭐⭐
通用对话Claude 3.7 Sonnet⭐⭐⭐⭐
复杂推理Claude 3.7 Sonnet⭐⭐⭐
中文场景Qwen Max⭐⭐⭐⭐

3. 实现请求缓存

缓存常见问题的答案:
const cache = new Map();

async function cachedChat(message) {
  const cacheKey = message.toLowerCase();

  if (cache.has(cacheKey)) {
    return cache.get(cacheKey);
  }

  const response = await chat(message);
  cache.set(cacheKey, response);

  // 设置过期时间(1小时)
  setTimeout(() => cache.delete(cacheKey), 3600000);

  return response;
}

4. 批量处理

合并多个独立请求:
// ❌ 不推荐:多次单独请求
for (const question of questions) {
  await chat(question);
}

// ✅ 推荐:合并为单次请求
const combinedPrompt = questions
  .map((q, i) => `问题${i+1}: ${q}`)
  .join('\n');

await chat(combinedPrompt);

5. 使用流式输出

流式输出提升用户体验,且不增加成本:
{
  "stream": true
}
优势
  • 用户感知响应更快
  • 可以提前取消不需要的请求
  • Token 计费相同

6. 精简系统提示词

保持系统提示词简洁明了:
// ❌ 过长的提示词
const systemPrompt = `
你是一个专业的AI助手。你需要遵循以下规则:
1. 回答要准确
2. 态度要友好
3. 格式要规范
...(200字)
`;

// ✅ 简洁的提示词
const systemPrompt = "你是一个专业友好的AI助手,回答要准确、简洁。";

7. 合理使用工具

仅在必要时启用工具:
// 简单对话不需要工具
const simpleChat = {
  messages: [...],
  // 不设置 allowedTools
};

// 需要执行操作时才启用
const actionChat = {
  messages: [...],
  allowedTools: ['bash']
};

8. 监控和分析

跟踪关键指标:
function logMetrics(response) {
  const { inputTokens, outputTokens, totalTokens } = response.data.usage;

  console.log({
    timestamp: new Date().toISOString(),
    inputTokens,
    outputTokens,
    totalTokens,
    cost: calculateCost(totalTokens)
  });
}

性能对比

优化措施Token 节省成本节省实施难度
消息剪裁40-70%40-70%简单
模型降级0%30-50%简单
请求缓存变化10-30%中等
精简提示词5-10%5-10%简单
批量处理20-40%20-40%中等

成本估算

典型场景的月成本估算(基于 Claude 3.7 Sonnet):
场景日请求数平均 Tokens月成本优化后
小型应用1,0001,500$45$20
中型应用10,0002,000$600$250
大型应用100,0001,800$5,400$2,200

最佳实践清单

  • 启用消息剪裁
  • 根据场景选择合适的模型
  • 实现请求结果缓存
  • 使用流式输出提升体验
  • 精简系统提示词
  • 仅在需要时启用工具
  • 监控 Token 使用情况
  • 定期审查和优化

下一步