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
This commit is contained in:
Glucksberg 2026-01-30 00:22:33 +00:00
parent 01e0d3a320
commit 2194d21124
2 changed files with 15 additions and 0 deletions

View File

@ -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);

View File

@ -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"))
);
}