#6353: fix(agents): detect Anthropic 'exceed context limit' error for auto-compaction
agents
size: XS
experienced-contributor
Cluster:
Context Management Enhancements
## 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_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
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.
<h3>Confidence Score: 4/5</h3>
- 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
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#18997: fix: improve context overflow error messages and docs
by realhoratiobot · 2026-02-17
81.8%
#15749: fix: improve context overflow error with diagnostic details
by superlowburn · 2026-02-13
80.0%
#10214: feat(compaction): Add Anthropic server-side compaction API support
by GodsBoy · 2026-02-06
78.3%
#9562: fix: detect Kimi K2.5 context overflow error (model token limit)
by danilofalcao · 2026-02-05
77.3%
#20539: fix: detect additional context overflow error patterns to prevent l...
by echoVic · 2026-02-19
77.1%
#10792: Agents: avoid context overflow false positives
by arunsanna · 2026-02-07
76.1%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
75.9%
#4042: agents: add proactive compaction before request
by freedomzt · 2026-01-29
75.9%
#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversa...
by hleliofficiel · 2026-02-10
75.8%
#5360: fix(compaction): add emergency pruning for context overflow
by sgwannabe · 2026-01-31
75.3%