# claude-code-stats
`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-stats` binary that prints a widget-friendly JSON payload.
## Features
- Fetches usage from Claude OAuth usage API
- Falls back to claude.ai web session usage API
- Falls back to probing `/usage` from the Claude CLI
- Caches responses for 4 minutes
- Computes usage windows and pace signals
- Scans recent Claude project logs for estimated 30-day cost
## Install
```bash
cargo install claude-code-stats
```
## CLI Usage
```bash
claude-code-stats
```
The command prints a JSON payload to stdout.
## Library Usage
```rust
fn main() {
let json = claude_code_stats::collect_widget_payload_json();
println!("{json}");
}
```
Or use the typed API:
```rust
fn main() -> anyhow::Result<()> {
let payload = claude_code_stats::collect_widget_payload()?;
println!("{}", serde_json::to_string_pretty(&payload)?);
Ok(())
}
```
## Authentication Sources
The crate tries the following usage sources in order:
1. OAuth API via keychain/credentials token
2. claude.ai web API via browser session cookie
3. CLI `/usage` probe via pseudo-terminal session
## License
MIT