From d3398f826ae2654b52a7988830d533a354754c13 Mon Sep 17 00:00:00 2001 From: "robopipi[bot]" <336283572+robopipi[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 20:27:30 +0100 Subject: [PATCH] fix(ai): detect LiteLLM ContextWindowExceededError as context overflow (#11494) LiteLLM's pre-call context check rejects requests that are too large with HTTP 400 and a message like: ``` litellm.ContextWindowExceededError: litellm.BadRequestError: litellm._pre_call_checks: Context Window exceeded for given call. ... Max Input Tokens=262144, Got=265262 ... ``` None of the `OVERFLOW_PATTERNS` matched this wording, so `isContextOverflow()` returned false. As a result, the agent session never ran overflow compaction and retry, and the turn failed with the 400 error. This change adds `/context window exceeded/i` to `OVERFLOW_PATTERNS` and documents the error in the doc comment that lists providers. Testing: - Added a regression test in `packages/ai/test/overflow.test.ts` with the exact error message from the issue. It fails without the pattern and passes with it. - `node ../../node_modules/vitest/dist/cli.js --run test/overflow.test.ts` in `packages/ai`: 32 passed. - `npm run check` passes. - Running `isContextOverflow()` directly on the reported message returned `false` before this change and returns `true` after it. Fixes #11492 Requested by @rwachtler Co-authored-by: triagent --- packages/ai/src/utils/overflow.ts | 2 ++ packages/ai/test/overflow.test.ts | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/packages/ai/src/utils/overflow.ts b/packages/ai/src/utils/overflow.ts index 03f82f61b..36ae26b49 100644 --- a/packages/ai/src/utils/overflow.ts +++ b/packages/ai/src/utils/overflow.ts @@ -12,6 +12,7 @@ import type { AssistantMessage } from "../types.ts"; * - Anthropic: "413 {\"error\":{\"type\":\"request_too_large\",\"message\":\"Request exceeds the maximum size\"}}" * - OpenAI: "Your input exceeds the context window of this model" * - 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)." * - 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" @@ -42,6 +43,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) + /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) /reduce the length of the messages/i, // Groq diff --git a/packages/ai/test/overflow.test.ts b/packages/ai/test/overflow.test.ts index e7ae90548..1cab5e92e 100644 --- a/packages/ai/test/overflow.test.ts +++ b/packages/ai/test/overflow.test.ts @@ -91,6 +91,14 @@ describe("isContextOverflow", () => { expect(isContextOverflow(message, 131072)).toBe(true); }); + it("detects LiteLLM pre-call ContextWindowExceededError", () => { + // Regression for #11492. + const message = createErrorMessage( + '400: {"message":"litellm.ContextWindowExceededError: litellm.BadRequestError: litellm._pre_call_checks: Context Window exceeded for given call. No models have context window large enough for this call.\nModel=Qwen3.8-Flash-Next, Max Input Tokens=262144, Got=265262\nmodel=Qwen3.8-Flash-Next. context_window_fallbacks=None. fallbacks=[]."}', + ); + expect(isContextOverflow(message, 262144)).toBe(true); + }); + it("detects OpenAI-compatible parenthesized maximum context length errors", () => { const message = createErrorMessage( "Error: 400 Input length (265330) exceeds model's maximum context length (262144).",