在 OpenClaw 中使用 HodlAI Claude 模型开启 Prompt Cache:需要改一行代码 + Opus 4.6 计费异常反馈

1 天前
 finne

最近在用 HodlAI 的 Claude 模型跑 OpenClaw (一个 AI agent 框架),发现默认情况下 prompt cache 不生效,研究了一下原因和解决方案,分享给有需要的朋友。

背景

HodlAI 的 API 兼容 OpenAI Chat Completions 格式,支持 Anthropic 的 prompt caching 机制——需要在 messages 中对 content block 加上 cache_control: {"type": "ephemeral"} 标记。

但 OpenClaw 底层( pi-ai 库)的 maybeAddOpenRouterAnthropicCacheControl 函数默认只对 OpenRouter 的 anthropic/* 模型注入 cache_control,自定义 provider 的 Claude 模型不会走这个逻辑,导致 cache 完全不生效。

解决方案:改一行代码

文件路径:

node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js

找到 maybeAddOpenRouterAnthropicCacheControl 函数,原始代码:

function maybeAddOpenRouterAnthropicCacheControl(model, messages) {
    if (model.provider !== "openrouter" || !model.id.startsWith("anthropic/"))
        return;

改为:

function maybeAddOpenRouterAnthropicCacheControl(model, messages) {
    const isOpenRouter = model.provider === "openrouter" && model.id.startsWith("anthropic/");
    const isClaudeOnCustomProvider = model.id.toLowerCase().includes("claude");
    if (!isOpenRouter && !isClaudeOnCustomProvider)
        return;

这样只要 model ID 包含 "claude"(不区分大小写),就会自动注入 cache_control

⚠️ 注意:OpenClaw 更新或 npm update 后会覆盖这个改动,需要重新打 patch 。

测试结果

改完后实测各模型缓存情况:

模型 Cache 命中 计费是否正常
Claude Sonnet 4.5 ✅ 正常(降约 91%)
Claude Opus 4.5 ✅ 正常(降约 91%)
Claude Opus 4.6 ✅ 后台显示命中 ❌ 仍按全量计费
Claude Opus 4-6 ✅ 后台显示命中 ❌ 仍按全量计费

Sonnet 4.5 和 Opus 4.5 缓存命中后费用从 ~$0.035 降到 ~$0.003 ( 9k tokens 测试),效果很明显。

Opus 4.6 和 Opus 4-6 虽然后台日志显示 cache 命中了,实际扣费还是按全量计算,感觉是 HodlAI 后端对这两个新模型的 cached token 计价还没适配好。

总结

  1. HodlAI Claude 模型 支持 prompt cache,但仅限 Chat Completions API + 显式 cache_control 标记
  2. OpenClaw 用户需要手动 patch 一行代码让 cache 注入生效
  3. Sonnet 4.5 / Opus 4.5 计费正常,cache 能省约 90% 费用
  4. Opus 4.6 / Opus 4-6 计费疑似有 bug ,希望官方看一下 🙏 @88AI

有用同样方案的朋友可以试试,省不少钱。

618 次点击
所在节点    HodlAI
15 条回复
88AI
1 天前
厉害厉害,支持缓存了!我们已自动添加 cache_control: {"type": "ephemeral"} 标记,用户无需手动添加。
finne
1 天前
@88AI 但是我看日志里的 opus4.6 还是按全量的价格计费呀
88AI
1 天前
@finne 更新了
finne
1 天前
@88AI okok 刚刚测试下 opus4.6 cache 计费正常工作了
workbest
1 天前
awesome
ares001
1 天前
难怪费用扣得那么快
88AI
1 天前
@ares001 这下全站每日支出会少很多了
WinkeyLin
1 天前
@88AI 现在 Claude 模型调用工具会报错,是不是自动添加的 cache_control: {"type": "ephemeral"} 和客户端添加的缓存设置冲突了?
{
"error": {
"type": "400",
"message": "Provider returned error ({\"raw\":\"{\\\"type\\\":\\\"error\\\",\\\"error\\\":{\\\"type\\\":\\\"invalid_request_error\\\",\\\"message\\\":\\\"***.***.content.0.cache_control.ttl: a ttl='1h' cache_control block must not come after a ttl='5m' cache_control block. Note that blocks are processed in the following order: `tools`, `system`, `messages`.\\\"},\\\"request_id\\\":\\\"req_xxx\\\"}\",\"provider_name\":\"Anthropic\",\"is_byok\":false})
},
"endpoint": "https://api.ho****ai.fun/",
"provider": "hodlai"
}
88AI
1 天前
@WinkeyLin 你不用添加了
88AI
1 天前
@WinkeyLin 忽略了。再试试
kkcity
14 小时 53 分钟前
@WinkeyLin 你这个问题解决了吗?我刚刚遇到同样的问题,Claude Code ,Opus 4.6:

⎿ API Error: 400 {"error":{"type":"400","message":"Provider returned error ({\"raw\":\"{\\\"type\\\":\\\"error\\\",\\\"error\\\":{\\\"type\\\":\\\"invalid_request_error\\\",\\\"message\\\":\\\"***.***.content.0.cache_control.ttl: a ttl='1h' cache_control block must not come after a ttl='5m' cache_control block. Note
that blocks are processed in the following order: `tools`, `system`, `messages`.\\\"},\\\"request_id\\\":\\\"req_vrtx_011CXySDzLubgsLVtjbMNzHp\\\"}\",\"provider_name\":\"Google\",\"is_byok\":false}) (request id: 20260210002346391351325F7FIPBYC)"},"type":"error"}

我本地之前没做过任何修改。

@88AI
88AI
14 小时 36 分钟前
@kkcity 更新了下,再试试有没有问题
@WinkeyLin
WinkeyLin
13 小时 55 分钟前
@88AI 我这边目前用下来没出现这个问题了,谢谢 dev
kkcity
13 小时 53 分钟前
@88AI 可以了,现在没有问题了
kkcity
8 小时 42 分钟前
@88AI 又出错了:

⏺ Web Search("Python read character image "sample_size" "tag_code"")
⎿  Did 0 searches in 5s
⎿  API Error: 400 {"error":{"type":"400","message":"Provider returned error ({\"raw\":\"{\\\"type\\\":\\\"error\\\",\\\"error\\\":{\\\"type\\\":\\\"invalid_request_error\\\",\\\"message\\\":\\\"A maximum of 4 blocks with cache_control may be provided. Found
5.\\\"},\\\"request_id\\\":\\\"req_vrtx_011CXyvxErxm1KrFfWXEo37p\\\"}\",\"provider_name\":\"Google\",\"is_byok\":false}) (request id: 20260210064030229492992k21gEBPq)"},"type":"error"}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1191680

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX