Skip to content

safx/ccr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ccr - Claude Code Usage StatusLine

A statusline hook for Claude Code that displays usage costs and session information.

Installation

Homebrew (Recommended)

brew tap safx/tap
brew install ccr

Build from source

# Build release binary
cargo build --release

# Copy to your bin directory
cp target/release/ccr ~/bin/

Setup

Add to ~/.claude/settings.json:

{
  "statusLine": {
    "type": "command",
    "command": "ccr"
  }
}

Note: If you installed via Homebrew, ccr will be in your PATH. If you built from source and installed to ~/bin/, you may need to use the full path $HOME/bin/ccr.

What it displays

The statusline shows:

  • Current session cost
  • Hourly burn rate
  • Remaining time at current rate
  • Context usage percentage
  • Active session blocks
  • Code changes (lines added/removed)
  • Current directory
  • Git branch (when in a git repository)
  • Model name
  • Output style (when not default)

Example output:

ccr main πŸ‘€ Opus 4.1 [Learning] ⏰ 1h 18m left πŸ’° $63.87 today, $11.58 session, $62.35 block πŸ”₯ $21.13/hr βš–οΈ 70% (108,887 / 155,000) ✏️ +23 -17

How it works

ccr reads Claude Code usage data from ~/.config/claude_code/projects/**/*.jsonl files and:

  1. Parses JSONL entries containing API usage information
  2. Deduplicates entries using message_id:request_id pairs
  3. Groups activity into 5-hour session blocks
  4. Calculates costs based on token usage and model pricing
  5. Outputs formatted statusline string to stdout

Input format

Claude Code sends JSON via stdin:

{
  "session_id": "3680e2cb-6c42-4c66-8545-973e66227c1d",
  "cwd": "/Users/someone/src/mydev/ccr",
  "transcript_path": "/Users/someone/.claude/projects/-Users-someone-src-mydev-ccr/3680e2cb-6c42-4c66-8545-973e66227c1d.jsonl",
  "model": {
    "id": "claude-opus-4-1-20250805",
    "display_name": "Opus 4.1"
  },
  "workspace": {
    "current_dir": "/Users/someone/src/mydev/ccr",
    "project_dir": "/Users/someone/src/mydev/ccr"
  },
  "version": "1.0.85",
  "output_style": {
    "name": "Standard"
  },
  "cost": {
    "total_cost_usd": 5.14,
    "total_duration_ms": 1234567,
    "total_api_duration_ms": 456789,
    "total_lines_added": 23,
    "total_lines_removed": 17
  }
}

Profiling tools

Two binaries are included for performance analysis:

# Basic profiling
./target/release/profile

# Detailed breakdown
./target/release/profile_deep

Project structure

src/
β”œβ”€β”€ lib.rs                      # Library exports
β”œβ”€β”€ constants.rs                # Shared constants  
β”œβ”€β”€ error.rs                    # Error types and handling
β”œβ”€β”€ types/                      # Data structures and domain logic
β”‚   β”œβ”€β”€ mod.rs                  # Module exports
β”‚   β”œβ”€β”€ ids.rs                  # ID types (SessionId, MessageId, etc.)
β”‚   β”œβ”€β”€ input.rs                # Input data structures
β”‚   β”œβ”€β”€ pricing.rs              # Pricing models and calculations
β”‚   β”œβ”€β”€ session.rs              # Session blocks and snapshots
β”‚   β”œβ”€β”€ usage.rs                # Usage entry structures
β”‚   β”œβ”€β”€ burn_rate.rs            # Burn rate calculation (NewType)
β”‚   β”œβ”€β”€ context_tokens.rs       # Context token handling (NewType)
β”‚   β”œβ”€β”€ cost.rs                 # Cost calculation and formatting (NewType)
β”‚   └── remaining_time.rs       # Remaining time calculation (NewType)
β”œβ”€β”€ utils/                      # Utility functions
β”‚   β”œβ”€β”€ mod.rs                  # Module exports
β”‚   β”œβ”€β”€ data_loader.rs          # Parallel JSONL file loading
β”‚   β”œβ”€β”€ transcript_loader.rs    # Transcript file parsing
β”‚   β”œβ”€β”€ git.rs                  # Git branch detection
β”‚   └── paths.rs                # Claude Code path discovery
└── bin/
    β”œβ”€β”€ ccr.rs                  # Main statusline hook
    β”œβ”€β”€ filter_stats.rs         # Statistics filtering tool
    β”œβ”€β”€ profile.rs              # Performance profiling
    β”œβ”€β”€ profile_deep.rs         # Detailed profiling
    β”œβ”€β”€ profile_loader.rs       # Data loader profiling
    β”œβ”€β”€ profile_micro.rs        # Micro-benchmarking tool
    └── bench_sessionid.rs      # SessionId benchmarking

Building from source

# Development
cargo build

# Release (with optimizations)
cargo build --release

# Run tests
cargo test

# Run with cargo-nextest (if installed)
cargo nextest run

Testing

# Run all tests
cargo test

# Run with cargo-nextest (recommended)
cargo nextest run

# Test with sample input
echo '{"session_id":"test","cwd":"/tmp","transcript_path":"/dev/null","model":{"display_name":"claude-3-5-sonnet-20241022","max_output_tokens":8192}}' | ./target/release/ccr

Development History

  1. Started with ccusage by ryoppippi
  2. Converted ccusage to other_langage/ccr_deno.ts (standalone Deno TypeScript version)
  3. Developed this Rust version based on ccr_deno.ts
  4. Refactored to use NewType pattern and clean architecture principles

The core algorithms - session block identification, cost calculation, and deduplication logic - originate from the ccusage implementation.

Acknowledgments

This implementation is heavily based on ccusage by ryoppippi. I'm grateful for the well-designed original implementation and for making it open source. The clear architecture and algorithms in ccusage made this Rust port possible. Thank you!

About

An ultra-fast status line hook for Claude Code written in Rust

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published