fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction#6353
fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction#6353Glucksberg wants to merge 1 commit intoopenclaw:mainfrom
Conversation
docs/concepts/model-providers.md
Outdated
| {/_ moonshot-kimi-k2-model-refs:start _/} | ||
| - `moonshot/kimi-k2.5` | ||
| - `moonshot/kimi-k2-0905-preview` | ||
| - `moonshot/kimi-k2-turbo-preview` | ||
| - `moonshot/kimi-k2-thinking` | ||
| - `moonshot/kimi-k2-thinking-turbo` | ||
| {/* moonshot-kimi-k2-model-refs:end */} | ||
| {/_ moonshot-kimi-k2-model-refs:end _/} |
There was a problem hiding this comment.
[P1] These {/_ ... _/} markers look like they were intended to be JSX/MDX comments ({/* ... */}), but in plain Markdown they’ll render as literal text. If these are used by a docs generation/templating step, please confirm it recognizes {/_ ... _/}; otherwise this change will visibly break the docs.
Also appears in: docs/providers/moonshot.md (around the “Current Kimi K2 model IDs” block). Are these markers consumed by a preprocessor (and if so, which one), or should they remain as hidden comments in the rendered docs?
Prompt To Fix With AI
This is a comment left during a code review.
Path: docs/concepts/model-providers.md
Line: 137:143
Comment:
[P1] These `{/_ ... _/}` markers look like they were intended to be JSX/MDX comments (`{/* ... */}`), but in plain Markdown they’ll render as literal text. If these are used by a docs generation/templating step, please confirm it recognizes `{/_ ... _/}`; otherwise this change will visibly break the docs.
Also appears in: `docs/providers/moonshot.md` (around the “Current Kimi K2 model IDs” block). Are these markers consumed by a preprocessor (and if so, which one), or should they remain as hidden comments in the rendered docs?
How can I resolve this? If you propose a fix, please make it concise.| describe("isContextOverflowError - Anthropic extended thinking", () => { | ||
| it("matches Anthropic 'exceed context limit' error from extended thinking", () => { | ||
| // When using extended thinking with large contexts, Anthropic returns this error: | ||
| // "input length and max_tokens exceed context limit: 156321 + 48384 > 200000" | ||
| const samples = [ | ||
| "input length and max_tokens exceed context limit: 156321 + 48384 > 200000", | ||
| '{"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit: 100000 + 16000 > 100000"}}', | ||
| "exceed context limit: 50000 + 10000 > 50000", | ||
| ]; | ||
| for (const sample of samples) { | ||
| expect(isContextOverflowError(sample)).toBe(true); | ||
| } | ||
| }); |
There was a problem hiding this comment.
[P2] The new tests duplicate the existing describe("isContextOverflowError", ...) coverage and split provider-specific cases into a second describe, which makes it slightly harder to see all overflow patterns in one place. Consider folding these new samples into the existing “matches known overflow hints”/Anthropic sections (or renaming the new describe to match the existing structure) to keep the test suite organized as the list grows.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers.iscontextoverflowerror.test.ts
Line: 51:63
Comment:
[P2] The new tests duplicate the existing `describe("isContextOverflowError", ...)` coverage and split provider-specific cases into a second `describe`, which makes it slightly harder to see all overflow patterns in one place. Consider folding these new samples into the existing “matches known overflow hints”/Anthropic sections (or renaming the new `describe` to match the existing structure) to keep the test suite organized as the list grows.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.bfc1ccb to
f92900f
Compare
…ompaction When using extended thinking with large contexts, Anthropic returns: 'input length and max_tokens exceed context limit: 156321 + 48384 > 200000' Without detection, auto-compaction is not triggered and the request fails. Added 'exceed context limit' pattern to isContextOverflowError(). Closes openclaw#4266
|
Thanks for pushing this early fix. I’m closing this in favor of #20539 as the canonical path. It now includes the broader overflow-pattern coverage plus dedicated regression tests/changelog updates, so we only need one merge path for this failure mode. Your work directly informed the final shape (especially around |
Summary
Detect Anthropic's 'input length and max_tokens exceed context limit' error to trigger auto-compaction.
Problem
When using extended thinking with large contexts, Anthropic returns this specific error:
Without detection, auto-compaction is not triggered and the request fails.
Solution
Add 'exceed context limit' pattern matching in
isContextOverflowError().Testing
Added test cases for the new error format.
Supersedes #4266
Greptile Overview
Greptile Summary
This PR extends
isContextOverflowError()to recognize Anthropic’s extended-thinking context overflow error string ("…exceed context limit…"), and adds Vitest coverage for the new format. There are also small docs formatting tweaks in provider docs/onboarding/canvas docs.The main behavior change is in
src/agents/pi-embedded-helpers/errors.ts, where additional substring matching allows auto-compaction logic to treat the new Anthropic error as a context overflow and retry accordingly.Confidence Score: 4/5