mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
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 <triagent@localhost>
This commit is contained in:
parent
b3a29f1111
commit
d3398f826a
2 changed files with 10 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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).",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue