Claude Code 每个会话的 token 用量:session-cost skill

Dmitriy Solodukha,Caprock 开发者 · 发布于

测试于 (Claude Code 和 Codex)

查看 Claude Code 每个会话的 token 用量及其来源。session-cost skill 读取 Caprock、/usage 或本地日志,绝不编造价格。

要查看当前及近期 Claude Code 会话的 token 用量,以及用量偏高的原因,安装 session-cost skill 后向智能体提问。它会从保存用量数据的来源读取数字(Caprock 的 cost 工具、智能体自己的用量命令,或通过小脚本读取本地会话日志),指出一两个主要原因,并且只报告有明确来源的美元金额。

安装 session-cost skill

每个智能体只需一条命令。skill 由一个 SKILL.md 和脚本 scripts/session_tokens.py 组成,安装命令会一并安装脚本;安装时不会运行任何内容。

将全部九个 skill 作为插件安装。在 Claude Code 中输入以下内容:

也可以只安装这个 skill,在终端中进入项目文件夹后运行:

添加 -g 即可为所有项目安装。

使用 session-cost skill 的提示词

你的措辞与 skill 描述匹配时,智能体会选择它;直接指定 skill 名称更有把握。点击提示词即可复制。

session-cost skill 从哪里读取 Claude Code token 用量

  • Caprock 的 cost 工具,连接后可用:按天、按模型显示单个仓库或所有仓库的 token 用量和美元金额。金额按 API 标价计算,因此对于固定费用套餐,它表示相同用量通过 API 产生的费用,并非实际账单;skill 会说明这一点。
  • 智能体自己的命令,由你输入:Claude Code 中使用 /usage(/cost 和 /stats 是别名,/context 显示上下文窗口由哪些内容占用),Codex 中使用 /status 和 /usage。
  • 本地日志,通过 scripts/session_tokens.py(仅用 Python 标准库,离线运行):读取当前文件夹对应的 ~/.claude/projects/ 下的 Claude Code 对话记录(包括子智能体),以及 ~/.codex/sessions/ 下的 Codex rollout 记录。只统计 token,不计算费用。

然后 skill 会指出数字背后的原因:每次调用都读取大段上下文产生的缓存读取、大量调用(通常是轮询循环)、各自建立上下文的子智能体、过长的输出,或用更昂贵的模型处理常规工作,并为每种原因给出一个具体的下一步。

我们运行了什么,智能体做了什么

每个 skill 都于 2026年11月29日在为其创建的临时仓库中运行,以项目 skill 的形式安装。这个临时仓库包含 calc.py,之前已运行过一个简短的 Claude Code 会话,为其添加了一个函数,因此有真实用量可供读取。Codex 测试在之后进行,此前在同一文件夹中运行了一个简短的 Codex 会话,也完成了 Claude Code 成本测试,因此共统计了三个会话。

Claude Code (claude -p --model sonnet)

How much have my Claude Code sessions in this project cost so far? Show the token usage and what drove it.

加载了 skill,调用 Caprock 的 cost 和 recent_sessions,然后运行日志脚本。它给出了 Caprock 中的美元金额(项目的两个会话按 API 标价计算约为 $0.17 至 $0.18,并注明了 Caprock 的定价版本),说明在固定费用套餐下,这并非实际账单金额,并指出缓存读取(占 token 的 82%)是主要原因。它还指出,两个会话各自的金额相加超过了项目总额,并表示尚未找到原因。

Codex (0.163.0,codex exec)

How much have my coding-agent sessions in this project cost so far? Show the token usage and what drove it.

读取了 skill,使用 Caprock 的 cost 和日志脚本:$0.23 和 338,504 个 token,其中缓存读取占 85%,同样需要注意固定费用套餐下的估算限制。

已测试的智能体

  • Claude Code:测试于 2026年11月29日,使用 claude -p --model sonnet,skill 作为项目 skill 安装在 .claude/skills/ 中。
  • Codex:于 2026年11月29日使用 Codex 0.163.0(codex exec)测试,skill 位于 .agents/skills/。
  • OpenCode 和 Cursor:未测试。此 skill 是以开放的 Agent Skills 格式编写的纯文本指令,但尚未在这两者上实际运行验证。

限制

  • 测试中没有实际运行不使用 Caprock 的流程(先用用量命令,再单独运行脚本)。
  • 这里未列出 OpenCode 和 Cursor 的用量命令,因为没有核实过任何此类命令。
  • 脚本只统计 token,从不换算成金额。计算金额需要 Caprock、/usage 或你提供的费率。
  • 智能体无法自行输入斜杠命令:遇到 /usage 或 /status 时,它会告诉你该输入哪一个。

使用和不使用 Caprock 的情况

连接 Caprock MCP 服务器后,skill 会调用 cost(按天和模型统计 token 数及美元花费,按 API 标价计算)和 recent_sessions,列出某天对应的会话。未连接时,则使用智能体自身的命令和日志脚本。Caprock 是一款免费开源应用,记录你电脑上的每个编程智能体会话。

获取 Caprock →

完整的 SKILL.md

这是 dspv/skills 中修订版本 dfce799 的文件原样。手动安装时,将它保存在项目中:Claude Code 使用 .claude/skills/session-cost/SKILL.md,Codex 使用 .agents/skills/session-cost/SKILL.md。

skills/session-cost/SKILL.md
---
name: session-cost
description: >-
  Tells you what the current coding-agent session, and recent ones, cost in tokens and
  (when a real price source exists) dollars, and what drove it: cache reads from a large
  context, many tool calls, subagents. Use when you ask "how much did this session cost",
  "claude code token usage", "how many tokens have I used", "why is this session so
  expensive" or "cost per session". Reads Caprock's cost tool when present, otherwise the
  agent's own usage command and local session logs; never invents a price.
