添加 prompt 缓存以降低 Claude API 费用
何时使用: API 支出在攀升,且每次请求都有重复的上下文。
步骤
-
阅读配方Use anthropic-cookbook. Show me the prompt caching pattern for repeated system prompts.✓ 已复制→ cache_control 示例
-
应用到我的代码Adapt to my service that re-sends a 5k-token style guide every call.✓ 已复制→ 缓存的系统 prompt;费用预测
-
验证节省效果Measure cache hit rate and cost delta over 100 calls.✓ 已复制→ 具体的成本降低数据
结果: 实际节省费用(缓存部分约 50-80%)。
注意事项
- 缓存生命周期较短——高流量工作负载收益最大 — 低流量 API 可能看不到缓存命中;优化前先评估
搭配使用: anthropic-courses-skill