Expand description
§claude-usage
A library for fetching Claude API usage data from Anthropic.
This crate provides a simple API to retrieve usage statistics including 5-hour and 7-day utilization percentages.
§Features
- Cross-platform credential retrieval (macOS Keychain, Linux credential file)
- Typed response structures for usage data
- Secure credential handling (read, use, discard immediately)
§Platform Support
| Platform | Credential Source |
|---|---|
| macOS | Keychain (“Claude Code-credentials”) |
| Linux | ~/.claude/.credentials.json |
§Example
ⓘ
use claude_usage::get_usage;
let usage = get_usage()?;
println!("5h utilization: {}%", usage.five_hour.utilization);
println!("7d utilization: {}%", usage.seven_day.utilization);
// Check if usage is sustainable
if usage.five_hour_on_pace() {
println!("5-hour usage is on pace");
}§Environment Variable
The CLAUDE_CODE_OAUTH_TOKEN environment variable can be set to override
file-based credential retrieval on any platform.
Re-exports§
pub use client::fetch_usage_raw;pub use credentials::get_token;pub use error::ApiError;pub use error::CredentialError;pub use error::Error;pub use types::ExtraUsage;pub use types::UsageData;pub use types::UsagePeriod;
Modules§
- client
- HTTP client for the Anthropic usage API.
- credentials
- Credential retrieval for Claude Code OAuth tokens.
- error
- Error types for the claude-usage crate.
- types
- Type definitions for Anthropic usage API responses.
Functions§
- get_
usage - Fetch current Claude API usage data.