license: Apache-2.0
---

# Session cost

Answer "what did this cost and why" from a source that holds the numbers. Never estimate a
figure from memory, never invent a price per token.

## Pick the source, best first

1. **Caprock `cost` tool** (MCP server `caprock`). If it is in your tool list, call it:
   `cost` with `days` (1 for today, default 7, at most 90), optional `repo` (a directory in
   the repository) or `all_repos: true`. It returns tokens and `cost_usd` per day and per
   model. Its own note says it prices at API list rates from Caprock's model table, so on a
   flat plan the dollars are what the same usage would cost on the API, not what was
   billed. Say that. If `recent_sessions` is also there, use it to name the sessions behind a
   day. (Caprock, https://caprock.dev, is free, open source and runs locally; it is what
   provides these tools.)
2. **The agent's own command.** You cannot run a slash command yourself; tell the user which
   one to type:
   - Claude Code: `/usage` ("Show session cost, plan usage limits, and activity stats";
     `/cost` and `/stats` are aliases). `/context` shows what fills the context window.
     Source: https://code.claude.com/docs/en/commands, read 2026-11-29. On Pro, Max and other
     plans the docs say the session cost figure "isn't relevant for billing purposes"
     (https://code.claude.com/docs/en/costs).
   - Codex: `/status` ("Display session configuration and token usage") and `/usage` ("View
     account token usage"). Source: https://learn.chatgpt.com/docs/developer-commands, read
     2026-11-29.
   - Other agents: only a command you found in their docs or `--help`. If none, skip.
3. **Local session logs**, read with the script beside this file (offline, standard library,
   prices nothing):

   ```bash
   python3 <this skill's folder>/scripts/session_tokens.py              # 5 newest, this dir
   python3 <this skill's folder>/scripts/session_tokens.py --session ID # one, with drivers
   python3 <this skill's folder>/scripts/session_tokens.py --json --last 10
   ```

   It reads Claude Code transcripts (`~/.claude/projects/<cwd as slug>/*.jsonl`, subagents
   under `<session>/subagents/`) and Codex rollouts (`~/.codex/sessions/**/rollout-*.jsonl`)
   whose working directory is `--cwd` (default: the current one). If the logs are missing
   or unreadable, say so; do not guess. The current session is usually the newest row; in
   Claude Code its id is also the transcript file name.

Use the first source that works. Use a second one only to cross-check or to explain.

## Dollars: only from a named source

- Caprock `cost` and Claude Code `/usage` give dollars: quote them with their source.
- From the log script you get tokens only. Turn them into dollars only with rates the user
  gives you or a provider pricing page already read in this session; then name the page or
  the user's rates and the date, and price each kind separately (input, cache write, cache
  read, output differ). Otherwise answer in tokens and say why there is no dollar figure.
- Do not fetch a pricing page unless the user asks you to go online.

## What drove it

Read the numbers, then name the one or two biggest causes, with the figure behind each:

- **Cache reads dominate** (usual for long Claude Code sessions): every call re-reads the
  whole context, so cost grows with context size times number of calls. Signs: cache read
  far above everything else, large calls near the context limit. Remedy: compact or start a
  fresh session with a handoff note.
- **Many calls**: a high `api calls` count with a long tool-call list (often a shell loop or
  polling). Remedy: batch commands, stop polling.
- **Subagents**: the subagent line of the script, or a model split in Caprock `cost`. Each
  subagent starts its own context. Remedy: fewer, narrower subagents.
- **Output** heavy: long generated files or reasoning. Usually the smallest share.
- **Model mix**: a costlier model on routine work (`by_model` in Caprock, `models` in the
  script).

Note the counting difference: in Codex logs `input_tokens` include cached tokens (the script
splits them out); in Claude Code logs they do not.

## Output

```
Session <id or "current"> (<agent>, <model>), <start> to <end>
Tokens: <total>  (input <n>, cache write <n>, cache read <n>, output <n>; subagents <n>)
Cost: $<x> from <source, date>   |   Cost: tokens only (<why no price>)
Recent: <one line per session or day: date, tokens, $ if sourced>
Drivers: 1. <cause, with its number>  2. <cause>
Next: <one concrete step, or "nothing to change">
```

Stop when every figure has a source. If no source works, say which ones you tried and what
the user can run (`/usage`, `/status`) to see it.

关于 session-cost skill 的问题

如何查看 Claude Code 单个会话的 token 用量?
在 Claude Code 中输入 `/usage`(`/cost` 是别名),或询问具备 session-cost skill 的智能体。它会读取 Caprock 或本地对话记录,将总量拆分为输入、缓存写入、缓存读取和输出。
为什么我的 Claude Code token 用量这么高?
长会话中,成本通常主要来自缓存读取:每次调用都会重新读取整个上下文,因此成本随上下文大小与调用次数的乘积增长。skill 会显示这部分占比,并建议压缩上下文,或通过交接记录开启新会话。
会话成本 skill 显示的是我实际支付的费用吗?
固定费用套餐不是。Caprock 和 Claude Code 中的会话金额都按 API 标价计算用量成本;对于 Pro 或 Max,这个数字表示同样的用量通过 API 使用时的费用。skill 会说明属于哪种情况。
它会把我的日志发送到其他地方吗?
不会。脚本用 Python 标准库读取本地文件,不发起网络调用;除非你要求,skill 也不会获取价格页面。

来源

应用本身是英文界面。