Paste this into Claude Code on your machine. It is self-contained. (Or skip it and run the included token-audit.mjs directly: node token-audit.mjs 2026-08-18.)
Audit my Claude Code token usage and price it at Anthropic API list rates.
Data: every *.jsonl under ~/.claude/projects/ (recursive). Each line is JSON.
Only count lines where type == "assistant" and message.usage exists.
Dedup by message.id + ":" + requestId (skip repeats).
Filter by timestamp to the window I give you.
Per request, from message.usage:
- read = cache_read_input_tokens
- w1h = cache_creation.ephemeral_1h_input_tokens (fallback: cache_creation_input_tokens)
- w5m = cache_creation.ephemeral_5m_input_tokens
- out = output_tokens
- inp = input_tokens
Prices ($ per million tokens), matched by model prefix:
claude-fable-5-1 / claude-fable-5: input 10, output 50, cache read 0.25
claude-opus-5 / claude-opus-4-8 / -4-7 / -4-6: input 5, output 25, cache read 0.5
claude-sonnet-5: input 2, output 10, cache read 0.2
claude-sonnet-4-6: input 3, output 15, cache read 0.3
claude-haiku-4-5: input 1, output 5, cache read 0.1
unknown model -> use opus-5 prices
Cache write cost = (w1h * 2 + w5m * 1.25) * input price.
Also compute:
- human prompts = lines with type == "user" whose message.content is a string, or an array with no tool_result block
- calls per prompt = requests / human prompts
- median request size = median of (read + w1h + w5m + inp); % of requests over 200k
- cold resumes = per file, consecutive assistant requests more than 1 hour apart (count them and their cache-write cost)
- subagent share = cost from files under a /subagents/ folder or whose name contains "agent-"
- cost per model
Output JSON: window, requests, sessions, humanPrompts, callsPerPrompt, costUSD, perDayUSD, per30dUSD,
share % of cost for {cacheReads, cacheWrites, output, typedInput}, medianRequestTokens,
pctRequestsOver200k, coldResumes, coldResumeWriteUSD, subagentShare, byModel.
Write it as ~/.claude/scripts/token-audit.mjs taking args [SINCE] [UNTIL] (YYYY-MM-DD),
run it for the last 30 days, save output to ~/.claude/reports/, and show me a short table:
cost/30d, the four cost shares, median request size, % over 200k, calls per prompt, cold resumes.
| Metric | Before hygiene rules | After |
|---|---|---|
| Cost / 30d (API-rate equivalent) | $8,631 | $2,390 |
| Cache reads / writes / output | 64 / 26 / 10% | 34 / 45 / 21% |
| Median request | 175k tokens | 110k |
| Requests over 200k | 44% | 4% |
| Calls per prompt | 7.8 | 6.4 |