fix(ai): don't treat 429 throttles as context overflow (#59649)

This commit is contained in:
Aiden Cline
2026-10-27 11:47:26 -05:00
committed by GitHub
Unverified
parent 96ae220b01
commit 618b890667
2 changed files with 20 additions and 2 deletions
+5 -2
View File
@@ -176,8 +176,9 @@ const AUTH_TEXT = /incorrect api key provided/i
const RATE_LIMIT_TEXT = /rate increased too quickly|rate[-_\s]?limit|too[_\s]?many[_\s]?requests/i
// Only consulted on 429, where throttles and account caps share a status.
// Z.ai reports balance, plan expiry, plan limits, and plan model access on 429.
// Cerebras reports its per-minute token throttle as `token_quota_exceeded`; that is a rate limit.
const QUOTA_TEXT =
/insufficient[-_\s]?(?:quota|balance)|quota[-_\s]?exceeded|budget exceeded|usage limit|limit exhausted|package has expired|plan does not yet include/i
/insufficient[-_\s]?(?:quota|balance)|(?<!token_)quota[-_\s]?exceeded|budget exceeded|usage limit|limit exhausted|package has expired|plan does not yet include/i
// Policy rejections without a dedicated code, matched against the provider's own
// explanation only. OpenAI reuses `invalid_prompt` for usage-policy rejections while
// Bedrock Mantle reuses it for schema validation; Anthropic reports blocked output
@@ -262,7 +263,9 @@ export function classifyProviderFailure(input: ProviderFailure): AIError["reason
(codes.includes("context_length_exceeded") ||
codes.includes("model_context_window_exceeded") ||
codes.includes("request_too_large") ||
(!codes.includes("throttlingexception") && isContextOverflow(text)))
// 429 bodies are throttles even when their wording matches an overflow phrase, e.g. Cerebras's
// "too many tokens processed". Text matching stays on for 4xx and mid-stream errors without a status.
(input.status !== 429 && !codes.includes("throttlingexception") && isContextOverflow(text)))
)
return new InvalidRequestError({ ...details, classification: "context-overflow" })
if (input.status === 413 || isPayloadTooLarge(text))
+15
View File
@@ -449,6 +449,21 @@ describe("provider error classification", () => {
expect(isContextOverflow("Too many tokens")).toBe(true)
})
test("classifies 429 token throttles as retryable rate limits, not context overflow", () => {
const bodies = [
// Cerebras, captured live
'{"message":"Tokens per minute limit exceeded - too many tokens processed.","type":"too_many_tokens_error","param":"quota","code":"token_quota_exceeded"}',
'{"message":"Token limit reached: too many tokens per minute for this model."}',
]
for (const body of bodies) {
const error = new AIError({
reason: classifyProviderFailure({ message: providerErrorMessage(body), status: 429, rawBody: body }),
})
expect(error.reason._tag).toBe("RateLimit")
expect(isRetryable(error)).toBe(true)
}
})
test("does not infer context overflow from known throttling exceptions", () => {
const cases = [
{ status: 429, data: { code: "ThrottlingException" } },