Analyze your Claude Code session data to identify cost hotspots, track spending, and get actionable recommendations for reducing costs.
No external dependencies required. Uses only the Python standard library.
Python 3.10+ recommended.
python tools/usage-analyzer/analyze.py ~/.claude/projects/Output:
Claude Code Usage Report
========================================================
Overview
--------------------------------------------------------
Sessions analyzed: 12
Total turns: 487
Total tokens: 2,341,890
Input tokens: 2,105,230
Output tokens: 236,660
Estimated cost: $9.86
Avg tokens/turn: 4,809
Top 5 Sessions (by cost)
--------------------------------------------------------
# Session Tokens Turns Cost
.... ........................ .......... ....... ..........
1 refactor-auth-module 482,100 87 $2.17
2 new-dashboard-feature 391,420 63 $1.64
3 debug-api-timeout 287,300 52 $1.19
4 update-dependencies 198,700 41 $0.83
5 fix-css-layout 156,200 38 $0.62
Cost Hotspots
--------------------------------------------------------
! Session 'refactor-auth-module' averages 5,541 tokens/turn.
Consider trimming context or using .claudeignore.
! Opus is used for 312/487 (64%) of turns. Switch routine
tasks to Sonnet or Haiku for major savings.
Recommendations
--------------------------------------------------------
1. Input tokens dominate your usage (90%). Focus on reducing
context size: trim CLAUDE.md, use .claudeignore, avoid
reading large files.
2. Estimated total spend: $9.86. Review the most expensive
sessions above for quick wins.
python tools/usage-analyzer/analyze.py ~/.claude/projects/ --top 10python tools/usage-analyzer/analyze.py ~/.claude/projects/ --sort tokens
python tools/usage-analyzer/analyze.py ~/.claude/projects/ --sort turns
python tools/usage-analyzer/analyze.py ~/.claude/projects/ --sort costpython tools/usage-analyzer/analyze.py ~/.claude/projects/ --jsonUseful for piping into other tools or dashboards.
| Flag | Description | Default | Example |
|---|---|---|---|
directory |
Path to directory containing session files | (required) | ~/.claude/projects/ |
--top N |
Number of top sessions to display | 5 |
--top 10 |
--sort FIELD |
Sort sessions by: cost, tokens, or turns |
cost |
--sort tokens |
--json |
Output results as JSON | off | --json |
The analyzer scans for .json and .jsonl files and attempts to parse them as Claude Code session data. It recognizes several formats:
- JSONL message logs: One JSON object per line, each representing a message or event with
usage,role, andcontentfields. - JSON session summaries: A single JSON object with aggregate
usageortoken_usagefields. - JSON message arrays: A JSON array of message objects.
Files that don't match any recognized format are silently skipped.
Total tokens, cost, and average tokens per turn across all sessions.
The most expensive sessions ranked by your chosen sort criteria. Look here for the biggest cost-saving opportunities.
Specific patterns that are driving up costs:
- High per-turn token usage: Sessions where each turn processes a large context.
- Heavy file reads: Too many file read operations inflating input tokens.
- Expensive model overuse: Using Opus where Sonnet or Haiku would suffice.
- Long sessions: Conversations that accumulate excessive context over many turns.
Actionable suggestions based on your usage patterns, linking back to strategies from the Claude Cost Optimizer guides.
- Run the analyzer weekly to track spending trends.
- After optimizing your setup (trimming CLAUDE.md, adding .claudeignore), re-analyze to measure the impact.
- Use
--jsonoutput to build your own cost tracking dashboards. - Focus on the top 2-3 most expensive sessions for the highest ROI.