用 Claude Code 做了个 Claude Code 的成本计算器 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
keakon
V2EX    Claude

用 Claude Code 做了个 Claude Code 的成本计算器

  •  
  •   keakon 152 天前 1568 次点击
    这是一个创建于 152 天前的主题,其中的信息可能已经有所发展或是发生改变。
    开发成本将近 $100 ,虽然都是白嫖的……
    https://github.com/keakon/claude-code-cost
    13 条回复    2025-07-29 14:00:13 +08:00
    mogutouer
        1
    mogutouer  
       151 天前
    不是有 ccusage 么
    keakon
        2
    keakon  
    OP
       151 天前
    @mogutouer 慢,维度少,不好看
    mogutouer
        3
    mogutouer  
       151 天前
    @keakon #2 ccusage 少的是每一天里显示某个模型用量多少,他 json 里可以显示但 10 天列表里没有显示,你这个也没有
    keakon
        4
    keakon  
    OP
       151 天前
    @mogutouer 它没有项目维度,没有排除 0 token 的天数,没有消息数,不能进行汇率转换,不支持中英文自动检测,不能自定义模型的单价,日期不是按用户的时区……总之相差很多。

    不做每天里分模型的用量是感觉没必要,维度太多了会乱
    mogutouer
        5
    mogutouer  
       151 天前
    @keakon #4 有啊,在 json 里都有,每天的分模型非常重要,claude code 默认的模式就是 Default (Opus 4 for up to 50% of usage limits, then use Sonnet 4),至少今天这个时候看看 opus 和 sonnet 分别用了多少,好对接下来的 5 消失的 block 做规划,ccusage 虽然 json 里显示出来了,但看的很累,算起来也很累。
    keakon
        6
    keakon  
    OP
       151 天前
    @mogutouer 你要的不是今天的模型统计,而是剩余用量。因为 5 小时就重置了,你看之前的消耗也没用。

    不过 Claude Code 的定价里并没有明确说明 usage limits ,这 2 篇文章有提到三档分别是至少保证 45 、225 和 900 条消息,但也和消息长度之类的相关。不知道显示过去 5 小时的用量(或剩余用量)是否才是你的需求?
    https://support.anthropic.com/en/articles/8324991-about-claude-pro-usage
    https://support.anthropic.com/en/articles/11014257-about-claude-s-max-plan-usage
    mogutouer
        7
    mogutouer  
       151 天前
    @keakon #6 我的需求就是你的日期+日期的模型消耗,就是我要看 opus 用了多少了,差不多的话,任务又不重要我就切到 sonnet 了,5 小时剩余时间如果有重要任务我再选择性的使用 opus ,无论是 ccusage 还是你这个项目,我都无法快捷的看到我这 5 个小时,opus 和 sonnet 分别用了多少,尤其是 opus ,你们都是混在一起显示计费的。
    mogutouer
        8
    mogutouer  
       151 天前
    ccusage 目前有这个,但无法看到具体模型的消耗,是合并的总值。至于今天用了多少昨天用了多少无所谓,重要的就是这 5 个小时的 block 用了多少。

    keakon
        9
    keakon  
    OP
       151 天前
    @mogutouer 刚研究了一下 ccusage ,有几个发现:
    1. 遇到配额用完时,Claude 会记录一条类似这样的消息:Claude AI usage limit reached. Your usage will reset at |1705334400|
    这是我猜测的,不一定是这样的文本,最好你能在 ~/.claude/projects 里搜索下,给我一条 JSON 作为例子。
    2. 剩余用量的计算是基于历史统计的。它把历史所有的 session blocks 的开销都统计出来,计算一个最大成本。然后计算当前 session block 的成本,来估算百分比。
    这个算法可能会出现误差:
    1. 轻度使用,一直没遇到 "Claude AI usage limit reached",导致可能统计了很长的一个 session block 。
    2. 更换了套餐,和历史数据不一致。
    3. Claude 官方也没说它们是这样限额的,并且 Claude 和 Claude Code 是共用配额的,计算会遗漏。
    我感觉既然都是估算的,不如直接参数里设置一个消息数上限,这才是 Claude 官方承认的。
    mogutouer
        10
    mogutouer  
       151 天前
    @keakon #9
    Claude AI usage limit reached. Your usage will reset at 这个消息我知道,但当我看到的时候已经用完了,另一个提示是 Claude Opus 4 limit reached, now using Sonnet ,表示 opus 额度用了此次 block 的 50%了。
    我就想在 opus 用量 50%之前做一些规划,但现在当前的 tokens 消耗都是合并的,但其实里面有分开每个模型用了多少,我希望能把这个信息显示出来,让我可以在这 5 小时的 block 里在额度之前做一些规划。

    ccusage daily --json 里有他解析之后汇总的详细的数据

    因为我没办法很容易的看到每个模型用了多少,都是今天汇总显示的,所以没办法估多少才是 opus ,或者一个 block 的限额是多少,如果可以看到的话,我可以进行统计真实具体的数字给你。
    keakon
        11
    keakon  
    OP
       150 天前
    @mogutouer 我大概想了一下方案:
    1. 从当前时间往前推,一直找到以下三种情况:
    1. 出现 Claude AI usage limit reached
    2. 出现 5 小时的 gap
    3. 找到第一条消息
    后两种情况从这条消息开始,每隔 5 小时切一次 block
    2. 根据用户传入的最大 block 消息数来计算用量,并以 5:1 的额度计算当前 block 各模型的剩余消息数
    3. 如果当前 block 内还有 Claude Opus 4 limit reached ,则将该消息之前的消息数( opus 算 5 条)的两倍当成最大 block 消息数来计算

    如果这个方案没问题的话,还需要你提供一下包含 Claude AI usage limit reached 和 Claude Opus 4 limit reached 的 jsonl 文件中的 2 条 JSON 示例,以便更好地进行匹配
    mogutouer
        12
    mogutouer  
       150 天前
    @keakon #11

    以下是出现 Claude Opus 4 limit reached, now using Sonnet 4 时的 json 数据

    ```
    {
    "date": "2025-07-29",
    "inputTokens": 3234,
    "outputTokens": 21535,
    "cacheCreationTokens": 2557523,
    "cacheReadTokens": 27695237,
    "totalTokens": 30277529,
    "totalCost": 70.92085875000001,
    "modelsUsed": [
    "claude-sonnet-4-20250514",
    "claude-opus-4-20250514"
    ],
    "modelBreakdowns": [
    {
    "modelName": "claude-opus-4-20250514",
    "inputTokens": 2425,
    "outputTokens": 7478,
    "cacheCreationTokens": 1887563,
    "cacheReadTokens": 19914687,
    "cost": 65.86106175000003
    },
    {
    "modelName": "claude-sonnet-4-20250514",
    "inputTokens": 809,
    "outputTokens": 14057,
    "cacheCreationTokens": 669960,
    "cacheReadTokens": 7780550,
    "cost": 5.059796999999999
    }
    ]
    }
    ```
    keakon
        13
    keakon  
    OP
       150 天前
    这里面没有消息,但是带了 modelBreakdowns 字段?
    而且格式好像完全不一样,正常的类似于:
    ```
    {
    "parentUuid": "5bc1ef43-d2fe-414e-8861-5f7a54baf34e",
    "isSidechain": false,
    "userType": "external",
    "cwd": "xxx",
    "sessionId": "11037184-db0d-49b3-a7d0-543313ada073",
    "version": "1.0.60",
    "gitBranch": "main",
    "type": "assistant",
    "timestamp": "2025-07-25T17:14:43.936Z",
    "message": {
    "id": "msg_01B4usYpDjtc2jurnoKfJbo7",
    "type": "message",
    "role": "assistant",
    "model": "claude-sonnet-4-20250514",
    "content": [
    {
    "type": "tool_use",
    "id": "toolu_01CeTbAM3nqq4d5xJLrDiNRR",
    "name": "TodoWrite",
    "input": {
    "todos": [
    {
    "content": "检查 main.py 中的时间处理逻辑",
    "id": "1",
    "priority": "high",
    "status": "completed"
    },
    {
    "content": "运行程序查看当前的时间显示和成本计算",
    "id": "2",
    "priority": "high",
    "status": "in_progress"
    },
    {
    "content": "分析时区转换是否正确处理了日期边界",
    "id": "3",
    "priority": "medium",
    "status": "pending"
    }
    ]
    }
    }
    ],
    "stop_reason": null,
    "stop_sequence": null,
    "usage": {
    "input_tokens": 5,
    "cache_creation_input_tokens": 14959,
    "cache_read_input_tokens": 18127,
    "output_tokens": 331,
    "service_tier": "standard"
    }
    },
    "requestId": "req_011CRU62x5fSHHj7NnnQV4Ng",
    "uuid": "84514a0c-f6de-4ec8-a1dc-6d60c1a001d3"
    }
    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5290 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 09:00 PVG 17:00 LAX 01:00 JFK 04:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86