use std::collections::HashMap;
use std::sync::LazyLock;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::lockorder::{RankedMutex, rank};
const USAGE_ENDPOINT: &str = "https://api.anthropic.com/api/oauth/usage";
const PROFILE_ENDPOINT: &str = "https://api.anthropic.com/api/oauth/profile";
const PROFILE_TTL_MS: u64 = 60 * 60 * 1000;
static PROFILE_FETCHED: LazyLock<RankedMutex<HashMap<String, u64>, rank::ProfileTtl>> =
LazyLock::new(|| RankedMutex::new(HashMap::new()));
const OAUTH_REQUEST_SPACING_MS: u64 = 5_000;
static NEXT_REQUEST_SLOT: LazyLock<RankedMutex<u64, rank::UsageThrottle>> =
LazyLock::new(|| RankedMutex::new(0));
fn reserve_slot(current_slot: u64, now: u64) -> (u64, u64) {
let fire_at = current_slot.max(now);
(
fire_at.saturating_add(OAUTH_REQUEST_SPACING_MS),
fire_at.saturating_sub(now),
)
}
fn await_request_slot() {
let now = now_ms();
let wait_ms = {
let Ok(mut slot) = NEXT_REQUEST_SLOT.lock() else {
return;
};
let (next, wait) = reserve_slot(*slot, now);
*slot = next;
wait
};
if wait_ms > 0 {
std::thread::sleep(Duration::from_millis(wait_ms));
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct UsageWindow {
pub(crate) utilization: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) resets_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(crate) struct ExtraUsage {
#[serde(default)]
pub(crate) is_enabled: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) monthly_limit: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) used_credits: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) utilization: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) currency: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub(crate) enum PlanTier {
Max(#[serde(default)] Option<u16>),
Pro,
Team,
Enterprise,
Free,
#[default]
Unknown,
}
impl PlanTier {
pub(crate) fn from_profile(
org_type: Option<&str>,
has_max: bool,
has_pro: bool,
rate_limit_tier: Option<&str>,
) -> Self {
match org_type.unwrap_or("") {
"claude_max" => PlanTier::Max(max_multiplier(rate_limit_tier)),
"claude_pro" => PlanTier::Pro,
"claude_team" | "claude_teams" => PlanTier::Team,
"claude_enterprise" => PlanTier::Enterprise,
"claude_free" | "free" => PlanTier::Free,
"" => {
if has_max {
PlanTier::Max(None)
} else if has_pro {
PlanTier::Pro
} else {
PlanTier::Unknown
}
}
_ => PlanTier::Unknown,
}
}
pub(crate) fn from_subscription_type(s: Option<&str>) -> Self {
match s.unwrap_or("pro") {
"pro" => PlanTier::Pro,
"max" => PlanTier::Max(None),
"team" | "teams" => PlanTier::Team,
"enterprise" => PlanTier::Enterprise,
_ => PlanTier::Unknown,
}
}
pub(crate) fn display(&self) -> String {
match self {
PlanTier::Max(Some(n)) => format!("Claude Max {n}x"),
PlanTier::Max(None) => "Claude Max".to_string(),
PlanTier::Pro => "Claude Pro".to_string(),
PlanTier::Team => "Claude Team".to_string(),
PlanTier::Enterprise => "Claude Enterprise".to_string(),
PlanTier::Free => "Claude Free".to_string(),
PlanTier::Unknown => "Claude".to_string(),
}
}
}
fn max_multiplier(tier: Option<&str>) -> Option<u16> {
let tier = tier?;
let last = tier.rsplit('_').next()?;
last.strip_suffix('x').and_then(|m| {
m.chars()
.all(|c| c.is_ascii_digit())
.then(|| m.parse().ok())?
})
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(crate) struct PlanInfo {
#[serde(default)]
pub(crate) tier: PlanTier,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(crate) struct UsageInfo {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) plan: Option<PlanInfo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) five_hour: Option<UsageWindow>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) seven_day: Option<UsageWindow>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) seven_day_opus: Option<UsageWindow>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) seven_day_sonnet: Option<UsageWindow>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) extra_usage: Option<ExtraUsage>,
}
pub(crate) const LABEL_5H: &str = "5h";
pub(crate) const LABEL_7D: &str = "7d";
pub(crate) const LABEL_7D_SONNET: &str = "7d sonnet";
pub(crate) const LABEL_7D_OPUS: &str = "7d opus";
impl UsageInfo {
pub(crate) fn windows(&self) -> Vec<(&'static str, &UsageWindow)> {
let mut out = Vec::new();
if let Some(w) = &self.five_hour {
out.push((LABEL_5H, w));
}
if let Some(w) = &self.seven_day {
out.push((LABEL_7D, w));
}
if let Some(w) = &self.seven_day_sonnet {
out.push((LABEL_7D_SONNET, w));
}
if let Some(w) = &self.seven_day_opus {
out.push((LABEL_7D_OPUS, w));
}
out
}
pub(crate) fn weekly_window(&self) -> Option<&UsageWindow> {
self.seven_day
.as_ref()
.or(self.seven_day_sonnet.as_ref())
.or(self.seven_day_opus.as_ref())
}
}
pub(crate) fn window_duration_secs(label: &str) -> Option<i64> {
match label {
LABEL_5H => Some(5 * 3600),
LABEL_7D | LABEL_7D_SONNET | LABEL_7D_OPUS => Some(7 * 86_400),
_ => parse_nh_nd_label(label),
}
}
fn parse_nh_nd_label(label: &str) -> Option<i64> {
let (num, unit) = label.split_at(label.len().checked_sub(1)?);
let n = num.parse::<i64>().ok().filter(|&n| n > 0)?;
match unit {
"h" => Some(n * 3600),
"d" => Some(n * 86_400),
_ => None,
}
}
pub(crate) fn ideal_pace_pct(label: &str, window: &UsageWindow, now_secs: i64) -> Option<f64> {
let duration = window_duration_secs(label)?;
let reset = iso_to_epoch_secs(window.resets_at.as_deref()?)?;
let remaining = (reset - now_secs).clamp(0, duration);
let elapsed = duration - remaining;
Some(elapsed as f64 / duration as f64 * 100.0)
}
pub(crate) fn window_avg_pace_per_day(
label: &str,
window: &UsageWindow,
now_secs: i64,
min_elapsed_secs: i64,
) -> Option<f64> {
let duration = window_duration_secs(label)?;
let reset = iso_to_epoch_secs(window.resets_at.as_deref()?)?;
let remaining = (reset - now_secs).clamp(0, duration);
let elapsed = duration - remaining;
if elapsed < min_elapsed_secs {
return None;
}
Some(window.utilization / (elapsed as f64 / 86_400.0))
}
#[derive(Deserialize)]
struct RawUsage {
#[serde(default)]
five_hour: Option<UsageWindow>,
#[serde(default)]
seven_day: Option<UsageWindow>,
#[serde(default)]
seven_day_opus: Option<UsageWindow>,
#[serde(default)]
seven_day_sonnet: Option<UsageWindow>,
#[serde(default)]
extra_usage: Option<ExtraUsage>,
}
#[derive(Deserialize)]
struct RawProfile {
#[serde(default)]
account: Option<RawProfileAccount>,
#[serde(default)]
organization: Option<RawProfileOrg>,
}
#[derive(Deserialize)]
struct RawProfileAccount {
#[serde(default)]
has_claude_max: bool,
#[serde(default)]
has_claude_pro: bool,
}
#[derive(Deserialize)]
struct RawProfileOrg {
#[serde(default)]
organization_type: Option<String>,
#[serde(default)]
rate_limit_tier: Option<String>,
}
pub(super) enum FetchError {
Status(u16),
RateLimited {
retry_after: Option<Duration>,
},
Network,
Parse,
}
pub(crate) fn parse_retry_after(value: &str) -> Option<Duration> {
parse_retry_after_at(value, now_epoch_secs())
}
pub(crate) fn parse_retry_after_at(value: &str, now_secs: i64) -> Option<Duration> {
let value = value.trim();
if let Ok(secs) = value.parse::<u64>() {
return Some(Duration::from_secs(secs));
}
let target = httpdate_to_epoch_secs(value)?;
Some(Duration::from_secs(
target.saturating_sub(now_secs).max(0) as u64
))
}
fn httpdate_to_epoch_secs(value: &str) -> Option<i64> {
let mut parts = value.split_ascii_whitespace();
parts.next()?; let day: i64 = parts.next()?.parse().ok()?;
let month: i64 = match parts.next()? {
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
_ => return None,
};
let year: i64 = parts.next()?.parse().ok()?;
let mut hms = parts.next()?.split(':');
if parts.next()? != "GMT" || parts.next().is_some() {
return None;
}
let hour: i64 = hms.next()?.parse().ok()?;
let minute: i64 = hms.next()?.parse().ok()?;
let second: i64 = hms.next()?.parse().ok()?;
if hms.next().is_some() || !(1..=31).contains(&day) || hour > 23 || minute > 59 || second > 60 {
return None;
}
Some(days_from_civil(year, month, day) * 86_400 + hour * 3600 + minute * 60 + second)
}
static AGENT: LazyLock<ureq::Agent> = LazyLock::new(|| {
ureq::Agent::config_builder()
.timeout_connect(Some(Duration::from_secs(4)))
.timeout_recv_response(Some(Duration::from_secs(8)))
.http_status_as_error(false)
.build()
.into()
});
pub(crate) fn http_agent() -> &'static ureq::Agent {
&AGENT
}
fn get_json(url: &str, access_token: &str) -> std::result::Result<String, FetchError> {
await_request_slot();
let mut response = AGENT
.get(url)
.header("Authorization", &format!("Bearer {access_token}"))
.header("anthropic-beta", "oauth-2025-04-20")
.call()
.map_err(|_| FetchError::Network)?;
let status = response.status().as_u16();
if status == 429 {
let retry_after = response
.headers()
.get("retry-after")
.and_then(|v| v.to_str().ok())
.and_then(parse_retry_after);
return Err(FetchError::RateLimited { retry_after });
}
if status >= 400 {
return Err(FetchError::Status(status));
}
response
.body_mut()
.read_to_string()
.map_err(|_| FetchError::Network)
}
pub(crate) fn expire_profile_ttl(name: &str) {
if let Ok(mut m) = PROFILE_FETCHED.lock() {
m.remove(name);
}
}
fn take_profile_fetch(name: &str, force: bool, now: u64) -> bool {
let fresh = PROFILE_FETCHED
.lock()
.ok()
.and_then(|m| m.get(name).copied())
.is_some_and(|t| now.saturating_sub(t) < PROFILE_TTL_MS);
let want = force || !fresh;
if want && let Ok(mut m) = PROFILE_FETCHED.lock() {
m.insert(name.to_string(), now);
}
want
}
pub(super) fn fetch_raw(
name: &str,
access_token: &str,
prev_plan: Option<PlanInfo>,
force_profile: bool,
) -> std::result::Result<UsageInfo, FetchError> {
let usage_text = get_json(USAGE_ENDPOINT, access_token)?;
let raw: RawUsage = serde_json::from_str(&usage_text).map_err(|_| FetchError::Parse)?;
let plan = if take_profile_fetch(name, force_profile, now_ms()) {
get_json(PROFILE_ENDPOINT, access_token)
.ok()
.and_then(|text| serde_json::from_str::<RawProfile>(&text).ok())
.map(|p| {
let org = p.organization.as_ref();
PlanInfo {
tier: PlanTier::from_profile(
org.and_then(|o| o.organization_type.as_deref()),
p.account.as_ref().is_some_and(|a| a.has_claude_max),
p.account.as_ref().is_some_and(|a| a.has_claude_pro),
org.and_then(|o| o.rate_limit_tier.as_deref()),
),
}
})
.or(prev_plan)
} else {
prev_plan
};
Ok(UsageInfo {
plan,
five_hour: raw.five_hour,
seven_day: raw.seven_day,
seven_day_opus: raw.seven_day_opus,
seven_day_sonnet: raw.seven_day_sonnet,
extra_usage: raw.extra_usage,
})
}
pub(crate) fn now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0)
}
pub(crate) fn iso_to_epoch_secs(s: &str) -> Option<i64> {
let bytes = s.as_bytes();
if bytes.len() < 19 || bytes[4] != b'-' || bytes[7] != b'-' || bytes[10] != b'T' {
return None;
}
let year: i64 = std::str::from_utf8(&bytes[0..4]).ok()?.parse().ok()?;
let month: i64 = std::str::from_utf8(&bytes[5..7]).ok()?.parse().ok()?;
let day: i64 = std::str::from_utf8(&bytes[8..10]).ok()?.parse().ok()?;
let hour: i64 = std::str::from_utf8(&bytes[11..13]).ok()?.parse().ok()?;
let minute: i64 = std::str::from_utf8(&bytes[14..16]).ok()?.parse().ok()?;
let second: i64 = std::str::from_utf8(&bytes[17..19]).ok()?.parse().ok()?;
let tail = &s[19..];
let after_frac = if let Some(rest) = tail.strip_prefix('.') {
let end = rest
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(rest.len());
&rest[end..]
} else {
tail
};
let tz_offset_secs: i64 = if after_frac.is_empty() || after_frac.starts_with('Z') {
0
} else {
let sign = match after_frac.as_bytes()[0] {
b'+' => 1,
b'-' => -1,
_ => return None,
};
let digits: String = after_frac[1..].chars().filter(|&c| c != ':').collect();
if after_frac[1..]
.chars()
.any(|c| c != ':' && !c.is_ascii_digit())
{
return None;
}
let (tz_h, tz_m): (i64, i64) = match digits.len() {
2 => (digits.parse().ok()?, 0),
4 => (digits[0..2].parse().ok()?, digits[2..4].parse().ok()?),
_ => return None,
};
sign * (tz_h * 3600 + tz_m * 60)
};
let days = days_from_civil(year, month, day);
Some(days * 86400 + hour * 3600 + minute * 60 + second - tz_offset_secs)
}
fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
let y = if month <= 2 { year - 1 } else { year };
let era = if y >= 0 { y } else { y - 399 } / 400;
let yoe = y - era * 400;
let doy = (153 * (if month > 2 { month - 3 } else { month + 9 }) + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
era * 146097 + doe - 719468
}
pub(crate) fn epoch_secs_to_iso(secs: i64) -> String {
let secs = secs.max(0);
let s = secs % 60;
let mi = (secs / 60) % 60;
let h = (secs / 3600) % 24;
let days = secs / 86400;
let z = days + 719468;
let era = z / 146097;
let doe = z - era * 146097;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let mo = if mp < 10 { mp + 3 } else { mp - 9 };
let y = if mo <= 2 { y + 1 } else { y };
format!("{y:04}-{mo:02}-{d:02}T{h:02}:{mi:02}:{s:02}+00:00")
}
pub(crate) fn now_epoch_secs() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
pub(crate) fn humanize_duration(secs: i64) -> String {
if secs <= 0 {
return "now".to_string();
}
let mins = secs / 60;
let hours = mins / 60;
let days = hours / 24;
if days > 0 {
format!("{}d {}h", days, hours % 24)
} else if hours > 0 {
format!("{}h {}m", hours, mins % 60)
} else {
format!("{}m", mins.max(1))
}
}
#[cfg(test)]
#[path = "../../tests/inline/fetch.rs"]
mod tests;