fix(providers): surface clear arrearage warning on quota/billing errors (#3006)

This commit is contained in:
04cb
2026-05-29 15:31:17 +08:00
committed by Xubin Ren
parent 672fabe5be
commit 9d3fe7c34b
4 changed files with 70 additions and 1 deletions
+8 -1
View File
@@ -53,6 +53,10 @@ from nanobot.utils.runtime import (
)
_DEFAULT_ERROR_MESSAGE = "Sorry, I encountered an error calling the AI model."
_ARREARAGE_ERROR_MESSAGE = (
"The AI provider rejected the request because the API key is out of quota or the "
"account is in arrears. Please top up / check the billing status of your API key and try again."
)
_PERSISTED_MODEL_ERROR_PLACEHOLDER = "[Assistant reply unavailable due to model error.]"
_MAX_EMPTY_RETRIES = 2
_MAX_LENGTH_RECOVERIES = 3
@@ -504,7 +508,10 @@ class AgentRunner:
continue
if response.finish_reason == "error":
final_content = clean or spec.error_message or _DEFAULT_ERROR_MESSAGE
if LLMProvider.is_arrearage_response(response):
final_content = _ARREARAGE_ERROR_MESSAGE
else:
final_content = clean or spec.error_message or _DEFAULT_ERROR_MESSAGE
stop_reason = "error"
error = final_content
self._append_model_error_placeholder(messages)
+23
View File
@@ -315,6 +315,29 @@ class LLMProvider(ABC):
return cls._is_transient_error(response.content)
@classmethod
def is_arrearage_response(cls, response: LLMResponse) -> bool:
"""Detect API-key arrearage / quota / billing errors that won't clear on retry.
These surface as HTTP 402 or as billing semantic tokens (e.g.
``insufficient_quota``, ``payment_required``); reuses the same token and
text markers the 429 retry policy treats as non-retryable.
"""
if response.error_status_code is not None and int(response.error_status_code) == 402:
return True
type_token = cls._normalize_error_token(response.error_type)
code_token = cls._normalize_error_token(response.error_code)
if any(
token in cls._NON_RETRYABLE_429_ERROR_TOKENS
for token in (type_token, code_token)
if token is not None
):
return True
content = (response.content or "").lower()
return any(marker in content for marker in cls._NON_RETRYABLE_429_TEXT_MARKERS)
@staticmethod
def _normalize_error_token(value: Any) -> str | None:
if value is None: