Sets the global quota exceeded flag and logs a critical warning. Also determines the error type based on the message for adaptive cooldowns. Optionally accepts a model_id to flag specific model exhaustion.
(cls, message: str = "", model_id: Optional[str] = None)
| 98 | |
| 99 | @classmethod |
| 100 | def set_quota_exceeded(cls, message: str = "", model_id: Optional[str] = None): |
| 101 | """ |
| 102 | Sets the global quota exceeded flag and logs a critical warning. |
| 103 | Also determines the error type based on the message for adaptive cooldowns. |
| 104 | Optionally accepts a model_id to flag specific model exhaustion. |
| 105 | """ |
| 106 | if not cls.IS_QUOTA_EXCEEDED: |
| 107 | cls.IS_QUOTA_EXCEEDED = True |
| 108 | cls.QUOTA_EXCEEDED_TIMESTAMP = time.time() |
| 109 | cls.QUOTA_EXCEEDED_EVENT.set() |
| 110 | |
| 111 | # Determine error type |
| 112 | safe_message = message if message else "" |
| 113 | msg_lower = safe_message.lower() |
| 114 | if ( |
| 115 | "429" in msg_lower |
| 116 | or "rate limit" in msg_lower |
| 117 | or "resource has been exhausted" in msg_lower |
| 118 | ): |
| 119 | # API "RESOURCE_EXHAUSTED" usually means 429/quota shared behavior, |
| 120 | # but "rate limit" specifically implies a temporary 429. |
| 121 | # However, Gemini "Resource has been exhausted" is often a harder limit. |
| 122 | # Let's verify standard Gemini strings: |
| 123 | # "429: Too Many Requests" -> Rate Limit |
| 124 | # "429: Resource has been exhausted" -> Quota |
| 125 | if "too many requests" in msg_lower: |
| 126 | cls.last_error_type = "RATE_LIMIT" |
| 127 | else: |
| 128 | cls.last_error_type = "QUOTA_EXCEEDED" |
| 129 | elif "quota" in msg_lower: |
| 130 | cls.last_error_type = "QUOTA_EXCEEDED" |
| 131 | else: |
| 132 | # Default fallback if unknown |
| 133 | cls.last_error_type = "QUOTA_EXCEEDED" |
| 134 | |
| 135 | # [FIX] If model_id is provided, immediately mark it as exhausted so rotation logic knows |
| 136 | if model_id and cls.last_error_type == "QUOTA_EXCEEDED": |
| 137 | cls.current_profile_exhausted_models.add(model_id.lower()) |
| 138 | logger.warning(f"⛔ Identified specific model exhaustion: {model_id}") |
| 139 | |
| 140 | logger.critical( |
| 141 | f"⛔ GLOBAL ALERT: Quota Exceeded! Type: {cls.last_error_type} (Event Signal Sent)" |
| 142 | ) |
| 143 | |
| 144 | @classmethod |
| 145 | def reset_quota_status(cls): |