Skip to main content

Aggregate and analyze Claude Code token consumption from local JSONL transcripts

Project description

claude-token-usage

CLI tool that aggregates and displays Claude Code token consumption. Scans ~/.claude/projects/ JSONL transcripts, estimates costs using live LiteLLM pricing data, and prints everything in a color-coded terminal table.

Installation

pip install tokstat

Requires Python 3.10+. No dependencies.

Usage

claude-token-usage                        # overview for today
claude-token-usage --period all           # all time
claude-token-usage --period "7 days"

Modes

Default — aggregated overview

Displays consumption by period, by project, by model, output speed, and grand total.

claude-token-usage
claude-token-usage --period all

--prompts — per-exchange detail

Per-exchange breakdown: user text, model, turns, tokens (input/output/cache), tool calls, cost.

claude-token-usage --prompts
claude-token-usage -p --period "7 days"

--audit — behavioral anti-pattern detection

Scans assistant transcripts for 11 categories of behavioral anti-patterns (French + English). Summary tables show breakdown by category and by model with incident rates.

claude-token-usage --audit
claude-token-usage -a --period "30 days"
Abbr. Category What it detects
Gaslt Gaslighting contextuel Denying previous statements, rewriting history
Anthr Anthropomorphisme / fausse empathie False emotions, fake experience claims
Hedge Dilution par prudence Dense hedging clusters in a single sentence
Lazy Paresse intellectuelle Deflecting to docs, generic non-answers, filler
Overc Aplomb trompeur Confident assertions followed by tool errors
Sycop Flagornerie / sycophancy Excessive praise, performative agreement
Compl Acquiescement performatif "You're right, but..." patterns
Prem. Solution prematuree Declaring victory before verification
Loop Boucle d'echec User reports same failure 3+ times
Verb. Verbosite creuse Long structured response to short question
FakeU Comprehension feinte "I understand" without addressing the issue

--anomalies — technical anomaly detection

Detects unusual patterns in per-exchange token data. Results grouped by project.

claude-token-usage --anomalies
claude-token-usage --anomalies --period "30 days"
Anomaly Trigger Severity
Runaway cost Prompt costs 10x+ the P90 HIGH
High cost Prompt costs 5x+ the P90 MEDIUM
Tool storm 30+ tool calls in a single prompt HIGH >60, MEDIUM >30
Turn spiral API turns 5x+ the P90 HIGH >10x, MEDIUM >5x
Cache thrashing High cache writes with <50% read-back MEDIUM
Context bloat Input/output ratio >50:1 with >10K input LOW
Empty exchange 5+ turns but <100 output tokens MEDIUM

Thresholds are computed dynamically from your own data (median, P90).

--plan — plan & optimization recommendations

Cost breakdown by model, plan recommendation, and data-driven optimization advice.

claude-token-usage --plan
claude-token-usage --plan --period all
  All time — 17 active days / 30

  Model              Calls     Cost   Avg/day  Projected/mo  Cache  Share
  ─────────────────  ─────  ───────  ────────  ────────────  ─────  ─────
  claude-opus-4-6      321  $475.19  $15.84/d    $475.19/mo    96%   100%
  claude-sonnet-4-6      8   $0.811  $0.027/d     $0.811/mo    96%     0%
  TOTAL                329  $476.00  $15.87/d    $476.00/mo    96%

  Plan (based on All time)
    Max 20x ($200/mo) strongly recommended.
    Projected API cost: $476.00/mo — you'd save ~$276.00/mo

  Optimization Recommendations

  Model selection
    100% of spend is on claude-opus-4-6. claude-sonnet-4-6 is 2x cheaper.
      - Use claude-sonnet-4-6 for simple tasks
      - Switching 30% would save ~$49.78/mo

--export — conversation export

Exports all exchanges to a JSON file.

claude-token-usage --export
claude-token-usage --export out.json --period "7 days"
{
  "tool": "Claude Code",
  "model": "claude-opus-4-6",
  "timestamp": "2026-04-08T...",
  "user": "the user prompt text",
  "assistant": ["response 1", "response 2"],
  "turns": 25,
  "tools_used": {"Bash": 3, "Read": 7, "Edit": 2},
  "tool_errors": ["error message"]
}

Filters

All modes support --period:

--period <period>    all, hour, "5 hours", today, yesterday, "7 days", "30 days", year
                     default: today  partial match works ("7" = "Last 7 days")

Pricing

Model pricing is fetched from LiteLLM's model pricing database and cached locally at ~/.cache/token-usage/litellm_prices.json for 24 hours. Falls back to stale cache if fetch fails.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokstat-1.0.2.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokstat-1.0.2-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file tokstat-1.0.2.tar.gz.

File metadata

  • Download URL: tokstat-1.0.2.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.1

File hashes

Hashes for tokstat-1.0.2.tar.gz
Algorithm Hash digest
SHA256 68c4c2785ca4f176f59111208d8ce32751e1224aa3955a8a4dc7ee45aa9a6fa5
MD5 9189628a856cb0532b273c4f583fadca
BLAKE2b-256 b5926cce27563ea178234303c0076fc23296ff7058e53b3d82742a434043fd7c

See more details on using hashes here.

File details

Details for the file tokstat-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: tokstat-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.1

File hashes

Hashes for tokstat-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 259a31ba5c3f142a8047c7b77b5a255aba0a3619efdba80be315999a2d1fd28f
MD5 1af94c42d95187d6f6b0bc6461ff2e00
BLAKE2b-256 a73d2ae7dcc0bc643609a15d94e0564ff70e028e82fe1591d8fc8141466fc8b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page