fix(ai): detect Bedrock Mantle context overflow errors

This commit is contained in:
Cristina Poncela Cubeiro 2026-10-28 15:45:03 +01:00
parent 024abf6dda
commit bcbad6f8b1
2 changed files with 10 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import type { AssistantMessage } from "../types.ts";
* - OpenAI/LiteLLM: "Requested token count exceeds the model's maximum context length of 131072 tokens"
* - LiteLLM pre-call check: "litellm.ContextWindowExceededError: ... Context Window exceeded for given call."
* - OpenAI-compatible: "Input length (265330) exceeds model's maximum context length (262144)."
* - Amazon Bedrock Mantle (OpenAI Responses): "invalid_prompt: prompt tokens (330095) exceed model maximum (278528) for base 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"
* - Groq: "Please reduce the length of the messages or completion"
@ -43,6 +44,7 @@ const OVERFLOW_PATTERNS = [
/input is too long for requested model/i, // Amazon Bedrock
/exceeds the context window/i, // OpenAI (Completions & Responses API)
/exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i, // OpenAI-compatible proxies (LiteLLM)
/prompt tokens \([\d,]+\) exceed model maximum/i, // Amazon Bedrock Mantle (OpenAI Responses observed error; exact text is not documented by AWS)
/context window exceeded/i, // LiteLLM pre-call check (ContextWindowExceededError)
/input token count.*exceeds the maximum/i, // Google (Gemini)
/maximum prompt length is \d+/i, // xAI (Grok)
@ -101,6 +103,7 @@ const NON_OVERFLOW_PATTERNS = [
* **Reliable detection (returns error with detectable message):**
* - Anthropic: "prompt is too long: X tokens > Y maximum" or "request_too_large"
* - OpenAI (Completions & Responses): "exceeds the context window", "exceeds the model's maximum context length of X tokens", or "exceeds model's maximum context length (X)"
* - Amazon Bedrock Mantle (OpenAI Responses): "invalid_prompt: prompt tokens (X) exceed model maximum (Y) for base model ..."
* - Google Gemini: "input token count exceeds the maximum"
* - xAI (Grok): "maximum prompt length is X but request contains Y"
* - Groq: "reduce the length of the messages"

View file

@ -106,6 +106,13 @@ describe("isContextOverflow", () => {
expect(isContextOverflow(message, 262144)).toBe(true);
});
it("detects Amazon Bedrock Mantle OpenAI Responses prompt-token maximum errors", () => {
const message = createErrorMessage(
"Amazon Bedrock Mantle OpenAI Responses API error (400): invalid_prompt: prompt tokens (330095) exceed model maximum (278528) for base model abc",
);
expect(isContextOverflow(message, 272000)).toBe(true);
});
it("detects OpenRouter Poolside maximum allowed input length errors", () => {
const message = createErrorMessage(
"Provider returned error: Input length 131393 exceeds the maximum allowed input length of 131040 tokens.",