From 2194d21124d6b364f4e3f34f253f61ff0e2a1a93 Mon Sep 17 00:00:00 2001 From: Glucksberg Date: Fri, 30 Jan 2026 00:22:33 +0000 Subject: [PATCH] fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction Add detection for Anthropic's 'input length and max_tokens exceed context limit' error message to trigger auto-compaction. Without this fix, extended thinking sessions with large contexts would fail without attempting compaction. Fixes cases like: - 'input length and max_tokens exceed context limit: 156321 + 48384 > 200000' - JSON wrapped versions of the same error --- ...embedded-helpers.iscontextoverflowerror.test.ts | 14 ++++++++++++++ src/agents/pi-embedded-helpers/errors.ts | 1 + 2 files changed, 15 insertions(+) diff --git a/src/agents/pi-embedded-helpers.iscontextoverflowerror.test.ts b/src/agents/pi-embedded-helpers.iscontextoverflowerror.test.ts index 19165caa5..8a9e63c7a 100644 --- a/src/agents/pi-embedded-helpers.iscontextoverflowerror.test.ts +++ b/src/agents/pi-embedded-helpers.iscontextoverflowerror.test.ts @@ -40,6 +40,20 @@ describe("isContextOverflowError", () => { } }); + it("matches Anthropic 'input length and max_tokens exceed context limit' error", () => { + // Anthropic returns this error when input_tokens + max_tokens > context_window. + // This typically happens when extended thinking is enabled with a large context. + // Without this fix, auto-compaction is NOT triggered. + const samples = [ + "input length and max_tokens exceed context limit: 156321 + 48384 > 200000, decrease input length or max_tokens and try again", + '{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit: 158984 + 48384 > 200000"}', + "LLM request rejected: input length and max_tokens exceed context limit", + ]; + for (const sample of samples) { + expect(isContextOverflowError(sample)).toBe(true); + } + }); + it("ignores unrelated errors", () => { expect(isContextOverflowError("rate limit exceeded")).toBe(false); expect(isContextOverflowError("request size exceeds upload limit")).toBe(false); diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index 849c4293e..8dff8bf58 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -21,6 +21,7 @@ export function isContextOverflowError(errorMessage?: string): boolean { lower.includes("exceeds model context window") || (hasRequestSizeExceeds && hasContextWindow) || lower.includes("context overflow") || + lower.includes("exceed context limit") || // Anthropic: "input length and max_tokens exceed context limit" (lower.includes("413") && lower.includes("too large")) ); }