Skip to content

feat(report): add comprehensive token usage analytics and reporting (#56)#57

Merged
luongnv89 merged 5 commits intomainfrom
feat/56-token-usage-analytics
Apr 7, 2026
Merged

feat(report): add comprehensive token usage analytics and reporting (#56)#57
luongnv89 merged 5 commits intomainfrom
feat/56-token-usage-analytics

Conversation

@luongnv89
Copy link
Copy Markdown
Owner

Closes #56

Summary

Adds a new context-stats report subcommand that generates comprehensive token usage analytics across all Claude Code projects. Users can now analyze token consumption patterns with project-level, session-level, and subagent-level breakdowns.

Features

  • Aggregate Analytics: Summarize token consumption across all projects
  • Project Breakdown: Per-project token usage with cost tracking
  • Session Details: Top 10 most expensive sessions per project with:
    • Token counts (input, output, cache creation, cache read)
    • Session duration and start time
    • Model ID and cost
  • Subagent Tracking: Identify most-used subagents per project
  • Date Filtering: Optional --since-days parameter to limit scope
  • Flexible Output: Configurable output file path with timestamp-based default

Implementation

New modules:

  • src/claude_statusline/analytics.py (287 lines) — Project data discovery and aggregation

    • Discovers projects in ~/.claude/projects/
    • Loads session state files from ~/.claude/statusline/
    • Parses subagent data from session JSONL files
    • Filters by date range
  • src/claude_statusline/cli/report.py (202 lines) — Report generation and formatting

    • Generates markdown with grand totals and per-project breakdowns
    • Formats token counts and dates for readability
    • Saves reports to disk with configurable paths

Updates:

  • src/claude_statusline/cli/context_stats.py — Add report action dispatch
    • Updated _KNOWN_ACTIONS to include "report"
    • Modified _normalize_argv() to handle report as session-ID-optional action
    • Added report action dispatch in main()
    • Updated help text and examples

Tests:

  • tests/python/test_report.py — Unit tests for report generation

Test Results

All 347 existing tests pass + 2 new tests:

  • ✓ Report generation with sample project data
  • ✓ Report generation with no data

Usage Examples

# Generate report for all projects
context-stats report

# Generate report with custom output file
context-stats report --output my-report.md

# Generate report for last 30 days
context-stats report --since-days 30

Architecture

  • No external dependencies — uses only stdlib and existing project modules
  • Defensive parsing — gracefully handles missing/malformed project data
  • Session-ID-optional action — similar to existing explain command
  • Data flows from ~/.claude/projects/ → state files → aggregation → markdown

Acceptance Criteria

  • Create new CLI subcommand for token usage reports
  • Parse session data from ~/.claude/projects/ with proper encoding handling
  • Generate markdown reports with project, session, and subagent breakdowns
  • Support date filtering via --since-days
  • Track input, output, cache creation, and cache read tokens
  • Format output with readable numbers and timestamps
  • Save reports to configurable output directory
  • Implement in Python package for CLI and library use
  • Add integration tests for sample data

Implement a new 'context-stats report' subcommand that generates
comprehensive token usage analytics across all Claude Code projects.

Features:
- Aggregate token consumption by project, session, and subagent
- Generate markdown reports with grand totals and per-project breakdowns
- Support date filtering with --since-days parameter
- Track input, output, cache creation, and cache read tokens separately
- Display top sessions and subagent usage per project
- Configurable output file path with timestamp default

New modules:
- analytics.py: Data loading and aggregation from ~/.claude/projects/
- cli/report.py: Report generation and markdown formatting

Updates:
- context_stats.py: Add report action dispatch, update CLI help text
- tests: Add comprehensive unit tests for report generation

Closes #56
@luongnv89 luongnv89 merged commit 0711b36 into main Apr 7, 2026
51 checks passed
@luongnv89 luongnv89 deleted the feat/56-token-usage-analytics branch April 7, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add token usage analytics and reporting

1 participant