diff --git a/packages/ai/src/provider-error.ts b/packages/ai/src/provider-error.ts index cf1786dc956..5f6574846e8 100644 --- a/packages/ai/src/provider-error.ts +++ b/packages/ai/src/provider-error.ts @@ -16,6 +16,8 @@ import { const patterns = [ /prompt is too long/i, /input is too long for requested model/i, + // Cloudflare Workers AI reports this as HTTP 413. + /exceeded this model context window limit/i, /exceeds the context window/i, /exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i, /input token count.*exceeds the maximum/i, diff --git a/packages/ai/test/provider-error.test.ts b/packages/ai/test/provider-error.test.ts index fabdf35ec39..52599f8ae3f 100644 --- a/packages/ai/test/provider-error.test.ts +++ b/packages/ai/test/provider-error.test.ts @@ -355,6 +355,17 @@ describe("provider error rawBody classification", () => { } }) + test("classifies Workers AI context window rejections as context overflow despite HTTP 413", () => { + const message = + "AiError: Ai: The estimated number of input and maximum output tokens (900047) exceeded this model context window limit (32000). (17851589-f2e6-405d-b67c-5550e52ee1c1)" + const reason = classifyProviderFailure({ + message, + status: 413, + rawBody: JSON.stringify({ errors: [{ message, code: 5021 }], success: false, result: {}, messages: [] }), + }) + expect(reason._tag === "InvalidRequest" ? reason.classification : reason._tag).toBe("context-overflow") + }) + test("classifies Google invalid API keys as authentication failures", () => { const rawBody = JSON.stringify({ error: {