From 39b1bf7b62647f986984c536717f5859730d85c4 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Wed, 1 Apr 2026 23:51:27 +0200 Subject: [PATCH] fix(ai): detect anthropic request_too_large overflow closes #2734 --- packages/ai/CHANGELOG.md | 4 ++++ packages/ai/src/utils/overflow.ts | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/ai/CHANGELOG.md b/packages/ai/CHANGELOG.md index f2adf6c6..d9a7d9f1 100644 --- a/packages/ai/CHANGELOG.md +++ b/packages/ai/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +- Fixed Anthropic context overflow detection to recognize HTTP 413 `request_too_large` errors, so callers can trigger compaction and retry instead of getting stuck on repeated oversized-image requests ([#2734](https://github.com/badlogic/pi-mono/issues/2734)) + ## [0.64.0] - 2026-03-29 ### Added diff --git a/packages/ai/src/utils/overflow.ts b/packages/ai/src/utils/overflow.ts index d39f16dc..314febf2 100644 --- a/packages/ai/src/utils/overflow.ts +++ b/packages/ai/src/utils/overflow.ts @@ -9,6 +9,7 @@ import type { AssistantMessage } from "../types.js"; * Provider-specific patterns (with example error messages): * * - Anthropic: "prompt is too long: 213462 tokens > 200000 maximum" + * - Anthropic: "413 {\"error\":{\"type\":\"request_too_large\",\"message\":\"Request exceeds the maximum size\"}}" * - OpenAI: "Your input exceeds the context window of this model" * - Google: "The input token count (1196265) exceeds the maximum number of tokens allowed (1048575)" * - xAI: "This model's maximum prompt length is 131072 but the request contains 537812 tokens" @@ -25,7 +26,8 @@ import type { AssistantMessage } from "../types.js"; * - Ollama: Some deployments truncate silently, others return errors like "prompt too long; exceeded max context length by X tokens" */ const OVERFLOW_PATTERNS = [ - /prompt is too long/i, // Anthropic + /prompt is too long/i, // Anthropic token overflow + /request_too_large/i, // Anthropic request byte-size overflow (HTTP 413) /input is too long for requested model/i, // Amazon Bedrock /exceeds the context window/i, // OpenAI (Completions & Responses API) /input token count.*exceeds the maximum/i, // Google (Gemini) @@ -73,7 +75,7 @@ const NON_OVERFLOW_PATTERNS = [ * ## Reliability by Provider * * **Reliable detection (returns error with detectable message):** - * - Anthropic: "prompt is too long: X tokens > Y maximum" + * - Anthropic: "prompt is too long: X tokens > Y maximum" or "request_too_large" * - OpenAI (Completions & Responses): "exceeds the context window" * - Google Gemini: "input token count exceeds the maximum" * - xAI (Grok): "maximum prompt length is X but request contains Y"