Skip to content

The agent that grows with you

fix(classifier): 429 quota walls route to billing across providers; reset signals stay rate-limited

Teknium0c3a5075350c
diff --git a/agent/error_classifier.py b/agent/error_classifier.py
index ea24a17c6bb..39e8ed5c527 100644
--- a/agent/error_classifier.py
+++ b/agent/error_classifier.py
@@ -245,10 +245,15 @@ _USAGE_LIMIT_TRANSIENT_SIGNALS = [
"retry",
"resets at",
"reset in",
+ "resets in",
+ "reset after",
+ "available in",
"wait",
"requests remaining",
"periodic",
"window",
+ "per minute",
+ "per second",
]
# Payload-too-large patterns detected from message text (no status_code attr).
@@ -1346,17 +1351,37 @@ def _classify_by_status(
# branch below used to retry it and Desktop rendered a provider error
# instead of the billing/quota recovery. Preserve periodic quotas when
# the response supplies an explicit reset/retry signal.
+ #
+ # The check covers the narrow #93419 core (Anthropic's
+ # ``usage_limit_reached``) plus the broader ``_USAGE_LIMIT_PATTERNS``
+ # ("quota", "limit exceeded", "key limit exceeded") so other providers'
+ # hard quota walls also route to billing — but ONLY when the message is
+ # not itself an explicit rate-limit phrase. Without that guard,
+ # "Rate limit exceeded" ("limit exceeded" substring) would wrongly
+ # promote to non-retryable billing. (broadening + guard credit #39441)
has_usage_limit = (
error_code.lower() == "usage_limit_reached"
- or "usage limit" in error_msg
or "usage_limit_reached" in error_msg
+ or any(p in error_msg for p in _USAGE_LIMIT_PATTERNS)
+ )
+ # Explicit billing phrases in a 429 body are a hard wall regardless of
+ # usage-limit wording — a provider that wraps "insufficient credits" in
+ # a 429 (rather than 402) was previously retried as a rate limit and
+ # burned the pool. (credit #39441)
+ has_billing = any(p in error_msg for p in _BILLING_PATTERNS)
+ has_explicit_rate_limit = any(
+ p in error_msg for p in _RATE_LIMIT_PATTERNS
)
has_transient_signal = _has_usage_limit_transient_signal(
error_msg,
body,
response_headers,
)
- if has_usage_limit and not has_transient_signal:
+ if (
+ (has_billing or has_usage_limit)
+ and not has_explicit_rate_limit
+ and not has_transient_signal
+ ):
return result_fn(
FailoverReason.billing,
retryable=False,
diff --git a/tests/agent/test_error_classifier.py b/tests/agent/test_error_classifier.py
index 432933171bf..56817ffd696 100644
--- a/tests/agent/test_error_classifier.py
+++ b/tests/agent/test_error_classifier.py
@@ -355,6 +355,62 @@ class TestClassifyApiError:
assert result.reason == FailoverReason.rate_limit
assert result.retryable is True
+ def test_429_generic_quota_wall_is_billing(self):
+ # Broadened from the narrow "usage limit" core to the full
+ # _USAGE_LIMIT_PATTERNS: a bare "quota" / "limit exceeded" 429 with no
+ # reset signal is a hard wall, not a retryable throttle. (credit #39441)
+ for msg in ("Monthly quota reached.", "API key limit exceeded."):
+ e = MockAPIError(msg, status_code=429)
+ result = classify_api_error(e, provider="groq", model="llama-3")
+ assert result.reason == FailoverReason.billing, msg
+ assert result.retryable is False, msg
+
+ def test_429_insufficient_credits_is_billing(self):
+ e = MockAPIError("Insufficient credits remaining.", status_code=429)
+ result = classify_api_error(e, provider="openrouter", model="x")
+ assert result.reason == FailoverReason.billing
+ assert result.retryable is False
+
+ def test_429_rate_limit_phrase_never_promotes_to_billing(self):
+ # The exclusion guard: "Rate limit exceeded" contains the
+ # "limit exceeded" usage-limit substring, but an explicit rate-limit
+ # phrase must stay a retryable rate limit. (guard credit #39441)
+ for msg in (
+ "Rate limit exceeded, please slow down.",
+ "Too many requests; rate_limit hit.",
+ ):
+ e = MockAPIError(msg, status_code=429)
+ result = classify_api_error(e, provider="anthropic", model="claude-opus-5")
+ assert result.reason == FailoverReason.rate_limit, msg
+ assert result.retryable is True, msg
+
+ def test_codex_weekly_usage_limit_resets_in_stays_rate_limit(self):
+ # Codex surfaces "Weekly usage limit reached. Resets in 6hr 29min."
+ # "resets in" was NOT a transient signal before, so this wrongly read
+ # as terminal billing. (transient-signal credit #63021)
+ e = MockAPIError(
+ "Weekly usage limit reached. Resets in 6hr 29min.",
+ status_code=429,
+ )
+ result = classify_api_error(e, provider="openai-codex", model="gpt-5-codex")
+ assert result.reason == FailoverReason.rate_limit
+ assert result.retryable is True
+
+ @pytest.mark.parametrize(
+ "phrase",
+ [
+ "usage limit reached, reset after 3600s",
+ "usage limit reached, available in 42 minutes",
+ "usage limit reached; 20 requests per minute",
+ ],
+ )
+ def test_429_usage_limit_with_extra_transient_phrases_stays_rate_limit(self, phrase):
+ # Additional transient signals. (credit #74785)
+ e = MockAPIError(phrase, status_code=429)
+ result = classify_api_error(e, provider="anthropic", model="claude-opus-5")
+ assert result.reason == FailoverReason.rate_limit
+ assert result.retryable is True
+
def test_alibaba_rate_increased_too_quickly(self):
"""Alibaba/DashScope returns a unique throttling message.