Claude Codeのセッション別トークン使用量:session-costスキル

Dmitriy Solodukha、Caprock開発者 · 公開日

にClaude CodeとCodexでテスト済み

Claude Codeのセッション別トークン使用量と要因を確認。session-costはCaprock、/usage、ローカルログを読み、料金を推測しません。

このセッションと最近のセッションのClaude Codeトークン使用量や、多くなった理由を知るには、session-costスキルを入れたエージェントに尋ねます。Caprockのcostツール、エージェント自身の使用量コマンド、小さなスクリプトで読むローカルログのいずれかから数値を取得します。主な原因を1つか2つ挙げ、金額は出典を明示できる場合だけ示します。

session-costスキルをインストールする

エージェントごとに1コマンド。スキルはSKILL.md 1ファイルと、コマンドが併せて導入するスクリプトscripts/session_tokens.pyです。インストール時には何も実行しません。

9スキルをプラグインとして導入します。Claude Code内で次を入力してください:

このスキルだけを導入する場合は、プロジェクトのフォルダーで次を実行します:

全プロジェクトに導入するには -g を追加します。

session-costスキルを使うプロンプト

依頼の内容が説明と合うと、エージェントがスキルを選びます。名前を指定すると確実です。プロンプトをクリックしてコピーできます。

session-costがClaude Codeのトークン集計に読む内容

  • Caprockのcostツールは、接続時に、リポジトリ1つまたは全体について日別・モデル別のトークン数と金額を示します。金額はAPI定価換算なので、定額プランでは実際の請求額ではなく、同じ使用量をAPIで使った場合の金額です。スキルはその点を明記します。
  • エージェント自身のコマンドを自分で入力します。Claude Codeは/usage(/costと/statsは別名、/contextはコンテキストの内訳)、Codexは/statusと/usageです。
  • ローカルログをscripts/session_tokens.py(Python標準ライブラリのみ使用、オフライン)で読みます。現在のフォルダーについて、サブエージェントを含む~/.claude/projects/内のClaude Codeトランスクリプトと、~/.codex/sessions/内のCodexロールアウトを対象にします。トークン数だけで、金額には換算しません。

数値を押し上げた要因を挙げます。毎回の大きなコンテキストの再読、多数の呼び出し(ポーリングなど)、各自のコンテキストを持つサブエージェント、長い出力、単純な作業での高価なモデルなどです。それぞれ具体的な次の対処を示します。

実行したテストとエージェントの動作

各スキルは2026年11月29日に、専用の使い捨てリポジトリへプロジェクトスキルとしてインストールして実行しました。calc.pyを持つ使い捨てリポジトリで、短いClaude Codeセッションにより関数を追加し、実際の使用量を読めるようにしました。Codexの検証は、同じフォルダーでの短いCodexセッションとClaude Codeのコスト検証の後に行い、3セッションを集計しました。

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.

スキルを読み込み、Caprockのcostとrecent_sessionsを呼び、ログスクリプトを実行しました。Caprockの金額(プロジェクトの2セッションで約$0.17〜$0.18、API定価換算、料金表のバージョン付き)を示し、定額プランの請求額ではないと説明しました。主因はキャッシュ読み取り(トークンの82%)とし、セッション別の2つの数値の合計がプロジェクト合計を超えることも指摘し、原因は不明としました。

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.

スキルを読み、Caprockのcostとログスクリプトを使いました。$0.23、338,504トークン、うち85% がキャッシュ読み取りでした。定額プランの金額に関する同じ注意も添えました。

検証したエージェント

  • Claude Code:2026年11月29日に、.claude/skills/にプロジェクトスキルとしてインストールし、claude -p --model sonnetで検証しました。
  • Codex:2026年11月29日にCodex 0.163.0(codex exec)、.agents/skills/内のスキルで検証しました。
  • OpenCodeとCursor:未検証です。オープンなAgent Skills形式の通常の指示ですが、どちらでも実行して確認していません。

制限

  • Caprockなしの手順(使用量コマンド、その後スクリプトのみ)は、実際のテストでは実行していません。
  • OpenCode・Cursorの使用量コマンドは、未検証のため掲載していません。
  • スクリプトはトークンを数えるだけで、金額には換算しません。金額にはCaprock、/usage、または指定した単価が必要です。
  • エージェント自身はスラッシュコマンドを入力できません。/usageや/statusの場合は、どれを入力すべきかを案内します。

Caprockがある場合とない場合

Caprock MCPサーバーに接続している場合、スキルはcost(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スキルについての質問

Claude Codeの1セッションのトークン使用量を確認するには?
Claude Codeで`/usage`(別名`/cost`)を入力するか、session-cost導入済みエージェントに尋ねます。Caprockまたはローカルのトランスクリプトから、入力、キャッシュ書き込み、キャッシュ読み取り、出力に分けて集計します。
Claude Codeのトークン使用量が多いのはなぜ?
長いセッションでは通常、キャッシュ読み取りが原因です。毎回コンテキスト全体を再読するため、容量と呼び出し回数に応じて費用が増えます。スキルは割合を示し、圧縮または引き継ぎ付きの新規セッションを提案します。
session-costスキルは請求額を表示しますか?
定額プランでは請求額ではありません。CaprockとClaude Codeのセッション単位の値はAPI定価換算です。Pro・Maxでは同じ使用量のAPI利用時の推定費用であり、スキルが区別を明記します。
ログを外部に送りますか?
送りません。スクリプトはPython標準ライブラリでローカルファイルを読み、ネットワーク通信しません。料金ページも依頼なしでは取得しません。

出典

アプリ本体の表示言語は英語です。