Skip to content

2eeYuan/tokenwise

Repository files navigation

tokenwise  CN

Real-time token usage & cost tracker for Claude Code. Multi-provider pricing, 2700+ models.

Features

  • StatusLine — Real-time display in Claude Code's status bar: model, tokens, cache hit rate, cost, context usage
  • Multi-provider — Anthropic / DeepSeek / OpenAI / Gemini / MiMo / MiniMax / GLM / Kimi, 2700+ models
  • Smart pricing — Auto-detects pricing mode: Anthropic 4-tier, DeepSeek cache-hit, standard 2-tier, >200K tiered pricing
  • Project stats — Cost breakdown by project and session
  • JSONL import — Historical data from Claude Code's local logs
  • Live interception — Patches fetch on Unix/macOS for real-time API capture (Windows uses JSONL fallback)
  • One-click updateupdate-pricing pulls latest model pricing from LiteLLM

Install

git clone https://github.com/2eeYuan/tokenwise.git
cd tokenwise
npm install
npm run build

# Optional: install globally
npm link

Requires Node.js >= 20, better-sqlite3 (needs C++ build tools).

Quick Start

Step 1: Install the Status Bar

tokenwise setup

Restart Claude Code and the status bar appears at the bottom:

deepseek-v4-pro | In: 26.1K | Out: 6.8K | Cache: 100% hit | $0.0342 | ctx 6%

Fields: model name | input tokens | output tokens | cache hit rate | session cost | context window usage %.

Step 2: Import Historical Data (optional)

# All sessions
tokenwise import

# Specific session
tokenwise import --session <session-id>

# Verbose output
tokenwise import --verbose

Data source: Claude Code automatically writes session logs to ~/.claude/projects/<project>/<session>.jsonl.

Step 3: View Stats

# Current project
tokenwise stats

# All projects
tokenwise stats --all

# Specific session
tokenwise stats --session <session-id>

# JSON output (for scripting)
tokenwise stats --all --json

Example output:

Project: D--test-claude-prj-money-cal
──────────────────────────────────────────────────────────────────
  deepseek-v4-pro              42 reqs    156.2K in      8.4K out    $0.2340
  mimo-v2.5-pro                15 reqs     51.2K in       104 out    $0.0342
──────────────────────────────────────────────────────────────────
  Total                        57 reqs    207.4K in      8.5K out    $0.2682

Step 4: Keep Pricing Up-to-Date

tokenwise update-pricing

Fetches the latest pricing JSON from the LiteLLM community (updated near-daily).

Platform Support

Feature Unix / macOS Windows
StatusLine
tokenwise stats
tokenwise import
tokenwise update-pricing
Live interception (fetch patch)

On Windows, the status bar reads JSONL files directly for token counts. Functionally identical.

Unix/macOS: Use tokenwise as a wrapper for claude to inject the interceptor:

tokenwise                    # Launch Claude Code with live tracking
tokenwise -c "explain this"  # Pass arguments to claude
tokenwise --mc-verbose       # Print per-request token breakdown

Commands

Command Description
tokenwise Launch Claude Code (Unix: with interceptor; Windows: direct)
tokenwise setup Install status bar to ~/.claude/settings.json
tokenwise setup --uninstall Remove status bar, restore previous config
tokenwise stats Show current project usage by model
tokenwise stats --all Show all projects
tokenwise stats --session <id> Show session details
tokenwise stats --json Output as JSON
tokenwise import Import all JSONL data
tokenwise import --session <id> Import specific session
tokenwise import --verbose Show each imported entry
tokenwise update-pricing Fetch latest pricing from LiteLLM
tokenwise update-pricing --dry-run Preview update (no file write)

Uninstall

tokenwise setup --uninstall

This removes the status bar script and the statusLine config from settings.json. The database at ~/.claude/money-calc/usage.db is preserved.

Data Storage

  • Usage data: ~/.claude/money-calc/usage.db (SQLite)
  • Status bar script: ~/.claude/money-calc-statusline.js
  • Pricing data: model_prices_and_context_window.json (in project directory)

Claude Code's original JSONL logs at ~/.claude/projects/ are unaffected.

Pricing Sources

FAQ

Q: Status bar shows In: 0 Out: 0 but cost is non-zero? A: Known Windows behavior — the status bar uses JSONL fallback for token counts (fixed in v0.1.1). Run tokenwise setup to reinstall if needed.

Q: Status bar disappeared after switching models? A: Manually editing ~/.claude/settings.json may overwrite the statusLine field. Run tokenwise setup again.

Q: Cost doesn't match my actual bill? A: Possible causes: ① run tokenwise update-pricing for latest rates; ② Pro/Max plan users — status bar prefers Claude Code's built-in cost; ③ some models include search/tool-call surcharges.

Q: better-sqlite3 fails to compile? A: Install C++ build tools — Microsoft Visual C++ Build Tools on Windows, or xcode-select --install on macOS.

Project Structure

src/
├── types.ts              # Type definitions
├── pricing.ts            # Pricing engine (LiteLLM JSON + hardcoded fallbacks)
├── db.ts                 # SQLite data layer
├── interceptor.ts        # fetch() patch interceptor (Unix)
├── interceptor-loader.js # CJS bootstrap for --require preload
├── jsonl-reader.ts       # JSONL parser
├── cli.ts                # CLI entry point
└── statusline.ts         # Embedded StatusLine script

License

MIT

About

Real-time token usage & cost tracker for Claude Code. StatusLine, 2700+ models, multi-provider pricing.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors