diff --git a/packages/ai/CHANGELOG.md b/packages/ai/CHANGELOG.md index ed673f1a..da2ea33d 100644 --- a/packages/ai/CHANGELOG.md +++ b/packages/ai/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +- Fixed OpenRouter/Poolside context overflow detection for `maximum allowed input length` errors ([#4943](https://github.com/earendil-works/pi/issues/4943)). + ## [0.75.5] - 2026-05-23 ### Breaking Changes diff --git a/packages/ai/src/utils/overflow.ts b/packages/ai/src/utils/overflow.ts index 8b908e85..e3a9b37a 100644 --- a/packages/ai/src/utils/overflow.ts +++ b/packages/ai/src/utils/overflow.ts @@ -16,6 +16,7 @@ import type { AssistantMessage } from "../types.ts"; * - 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" * - OpenRouter: "This endpoint's maximum context length is X tokens. However, you requested about Y tokens" + * - OpenRouter/Poolside: "Input length X exceeds the maximum allowed input length of Y tokens." * - Together AI: "The input (X tokens) is longer than the model's context length (Y tokens)." * - llama.cpp: "the request exceeds the available context size, try increasing it" * - LM Studio: "tokens to keep from the initial prompt is greater than the context length" @@ -39,7 +40,8 @@ const OVERFLOW_PATTERNS = [ /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 - /maximum context length is \d+ tokens/i, // OpenRouter (all backends) + /maximum context length is \d+ tokens/i, // OpenRouter (most backends) + /exceeds (?:the )?maximum allowed input length of [\d,]+ tokens?/i, // OpenRouter/Poolside /input \(\d+ tokens\) is longer than the model'?s context length \(\d+ tokens\)/i, // Together AI /exceeds the limit of \d+/i, // GitHub Copilot /exceeds the available context size/i, // llama.cpp server @@ -89,7 +91,8 @@ const NON_OVERFLOW_PATTERNS = [ * - Groq: "reduce the length of the messages" * - Cerebras: 400/413 status code (no body) * - Mistral: "Prompt contains X tokens ... too large for model with Y maximum context length" - * - OpenRouter (all backends): "maximum context length is X tokens" + * - OpenRouter (most backends): "maximum context length is X tokens" + * - OpenRouter/Poolside: "Input length X exceeds the maximum allowed input length of Y tokens." * - Together AI: "The input (X tokens) is longer than the model's context length (Y tokens)." * - llama.cpp: "exceeds the available context size" * - LM Studio: "greater than the context length" diff --git a/packages/ai/test/overflow.test.ts b/packages/ai/test/overflow.test.ts index b26948e2..346d2006 100644 --- a/packages/ai/test/overflow.test.ts +++ b/packages/ai/test/overflow.test.ts @@ -49,6 +49,13 @@ describe("isContextOverflow", () => { expect(isContextOverflow(message, 131072)).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.", + ); + expect(isContextOverflow(message, 131072)).toBe(true); + }); + it("does not treat generic non-overflow Ollama errors as overflow", () => { const message = createErrorMessage("500 `model runner crashed unexpectedly`"); expect(isContextOverflow(message, 32768)).toBe(false);