claude-code-stats is a Rust library and CLI for collecting Claude Code usage
stats and rendering them as JSON payloads.
The crate supports both:
- a reusable library API (
claude_code_stats), and - a
claude-code-statsbinary that prints a widget-friendly JSON payload.
- Fetches usage from Claude OAuth usage API
- Falls back to claude.ai web session usage API
- Falls back to probing
/usagefrom the Claude CLI - Caches responses for 4 minutes
- Computes usage windows and pace signals
- Scans recent Claude project logs for estimated 30-day cost
cargo install claude-code-statsclaude-code-statsThe command prints a JSON payload to stdout.
fn main() {
let json = claude_code_stats::collect_widget_payload_json();
println!("{json}");
}Or use the typed API:
fn main() -> anyhow::Result<()> {
let payload = claude_code_stats::collect_widget_payload()?;
println!("{}", serde_json::to_string_pretty(&payload)?);
Ok(())
}The crate tries the following usage sources in order:
- OAuth API via keychain/credentials token
- claude.ai web API via browser session cookie
- CLI
/usageprobe via pseudo-terminal session
MIT