1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # 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. It handles credential retrieval
//! from platform-specific secure storage and returns typed, structured data.
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use claude_usage::get_usage;
//!
//! let usage = get_usage()?;
//! println!("5h utilization: {}%", usage.five_hour.utilization);
//! println!("7d utilization: {}%", usage.seven_day.utilization);
//! ```
//!
//! ## Features
//!
//! - **Cross-platform credentials**: macOS Keychain, Linux credential file
//! - **Typed responses**: [`UsageData`], [`UsagePeriod`], [`ExtraUsage`]
//! - **Secure handling**: Tokens are read, used, and immediately discarded
//! - **Helper methods**: Check if usage is on-pace, time until reset
//! - **Node.js bindings**: Available via the `napi` feature
//!
//! ## Platform Support
//!
//! | Platform | Credential Source | Status |
//! |----------|-------------------|--------|
//! | macOS | Keychain ("Claude Code-credentials") | ✅ |
//! | Linux | `~/.claude/.credentials.json` | ✅ |
//! | Windows | - | ❌ |
//!
//! ## Usage Examples
//!
//! ### Basic Usage
//!
//! ```rust,ignore
//! use claude_usage::get_usage;
//!
//! fn main() -> Result<(), claude_usage::Error> {
//! let usage = get_usage()?;
//!
//! println!("5-hour: {}%", usage.five_hour.utilization);
//! println!("7-day: {}%", usage.seven_day.utilization);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Checking If Usage Is Sustainable
//!
//! ```rust,ignore
//! use claude_usage::get_usage;
//!
//! let usage = get_usage()?;
//!
//! // Check if current usage rate won't exceed quota before reset
//! if usage.five_hour_on_pace() {
//! println!("5-hour usage is sustainable");
//! } else {
//! println!("Warning: 5-hour usage may exceed quota!");
//! }
//!
//! if usage.seven_day_on_pace() {
//! println!("7-day usage is sustainable");
//! }
//! ```
//!
//! ### Time Until Reset
//!
//! ```rust,ignore
//! use claude_usage::get_usage;
//!
//! let usage = get_usage()?;
//!
//! let time_left = usage.five_hour.time_until_reset();
//! println!("5-hour quota resets in {} minutes", time_left.num_minutes());
//!
//! // Get percentage of period elapsed
//! let elapsed = usage.five_hour.time_elapsed_percent(5);
//! println!("{}% of 5-hour period has elapsed", elapsed);
//! ```
//!
//! ### Handling Errors
//!
//! ```rust,ignore
//! use claude_usage::{get_usage, Error, CredentialError, ApiError};
//!
//! match get_usage() {
//! Ok(usage) => println!("Usage: {}%", usage.five_hour.utilization),
//! Err(Error::Credential(CredentialError::NotFound)) => {
//! eprintln!("Please run `claude` to login first");
//! }
//! Err(Error::Credential(CredentialError::Expired)) => {
//! eprintln!("Token expired. Please run `claude` to re-login");
//! }
//! Err(Error::Api(ApiError::RateLimited { retry_after })) => {
//! eprintln!("Rate limited. Retry after: {:?}", retry_after);
//! }
//! Err(e) => eprintln!("Error: {}", e),
//! }
//! ```
//!
//! ## Environment Variable
//!
//! The `CLAUDE_CODE_OAUTH_TOKEN` environment variable can override
//! file-based credential retrieval on any platform:
//!
//! ```bash
//! export CLAUDE_CODE_OAUTH_TOKEN="sk-ant-oat01-..."
//! ```
//!
//! ## Module Overview
//!
//! - [`client`]: HTTP client for the Anthropic usage API
//! - [`credentials`]: Platform-specific credential retrieval
//! - [`types`]: Response types ([`UsageData`], [`UsagePeriod`], [`ExtraUsage`])
//! - [`error`]: Error types ([`Error`], [`CredentialError`], [`ApiError`])
//! - `napi`: Node.js bindings (requires `napi` feature)
//!
//! ## Security
//!
//! This crate follows strict security practices:
//!
//! 1. Tokens are read from secure storage, used once, and immediately discarded
//! 2. Tokens are never stored in memory, logged, or passed to other modules
//! 3. Error messages use generic text to prevent credential exposure
pub use fetch_usage_raw;
pub use get_token;
pub use ;
pub use ;
/// Fetch current Claude API usage data.
///
/// This is the main entry point for the crate. It:
/// 1. Retrieves credentials from platform-specific storage
/// 2. Calls the Anthropic usage API
/// 3. Returns typed usage data
///
/// # Example
///
/// ```rust,ignore
/// use claude_usage::get_usage;
///
/// let usage = get_usage()?;
/// println!("5h utilization: {}%", usage.five_hour.utilization);
/// println!("7d utilization: {}%", usage.seven_day.utilization);
/// ```
///
/// # Errors
///
/// Returns [`Error`] if:
/// - Credentials are not found or expired
/// - API call fails
/// - Response parsing fails