fix(classifier): terminal_quota_exhausted 429s classify as billing, not rate_limit

Port from code-yeongyu/oh-my-openagent#6677 (credit: @niStee).

LiteLLM proxies stamp a structured `terminal_quota_exhausted` code on
hard-cap 429s. Hermes' `_status_429` handler always returns a verdict, so
`_by_error_code` (which maps _BILLING_ERROR_CODES to billing) never saw
the code: the exhausted key classified as rate_limit, earned the 429
cooldown, and got retried against a wall that cannot clear until someone
pays. Upstream this respawned duplicate subagent sessions.

- `_status_429` now honors a structured billing code first (decisive
  signal outranks message heuristics).
- `terminal_quota_exhausted` joins _BILLING_ERROR_CODES so every path
  (429, 402, status-less) agrees.
- "hard billing limit" free text joins _BILLING_PATTERNS ("billing hard
  limit" was already there; providers use both orders). "terminal billing
  limit" text is deliberately NOT matched: substring rules cannot negate
  the "non-terminal billing limit" wording — the structured code covers it.
This commit is contained in:
Teknium
2026-09-09 02:38:20 -07:00
parent 833594cda6
commit 92df11f81d
2 changed files with 35 additions and 1 deletions
+24
View File
@@ -539,6 +539,30 @@ class TestClassifyApiError:
assert result.reason == FailoverReason.rate_limit
assert result.should_rotate_credential is True
def test_429_with_structured_terminal_quota_code_is_billing(self):
"""LiteLLM stamps ``terminal_quota_exhausted`` on a hard-cap 429. The
429 handler always returns a verdict, so the structured billing code
must be honored inside it — otherwise the exhausted key is retried
(upstream this respawned duplicate subagents; ported from
code-yeongyu/oh-my-openagent#6677)."""
e = MockAPIError(
"request failed", status_code=429,
body={"error": {"code": "terminal_quota_exhausted", "message": "request failed"}},
)
result = classify_api_error(e)
assert result.reason == FailoverReason.billing
assert result.retryable is False
assert result.should_fallback is True
def test_429_hard_billing_limit_text_is_billing(self):
"""The free-text twin: "hard billing limit" is exhaustion wording, not
throttling, even though it contains no reset signal to disambiguate."""
result = classify_api_error(
MockAPIError("hard billing limit reached for this key", status_code=429)
)
assert result.reason == FailoverReason.billing
assert result.retryable is False
# ── 5xx that are actually request-validation errors ──
# Some OpenAI-compatible gateways (e.g. codex.nekos.me) return
# request-validation failures with a 5xx status. These are