V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录

HodlAI 简介

HodlAI 是一个将 Web3 经济模型与 AI API 使用结合的平台。持有 $HODLAI 代币即可获得每日免费额度,用于调用 200+ 顶级 AI 模型,包括 GPT-5.2 、Claude 4.5 、Gemini 3 Pro 等。AI API 兼容 OpenAI 、Anthropic 和其他主流接口。

核心特点

  • 🧠 200+ AI 模型一站调用
  • 🪙 持币领取免费额度(每日自动刷新)
  • 📊 链上透明,资产始终在用户钱包
  • 🔗 全端兼容 OpenAI / Claude / Gemini 接口
  • 🔑 链接钱包即可获取 API Key

经济机制

  • 每笔交易收取 3% 作为资金池保障
  • 每持仓价值 $10 代币 = $1 每日额度
  • 钻石手机制:持有越久额度释放越快

相关链接

平台 链接
🌐 官网 https://hodlai.fun/
🐦 Twitter https://x.com/hodlai_bsc
💬 Telegram https://t.me/hodlai_fun
finne
V2EX  ›  HodlAI

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

  •  
  •   finne · 1 天前 · 582 次点击

    最近在用 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

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

    15 条回复    2026-02-10 14:42:39 +08:00
    88AI
        1
    88AI  
    PRO
       1 天前
    厉害厉害,支持缓存了!我们已自动添加 cache_control: {"type": "ephemeral"} 标记,用户无需手动添加。
    finne
        2
    finne  
    OP
       1 天前 via iPhone
    @88AI 但是我看日志里的 opus4.6 还是按全量的价格计费呀
    88AI
        3
    88AI  
    PRO
       1 天前
    @finne 更新了
    finne
        4
    finne  
    OP
       1 天前 via iPhone
    @88AI okok 刚刚测试下 opus4.6 cache 计费正常工作了
    workbest
        5
    workbest  
       1 天前
    awesome
    ares001
        6
    ares001  
    PRO
       1 天前
    难怪费用扣得那么快
    88AI
        7
    88AI  
    PRO
       1 天前
    @ares001 这下全站每日支出会少很多了
    WinkeyLin
        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"
    }
    88AI
        9
    88AI  
    PRO
       21 小时 7 分钟前
    @WinkeyLin 你不用添加了
    88AI
        10
    88AI  
    PRO
       21 小时 7 分钟前
    @WinkeyLin 忽略了。再试试
    kkcity
        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
    88AI
        12
    88AI  
    PRO
       7 小时 43 分钟前
    @kkcity 更新了下,再试试有没有问题
    @WinkeyLin
    WinkeyLin
        13
    WinkeyLin  
       7 小时 2 分钟前
    @88AI 我这边目前用下来没出现这个问题了,谢谢 dev
    kkcity
        14
    kkcity  
       7 小时 0 分钟前
    @88AI 可以了,现在没有问题了
    kkcity
        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"}
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   4309 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 08:32 · PVG 16:32 · LAX 00:32 · JFK 03:32
    ♥ Do have faith in what you're doing.