tokenbill is a local command-line tool for analyzing the cost of Claude Code sessions, primarily solving the question of "where the context tokens are being spent." Claude Code resends the entire conversation with every request; tokenbill reads the session logs already on disk and shows the composition of the context: the conversation itself, command and tool output, file reads, replies, summaries, etc. The tool makes no network requests and requires no configuration or account. API users can add the --cost flag to view dollar costs; subscription users can view tokens and real plan usage.
Main capabilities
- Context composition analysis: using an incremental-difference heuristic, it classifies the tokens added by each request and splits the entire context according to the composition ratio during the run, so that the sum of all categories always equals the total.
- Session and project reports: running in a project directory produces a report for the entire project, including per-session totals, merged categories, and the largest turn across all sessions; running elsewhere falls back to the most recent session.
- Compaction events: shows when the context was summarized mid-session, what was resent, and how many historical tokens were discarded.
- Cache efficiency: hit rate and mid-session cache rebuilds (a sign of a broken prompt prefix).
- Largest turns: highlights the moments that moved the most context, and labels the user's input prompt and the agent's response (--redact keeps only the latter).
- Subagents: in project and --all reports, shows the share of subagents in the total and the largest run, and labels their respective tasks (--redact shows the log id).
- Suggestions: actionable findings, such as cache prefix breaks, sessions with excessive file reads, heavy compaction, and low hit rates.
- Daily trend: a sparkline for the last 14 days with a day-by-day breakdown (project report).
- --cost: presents the same report in API-equivalent US dollars, including per-model pricing and effective dates, 0.1× cache reads, 5-minute (1.25×) or 1-hour (2×) cache write premiums, $10 per 1,000 web searches, the dollar amounts of cache savings and rebuild waste, and a "potential savings" estimate.
- Markdown report: --md renders the report as GitHub tables, suitable for PR comments and task summaries.
- Shareable HTML report: --html writes a single self-contained dark-theme HTML file (no external requests, sortable turn table).
- Card image: tokenbill card turns the numbers into a 120A 0×630 layout image (PNG rendered at 2× as 2400×1260, SVG as 1200×630), showing total tokens, a spectrum band of where the context went, the largest turn, and the average request size, and printing explanatory text that can be pasted directly; --redact hides prompt content.
- Status line: tokenbill statusline reads the JSON that Claude Code passes to the status line command and outputs one line: this session's tokens, today's project tokens, plan usage, cache hit rate, and a warning after a cache prefix break. The 5-hour and weekly figures come from the rate_limits field that Claude Code sends to Pro and Max subscribers; when that field is missing (API key or before the first response) it is not shown, and tokenbill never converts token counts into plan percentages.
- Budget guard and GitHub Action: --budget switches the report to dollars and prints it in full, exiting with code 2 when over budget (0 normal, 1 error, 2 over budget). CI runners do not have ~/.claude, so logs must be committed to the repository or downloaded as an artifact and pointed at; the built-in action writes the Markdown report to the job summary and applies the guard.
- --all: merges all sessions under ~/.claude/projects into a single report, including totals, per-session totals, model breakdown, daily trend, and the largest turn across projects; subagent logs are counted through the parent session, not counted again as separate sessions, and the largest subagent runs are listed separately. The budget applies to the entire recorded history, not a monthly quota; --all cannot be combined with a session or project path.
- Date filtering: --since and --until accept dates and relative durations (<n>d or <n>w, counted from today UTC, inclusive of the current day); an end date earlier than the start date is an error, identical dates select the full day; matching sessions are counted in full even if they span multiple days; sessions without a start time are excluded when filtering; when no sessions match, it exits with code 1 and gives an explanation.
Usage After installation, run it from the project directory to get a project report; running it elsewhere falls back to the most recent session. The status line must be installed globally (npx adds startup time to every refresh), then point to it in ~/.claude/settings.json; it only parses session files modified today, does not write to disk, and on any errorPrint tokenbill - and exit with 0.
Limitations and notes The total token count comes directly from the logs; the breakdown by category is a documented heuristic, not an exact accounting: tool output and file sizes are estimated by character count (images by pixel dimensions). Tokens are not a plan unit, and Anthropic has not published a mapping between tokens and subscription limits, so plan usage is only shown in the status bar as reported by Claude Code. --cost is an API-equivalent estimate; subscribers pay a flat fee, so it is not a real bill. Prices change, and the price table carries an asOf date (printed in each report footer), refreshed via npm run pricing:sync; rates have effective dates, and sessions are priced at the actual billed rate, including promotional windows; --pricing can override everything without waiting for a release. Models with no published rates are flagged rather than guessed: priced at the fallback Sonnet rate and labeled (~unknown model, sonnet rates). Fast mode is priced at a premium, and the batch service tier at a discount; the priority tier is billed at standard rates, so totals are underestimated if it is used. Server tool pricing assumes standard published rates; failed web searches are not billed by the API and do not appear in the logs as billed requests, so they are not counted. tokenbill never makes network calls; it only reads local files and prints text or writes the requested report/card files; reports quote prompts to help identify expensive turns, and that text stays on your machine unless you share the output; --redact removes it before sharing. Repository test fixtures are anonymized via scripts/anonymize.ts (equal-length placeholder text, preserving structure, token counts, and tool names). The same goes for pricing: scripts/sync-pricing.ts is run manually to refresh the price table, and the output is committed as source; the published package contains only dist and the card fonts, with no fetch path at runtime, no cache directory, and no opt-out option needed. Currently Claude Code is supported; the parser is an adapter interface (src/adapters/), and contributions of Codex/Cursor/Aider adapters are welcome. Type checking is split across three projects, all run in CI. Before changing prices, see CONTRIBUTING.md: src/cost/prices.generated.ts is a generated file, and manually curated facts should go in src/cost/pricing.overrides.json. Licensed under the MIT License.