Skip to content

fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction#6353

Closed
Glucksberg wants to merge 1 commit intoopenclaw:mainfrom
Glucksberg:fix/anthropic-context-limit-4266
Closed

fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction#6353
Glucksberg wants to merge 1 commit intoopenclaw:mainfrom
Glucksberg:fix/anthropic-context-limit-4266

Conversation

@Glucksberg
Copy link
Contributor

@Glucksberg Glucksberg commented Feb 1, 2026

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:

input length and max_tokens exceed context limit: 156321 + 48384 > 200000

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

  • This PR is likely safe to merge; it adds a narrow error-pattern match and corresponding tests.
  • The code change is a simple additional substring check in a well-scoped helper, and tests cover representative raw/plain-text variants. Primary risk is around the doc marker syntax change potentially rendering unintended text in docs builds.
  • docs/concepts/model-providers.md, docs/providers/moonshot.md

@openclaw-barnacle openclaw-barnacle bot added docs Improvements or additions to documentation agents Agent runtime and tooling labels Feb 1, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +137 to +143
{/_ 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 _/}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Comment on lines +51 to +63
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);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

…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
@vincentkoc
Copy link
Member

vincentkoc commented Feb 23, 2026

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 exceed context limit detection), you will be credited as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants