use claude_agent_sdk_rust::{ClaudeAgentOptions, ClaudeSDKClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClaudeSDKClient::new(ClaudeAgentOptions::default());
println!("đ Fetching Claude Code usage data...\n");
match client.get_usage().await {
Ok(usage) => {
println!("=== Current Usage ===\n");
println!("đ 5-Hour Rolling Window:");
println!(" Usage: {:.1}%", usage.five_hour.utilization);
if let Some(resets_at) = &usage.five_hour.resets_at {
println!(" Resets at: {}", resets_at);
}
println!();
println!("đ
7-Day (Weekly) - All Models:");
println!(" Usage: {:.1}%", usage.seven_day.utilization);
if let Some(resets_at) = &usage.seven_day.resets_at {
println!(" Resets at: {}", resets_at);
}
println!();
println!("đ 7-Day (Weekly) - Opus Only:");
println!(" Usage: {:.1}%", usage.seven_day_opus.utilization);
if let Some(resets_at) = &usage.seven_day_opus.resets_at {
println!(" Resets at: {}", resets_at);
} else {
println!(" Not applicable (Opus not used or not available)");
}
println!();
println!("đ 7-Day OAuth Apps:");
println!(" Usage: {:.1}%", usage.seven_day_oauth_apps.utilization);
if usage.seven_day_oauth_apps.utilization > 0.0 {
if let Some(resets_at) = &usage.seven_day_oauth_apps.resets_at {
println!(" Resets at: {}", resets_at);
}
} else {
println!(" (Typically 0 for CLI usage)");
}
println!();
println!("=== Usage Analysis ===\n");
let max_util = usage.max_utilization();
println!("đ Highest utilization: {:.1}%", max_util);
if usage.is_at_limit() {
println!("â ī¸ WARNING: At or very close to usage limit (>= 95%)!");
println!(" Consider waiting for limits to reset before heavy usage.");
} else if usage.is_approaching_limit() {
println!("â ī¸ Approaching usage limit (>= 80%).");
println!(" Use conservatively to avoid hitting limits.");
} else if usage.is_above_threshold(50.0) {
println!("âšī¸ Moderate usage detected.");
println!(" You have plenty of quota remaining.");
} else {
println!("â
Low usage - plenty of quota available!");
}
println!("\n=== Visual Progress ===\n");
print_progress_bar("5-Hour", usage.five_hour.utilization);
print_progress_bar("Weekly", usage.seven_day.utilization);
print_progress_bar("Opus ", usage.seven_day_opus.utilization);
}
Err(e) => {
eprintln!("â Failed to fetch usage data: {}", e);
eprintln!("\nPossible causes:");
eprintln!(" - No OAuth credentials found (Max Plan required)");
eprintln!(" - Access token expired (run `claude` to refresh)");
eprintln!(" - Network error");
eprintln!(" - API endpoint unavailable");
return Err(e.into());
}
}
Ok(())
}
fn print_progress_bar(label: &str, utilization: f64) {
let width = 50;
let filled = ((utilization / 100.0) * width as f64) as usize;
let empty = width - filled;
let color = if utilization >= 95.0 {
"đ´" } else if utilization >= 80.0 {
"đĄ" } else {
"đĸ" };
print!("{} {} [", color, label);
for _ in 0..filled {
print!("â");
}
for _ in 0..empty {
print!("â");
}
println!("] {:.1}%", utilization);
}