Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions codex-rs/cli/src/usage_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use codex_backend_client::AccountEntry;
use codex_backend_client::Client as BackendClient;
use codex_core::auth_profile_usage::AuthProfileUsageHealth;
use codex_core::auth_profile_usage::TokenUsageProfileResponse;
use codex_core::auth_profile_usage::usage_health_for_snapshots;
use codex_core::auth_profile_usage::usage_assessment_for_snapshots;
use codex_core::config::AuthProfileAutoSwitchConfig;
use codex_core::config::Config;
use codex_login::AuthManager;
Expand Down Expand Up @@ -170,6 +170,21 @@ struct CliUsageHealth {
remaining_percent: Option<f64>,
resets_at: Option<i64>,
reason: Option<&'static str>,
/// Lanes that still have capacity, best-remaining first.
///
/// A profile is reported healthy when any lane has capacity, so this names which model
/// to route to. It is empty when the profile is exhausted.
usable_lanes: Vec<CliUsageLane>,
}

/// One rate-limit lane with capacity left, as reported to the operator.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct CliUsageLane {
limit_id: Option<String>,
limit_name: Option<String>,
remaining_percent: f64,
resets_at: Option<i64>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -646,7 +661,19 @@ impl CliUsageHealth {
snapshots: &[RateLimitSnapshot],
config: &AuthProfileAutoSwitchConfig,
) -> Self {
match usage_health_for_snapshots(snapshots, config) {
let assessment = usage_assessment_for_snapshots(snapshots, config);
let usable_lanes = assessment
.usable_lanes()
.into_iter()
.map(|lane| CliUsageLane {
limit_id: lane.limit_id.clone(),
limit_name: lane.limit_name.clone(),
remaining_percent: lane.remaining_percent,
resets_at: lane.resets_at,
})
.collect::<Vec<_>>();

match assessment.health {
AuthProfileUsageHealth::Healthy {
remaining_percent,
resets_at,
Expand All @@ -655,18 +682,24 @@ impl CliUsageHealth {
remaining_percent: Some(remaining_percent),
resets_at,
reason: None,
usable_lanes,
},
AuthProfileUsageHealth::Exhausted { retry_at } => Self {
AuthProfileUsageHealth::Exhausted {
remaining_percent,
retry_at,
} => Self {
status: CliUsageHealthStatus::Exhausted,
remaining_percent: Some(0.0),
remaining_percent: Some(remaining_percent),
resets_at: retry_at,
reason: None,
usable_lanes,
},
AuthProfileUsageHealth::Unknown => Self {
status: CliUsageHealthStatus::Unknown,
remaining_percent: None,
resets_at: None,
reason: Some("unsupported_or_missing_usage_windows"),
usable_lanes,
},
}
}
Expand Down Expand Up @@ -1037,7 +1070,13 @@ mod tests {
"status": "healthy",
"remainingPercent": 20.0,
"resetsAt": 100,
"reason": null
"reason": null,
"usableLanes": [{
"limitId": "codex",
"limitName": null,
"remainingPercent": 20.0,
"resetsAt": 100
}]
})
);
}
Expand Down
Loading
Loading