HodlAI 简介
HodlAI 是一个将 Web3 经济模型与 AI API 使用结合的平台。持有 $HODLAI 代币即可获得每日免费额度,用于调用 200+ 顶级 AI 模型,包括 GPT-5.2 、Claude 4.5 、Gemini 3 Pro 等。AI API 兼容 OpenAI 、Anthropic 和其他主流接口。
核心特点
经济机制
| 平台 | 链接 |
|---|---|
| 🌐 官网 | https://hodlai.fun/ |
| https://x.com/hodlai_bsc | |
| 💬 Telegram | https://t.me/hodlai_fun |
最近在用 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 计价还没适配好。
cache_control 标记有用同样方案的朋友可以试试,省不少钱。
1
88AI PRO 厉害厉害,支持缓存了!我们已自动添加 cache_control: {"type": "ephemeral"} 标记,用户无需手动添加。
|
5
workbest 1 天前
awesome
|
6
ares001 PRO 难怪费用扣得那么快
|
8
WinkeyLin 22 小时 11 分钟前
@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" } |
11
kkcity 8 小时 1 分钟前
@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 |
15
kkcity 1 小时 49 分钟前
@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"} |