Appearance
自定义 Anthropic 兼容 Provider 的 cacheRetention: long 静默回退到 5 分钟缓存
问题
使用第三方 Anthropic 兼容 API 代理(如 ZenMux)配合 api: "anthropic-messages" 时,配置 cacheRetention: "long" 应在 cache_control payload 中注入 ttl: "1h",但实际上静默回退到默认 5 分钟缓存。
根本原因:1 小时缓存 TTL 支持被硬编码仅限于官方 Anthropic provider,使用相同 anthropic-messages API 的自定义兼容 provider 被排除在外。
解决方案
该功能已在后续版本中修复:将 cacheRetention: "long" 的判断从"仅限官方 Anthropic provider"扩展为"所有使用 anthropic-messages API 的 provider"。
临时缓解措施(等待升级时):
方案 A - 改为使用 Anthropic 官方 provider:如果代理支持完全透传,直接将模型路由到官方 Anthropic endpoint 可绕过此限制。
方案 B - 显式指定 TTL 参数:在 agent 配置中通过 cacheTtl: "1h" 强制设置(如 provider 配置支持此字段)。
方案 C - 关闭长缓存,改用短缓存:将 cacheRetention 设为 "short"(5 分钟),至少能获得有效的缓存结果,避免完全不缓存:
json5
{
agents: {
defaults: {
cacheRetention: "short",
},
},
}社区补充方法
LiteLLM 代理场景(来自 #37966)
同样的问题发生在通过 LiteLLM 代理访问 Anthropic 模型时(如 litellm/claude-opus-4-6)。agents.defaults.models 中配置的 cacheRetention 被静默忽略,API payload 中没有注入任何 cache_control 标记。
根本原因相同:resolveCacheRetention 函数将自动注入限制在直连 Anthropic provider,LiteLLM 路由的模型不触发任何缓存处理。
PR #38221 提供了更全面的修复:将 resolveCacheRetention 扩展为对任何显式配置了 cacheRetention 或 cacheControlTtl 的 provider 都生效(包括 LiteLLM),仅将"自动默认值注入"保留给直连 Anthropic 的情况。这个修复比仅扩展 anthropic-messages API 检查更准确。
注:如果社区用户在等待官方合并期间需要自行 patch,找到
resolveCacheRetention函数所在文件,将触发条件从"provider === 'anthropic'"改为"provider === 'anthropic' || config.cacheRetention !== undefined"即可。