MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / increment_token_count

Method increment_token_count

config/global_state.py:162–205  ·  view source on GitHub ↗

Increments the token count for the current profile and checks if it exceeds the limit. [GR-01] Implements Graceful Rotation logic.

(cls, count: int, model_id: str = "default")

Source from the content-addressed store, hash-verified

160
161 @classmethod
162 def increment_token_count(cls, count: int, model_id: str = "default"):
163 """
164 Increments the token count for the current profile and checks if it exceeds the limit.
165 [GR-01] Implements Graceful Rotation logic.
166 """
167 if count <= 0:
168 return
169
170 # Ensure model_id is a valid string for key usage
171 safe_model_id = model_id if model_id else "default"
172 model_key = safe_model_id.lower()
173
174 cls.current_profile_model_usage[model_key] += count
175 current_usage = cls.current_profile_model_usage[model_key]
176
177 # Retrieve limit (fallback to global hard limit)
178 limit = MODEL_QUOTA_LIMITS.get(model_key, QUOTA_HARD_LIMIT)
179
180 # Check Hard Limit (Emergency Kill / Model Exhaustion)
181 if current_usage >= limit:
182 logger.critical(
183 f"⛔ HARD LIMIT REACHED ({model_key}): {current_usage} >= {limit}. Marking model as exhausted."
184 )
185 cls.current_profile_exhausted_models.add(model_key)
186 # Trigger global rotation signal
187 cls.set_quota_exceeded(message=f"Quota exceeded for model {model_key}")
188 # Raise exception to propagate up to request processor
189 raise QuotaExceededError(
190 f"Quota exceeded for model {model_key} ({current_usage} >= {limit})"
191 )
192
193 # Check Soft Limit (Graceful Signal)
194 # Note: Using global soft limit as baseline for rotation signal
195 if current_usage >= QUOTA_SOFT_LIMIT and not cls.NEEDS_ROTATION:
196 logger.warning(
197 f"🔄 SOFT LIMIT REACHED ({model_key}): {current_usage} >= {QUOTA_SOFT_LIMIT}. Setting NEEDS_ROTATION flag."
198 )
199 cls.NEEDS_ROTATION = True
200
201 # Log status
202 limit_str = f"{QUOTA_SOFT_LIMIT}(Soft)/{limit}(Hard)"
203 logger.info(
204 f"📊 Token usage updated ({model_key}): +{count} => {current_usage} (Limits: {limit_str}) | Rotation Pending: {cls.NEEDS_ROTATION}"
205 )

Calls 5

QuotaExceededErrorClass · 0.90
set_quota_exceededMethod · 0.80
warningMethod · 0.80
infoMethod · 0.80
getMethod · 0.45