P1 Anthropic Prompt Caching 插件 — 对标 DeepAgents AnthropicPromptCachingMiddleware。
在 before_request 事件中检测是否为 Anthropic 模型,若是则选取最多
max_cache_points(默认 3)个消息位置打上 cache_control 标记,
通过 emit {:update_message_metadata, list} 通知 Agent 更新消息 metadata。
Provider 在构建 Anthropic 请求时读取 metadata 中的 cache_control: true,
将对应消息的 content block 末尾追加 {"cache_control": {"type": "ephemeral"}}。
非 Anthropic 模型(OpenAI、Gemini 等)自动跳过,不修改任何消息。
缓存点选取策略
按以下优先级选取,最多 max_cache_points 个:
- system 消息(通常是最大的固定内容)
- 最后一条 user 消息(对话的最新上下文)
- 最早的 assistant + tool_result 消息对(历史工具调用结果)
配置
{CMDC.Plugin.Builtin.PromptCache,
max_cache_points: 3, # 最多打多少个缓存标记(Anthropic 限制最多 4 个)
min_tokens: 100 # 跳过估计 token 数低于此值的消息(避免无效缓存)
}