A Go implementation of claude-costs - analyze your Claude Code usage costs and statistics by parsing local metadata files.
- π° Cost Analysis: Calculate actual API costs with cache savings
- π Token Usage: Track input, output, and cached tokens
- π Project Breakdown: See costs grouped by project
- β° Activity Patterns: Visualize usage by hour and day
- π€ Model Usage: Distribution of different Claude models
- β±οΈ Response Times: Analyze response time statistics
- π§ Tool Usage: Track tool acceptance/rejection rates
go install github.com/photostructure/go-claude-costs/cmd/claude-costs@latestgit clone https://github.com/photostructure/go-claude-costs
cd go-claude-costs
go build -o claude-costs ./cmd/claude-costs# Analyze last 30 days (default)
claude-costs
# Analyze last 7 days
claude-costs -d 7
# Show all projects (not just top 10)
claude-costs -v
# Show detailed cache statistics
claude-costs --cache
# Use custom Claude directory
claude-costs -c /path/to/.claude-d, --days: Number of days to analyze (default: 30)-v, --verbose: Show all projects instead of top 10--cache: Show detailed cache statistics-c, --claude-dir: Path to Claude directory (default: ~/.claude)-h, --help: Show help message
Analyzing: /home/user/.claude
π° $1234.56 API value (last 30 days, 25 with activity)
π 142 sessions β’ $8.69/session β’ $49.38/day
Note: This shows API value, not your actual subscription cost
π€ 345.2M tokens total
π Project Costs
βββββββββββββββββββββββββββββββ¬ββββββββββ¬βββββββββββ¬βββββββββ¬βββββββ¬βββββββββββββββ
β PROJECT β COST β SESSIONS β TOKENS β DAYS β AVG RESPONSE β
βββββββββββββββββββββββββββββββΌββββββββββΌβββββββββββΌβββββββββΌβββββββΌβββββββββββββββ€
β my-web-app β $456.78 β 42 β 125.4M β 12 β 6.2s β
β data-analysis β $234.90 β 38 β 89.1M β 18 β 7.8s β
β api-service β $156.32 β 24 β 67.3M β 14 β 5.9s β
β ml-project β $89.45 β 18 β 34.7M β 8 β 8.1s β
β documentation β $67.21 β 12 β 15.2M β 6 β 4.3s β
βββββββββββββββββββββββββββββββ΄ββββββββββ΄βββββββββββ΄βββββββββ΄βββββββ΄βββββββββββββββ
Showing top 5 of 12 projects. Use -v to see all.
β° Activity Patterns
Hourly Distribution:
00:00 ββββββββββββββββββββ 0
07:00 ββββββββββββββββββββ 45
08:00 ββββββββββββββββββββ 89
09:00 ββββββββββββββββββββ 234
15:00 ββββββββββββββββββββ 456
Daily Activity:
βββββββββ
ββββββ
βββββββββββββ
ββ
π€ Model Usage
ββββββββββββββββββββββββββββ¬ββββββββ¬βββββββββββββ
β MODEL β COUNT β PERCENTAGE β
ββββββββββββββββββββββββββββΌββββββββΌβββββββββββββ€
β claude-opus-4-20250514 β 3456 β 75.2% β
β claude-sonnet-4-20250514 β 1139 β 24.8% β
ββββββββββββββββββββββββββββ΄ββββββββ΄βββββββββββββ
π§ Tool Use
Accepted: 8945 (95.7%)
Rejected: 402 (4.3%)
β±οΈ Response Times
βββββββββββ¬βββββββββ
β Min β 87ms β
β Average β 6.4s β
β P50 β 4.8s β
β P90 β 11.2s β
β P95 β 16.7s β
β P99 β 28.9s β
β Max β 145.3s β
βββββββββββ΄βββββββββ
The tool reads JSONL files from your local Claude Code metadata directory (typically ~/.claude/projects/). These files contain:
- Message content and metadata
- Token counts for each interaction
- Cache usage information
- Timestamps and session IDs
- Model information
The Go implementation provides the same functionality as the original Python version with:
- 30% faster performance than the Python version (~1.9s vs ~2.8s)
- Efficient single-pass parsing with optimized memory usage
- Clean separation of concerns with modular package structure
- Rich terminal output using go-pretty
- Comprehensive error handling and validation
- Support for both legacy costUSD and modern token-based formats
- Accurate Claude 4 model pricing (Opus and Sonnet 4)
The Go implementation is significantly faster than the original Python version:
| Implementation | Time | Improvement |
|---|---|---|
| Python (uv) | ~2.8s | baseline |
| Go (optimized) | ~1.9s | 30% faster |
Performance optimizations include:
- Single-pass file parsing (eliminated redundant file reads)
- Efficient memory allocation with pre-sized collections
- Project name caching to avoid repeated file system operations
- Optimized JSON parsing and string operations
go-claude-costs/
βββ cmd/claude-costs/ # CLI entry point
βββ internal/
β βββ models/ # Data structures
β βββ parser/ # JSONL parsing logic
β βββ calculator/ # Statistical calculations
β βββ display/ # Output formatting
β βββ config/ # Configuration management
βββ pkg/claudecosts/ # Public API and errors
- Go 1.24 or higher
- Access to Claude Code metadata files (Claude Code CLI generates these automatically)
# Run all tests
make test
# Run tests with coverage
make coverage
# Run tests with race detection
make race
# Build the binary
make build
# Clean build artifacts
make clean
# Run linter
make lint
# Show all available commands
make help# Run tests (short version)
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with race detection
go test -race ./...
# Run only fast tests
go test -short ./...
# Run specific package tests
go test ./internal/parser
# Run with verbose output
go test -v ./...
# Build and install
go install ./cmd/claude-costs- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes
- Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This Go implementation includes several improvements and bug fixes:
- Accurate cost calculation: Fixed token cost calculation bug where cache read tokens were incorrectly subtracted from input tokens
- Complete model support: Added missing Claude 4 model pricing (Opus and Sonnet 4)
- Proper session tracking: Fixed session counting to match all assistant messages, not just those with cost data
- Comprehensive token counting: Includes all token types (input + output + cache read + cache write) in totals
- Performance optimizations: 30% faster than Python through algorithmic improvements
- Better error handling: More robust parsing with detailed error messages
- Memory efficiency: Optimized data structures and reduced allocations
- Consistent output: Matches Python output format while being more performant
- Full feature parity: All functionality from the Python version
- Same CLI interface: Drop-in replacement with identical command-line options
- Accurate calculations: Produces identical results to the corrected Python version
This project is licensed under the MIT License - see the LICENSE file for details.
- Original Python implementation: claude-costs
- Built with Cobra for CLI
- Terminal tables by go-pretty