Skip to main content

See where your AI coding tokens actually go. Analyze Claude Code session costs, find token bombs, and track spending.

Project description

TokenXRay icon

TokenXRay

See where your AI coding tokens actually go.

I spent $104 in a single Claude Code session. Then I audited all 514 of mine and found that 9% of sessions burned 92% of the money — $11,600 out of $12,600 total. The culprit: context that grows quadratically, cache creation fees nobody mentions, and tool results that ride in context forever.

TokenXRay — The Problem and Solution

Install

pip install tokenxray
tokenxray --install-hook --confirm   # one-time setup, then forget about it

Zero dependencies. Pure Python stdlib. Python 3.9+.

How It Works

TokenXRay has two layers: hooks that run automatically inside Claude Code, and a CLI you run when you want to review your spending.

Daily: Hooks (automatic, zero effort)

After --install-hook, every Claude Code session gets two hooks that run silently in the background:

  1. Cost hook — tracks your running cost after every tool use. Shows a status line every 10 turns. Alerts when you cross $10/$25/$50/$100. At 80 turns or $30, auto-saves your session state to .claude/checkpoint.md.
  2. Resume hook — when you start a new session, detects the checkpoint and restores context automatically. One-shot: fires once, then gets out of the way.

Your daily workflow:

Open Claude Code → checkpoint detected? context restored automatically
       ↓
Work normally → cost hook tracks silently in background
       ↓
Hit 80 turns or $30 → checkpoint auto-saved
       ↓
You decide: keep going or start fresh (checkpoint is saved either way)
       ↓
Start fresh → next session picks up where you left off

You never run tokenxray during a session. The hooks handle it.

[TokenXRay] Opus — turn 40, $12.50 total, ~$0.31/turn, ctx 85K
[TokenXRay] Consider splitting this session! (80 turns, $31.20, ctx 120K)
[TokenXRay] Auto-checkpoint saved to .claude/checkpoint.md

Weekly: CLI (manual review)

Run these when you want to understand your spending patterns and change habits:

tokenxray                  # Overview — where your money goes
tokenxray --diagnose       # Specific recommendations
tokenxray --session <id>   # Deep dive into one session
tokenxray --dashboard      # Interactive HTML charts
tokenxray --projects       # Cost by project
TokenXRay - Session Overview
----------------------------------------------------------------------
  514 sessions    43,000+ total turns    $12,600+ total cost

  Segment Breakdown:
    1-10 turns:  329 sessions  avg  $0.19   total    $62   ░░░░░░░░░░  0%
         11-30:   76 sessions  avg  $4.01   total   $305   ░░░░░░░░░░  2%
        31-100:   61 sessions  avg $11.05   total   $674   █░░░░░░░░░  5%
          100+:   48 sessions  avg   $241   total $11,600  ████████░░ 92%

The retrospective analysis is the most valuable part. After a few --diagnose runs, you start naturally scoping sessions better — "I'll do the refactor, then start fresh for tests." That's where the real savings come from.

Configuration

Customize hook thresholds in ~/.tokenxray/config.json:

{
    "split_turns": 80,
    "split_cost": 30,
    "alert_thresholds": [10, 25, 50, 100, 200, 500],
    "status_interval": 10
}

Supported Tools

Tool Source Notes
Claude Code ~/.claude/projects/**/*.jsonl Full token breakdown: input, output, cache read, cache create
Gemini CLI ~/.gemini/tmp/*/chats/session-*.json Input, output, cached, thinking tokens
GitHub Copilot VS Code workspace storage Estimated from message lengths (token events are ephemeral)
tokenxray --source claude    # Claude only
tokenxray --source gemini    # Gemini only
tokenxray --source copilot   # Copilot only
tokenxray --source all       # Everything (default)

Your data stays local. TokenXRay reads files on your machine. Nothing is sent anywhere.

Additional Flags

Flag Description
--top N Show top N sessions (default: 15)
--path <dir> Custom path to session logs directory
--no-color Disable colored output
--baseline / --compare Save baseline, compare after changing habits
--export csv Export sessions to CSV
--checkpoint Manually extract session state

The Full Story

Read the detailed analysis: I Spent $104 in a Single AI Coding Session. Then I Audited All 514 of Mine.

License

MIT

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

tokenxray-0.2.0.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

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

tokenxray-0.2.0-py3-none-any.whl (37.9 kB view details)

Uploaded Python 3

File details

Details for the file tokenxray-0.2.0.tar.gz.

File metadata

  • Download URL: tokenxray-0.2.0.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tokenxray-0.2.0.tar.gz
Algorithm Hash digest
SHA256 332307bd2218a73683c97bf6527c4649b3aa1e74a0abe66d6c5671e6cb574215
MD5 3a8b8755f45868261f727609e23a67c9
BLAKE2b-256 935c4bc81ef728380a2388bfd76dbfc2246e74ef41e85eb310115c3ef9981c49

See more details on using hashes here.

File details

Details for the file tokenxray-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenxray-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 37.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tokenxray-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7beca95b5fcbf66b09f0bb0a103e281a90d74e5a3e98bc2cad241c6cd23e2b96
MD5 aa2bdfb2784eda5562f5fc33a114777b
BLAKE2b-256 4c2dbffb07e32c7ca667ffe6bab035a24669808951205bc4b99fcd2a990c8fa6

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