fix(token): reset consumed when token enters cooling state

- Reset consumed to 0 when quota reaches 0 in default mode (consume())
- Reset consumed to 0 when 429 rate limit is triggered (mark_rate_limited())
- Remove consumed reset on API sync, only restore active status
- This ensures consumed represents 'usage count per cooling cycle'
This commit is contained in:
Jensen Zhang committed 2026-03-12 08:24:54 +00:00
1 parent 3e607e9f7c
commit 2ea3d9f2fd
2 files changed
+7 -7

No files matched your search

+5 -6
View File
@@ -673,6 +673,7 @@ class TokenManager:
old_quota = token.quota
token.quota = 0
token.status = TokenStatus.COOLING
token.consumed = 0 # 进入冷却时重置本轮消耗
logger.warning(
f"Token {raw_token[:10]}...: marked as rate limited "
f"(quota {old_quota} -> 0, status -> cooling)"
@@ -959,15 +960,13 @@ class TokenManager:
if consumed_mode:
# Consumed 模式:使用新逻辑
token_info.update_quota_with_consumed(new_quota)
# 刷新成功后重置消耗记录
if new_quota > 0:
token_info.consumed = 0
else:
# 默认模式:使用旧逻辑
token_info.update_quota(new_quota)
# 刷新成功后如果 quota > 0,清除冷却状态
if new_quota > 0:
token_info.status = TokenStatus.ACTIVE
# 刷新成功后如果 quota > 0,恢复活跃状态
if new_quota > 0:
token_info.status = TokenStatus.ACTIVE
token_info.mark_synced()
window_size = self._extract_window_size_seconds(result)
+2 -1
View File
@@ -136,9 +136,10 @@ class TokenInfo(BaseModel):
self.use_count += actual_cost
self.quota = max(0, self.quota - actual_cost)
# 默认行为:quota 耗尽时标记冷却
# 默认行为:quota 耗尽时标记冷却,并重置消耗记录
if self.quota == 0:
self.status = TokenStatus.COOLING
self.consumed = 0 # 进入冷却时重置本轮消耗
elif self.status == TokenStatus.COOLING:
# 只从 COOLING 恢复,不从 EXPIRED 恢复
self.status = TokenStatus.ACTIVE