persisting credits if new snapshot does not contain credit info (#7490)

in response to incoming changes to responses headers where the header
may sometimes not contain credits info (no longer forcing a credit
check)
This commit is contained in:
zhao-oai
2025-12-02 16:23:24 -05:00
committed by GitHub
Unverified
parent f6a7da4ac3
commit 5ebdc9af1b
4 changed files with 149 additions and 2 deletions
+15 -1
View File
@@ -62,7 +62,10 @@ impl SessionState {
}
pub(crate) fn set_rate_limits(&mut self, snapshot: RateLimitSnapshot) {
self.latest_rate_limits = Some(snapshot);
self.latest_rate_limits = Some(merge_rate_limit_credits(
self.latest_rate_limits.as_ref(),
snapshot,
));
}
pub(crate) fn token_info_and_rate_limits(
@@ -79,3 +82,14 @@ impl SessionState {
self.history.get_total_token_usage()
}
}
// Sometimes new snapshots don't include credits
fn merge_rate_limit_credits(
previous: Option<&RateLimitSnapshot>,
mut snapshot: RateLimitSnapshot,
) -> RateLimitSnapshot {
if snapshot.credits.is_none() {
snapshot.credits = previous.and_then(|prior| prior.credits.clone());
}
snapshot
}