#20539: fix: detect additional context overflow error patterns to prevent leak to user
agents
size: XS
Cluster:
Context Management Enhancements
## Problem
Fixes #9951
The context overflow error format used by some providers:
> LLM request rejected: input length and max_tokens exceed context limit: 170636 + 34048 > 200000, decrease input length or max_tokens and try again
was not caught by `isContextOverflowError()`. It fell through to `formatAssistantErrorText()`'s `invalidRequest` regex fallback, which returned the raw error verbatim as `LLM request rejected: ...` — leaking internal token counts and context limits to the user.
## Solution
Add three new patterns to `isContextOverflowError()`:
1. `"exceed context limit"` — direct substring match
2. `"exceeds the model's maximum context"` — alternate provider wording
3. `max_tokens` / `input length` + `exceed` + `context` — compound match for variations
These errors are now rewritten to the standard friendly message:
> Context overflow: prompt too large for the model. Try /reset (or /new) to start a fresh session, or use a larger-context model.
## Impact
Since `isContextOverflowError()` is checked before the `invalidRequest` regex in `formatAssistantErrorText()`, these patterns are now caught early and never leak raw details.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added four new error pattern detections to `isContextOverflowError()` to catch provider error formats that were previously falling through to the generic error handler, which leaked internal token counts and context limits to users. The new patterns detect errors containing "exceed context limit", "exceeds the model's maximum context", and compound patterns matching `max_tokens`/`input length` + `exceed` + `context`.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The changes add four new substring checks to an existing error detection function. The logic is straightforward and defensive - the compound patterns require multiple keywords to match, reducing false positive risk. The change prevents information leakage by catching errors earlier in the error handling flow, which is a security improvement. No breaking changes or complex logic introduced.
- No files require special attention
<sub>Last reviewed commit: 3e36a0f</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#15749: fix: improve context overflow error with diagnostic details
by superlowburn · 2026-02-13
82.2%
#18997: fix: improve context overflow error messages and docs
by realhoratiobot · 2026-02-17
78.9%
#8661: fix: display rate limit errors correctly instead of as context over...
by dbottme · 2026-02-04
78.7%
#22855: Add Chinese context overflow patterns to isContextOverflowError
by Clawborn · 2026-02-21
77.9%
#9562: fix: detect Kimi K2.5 context overflow error (model token limit)
by danilofalcao · 2026-02-05
77.2%
#6353: fix(agents): detect Anthropic 'exceed context limit' error for auto...
by Glucksberg · 2026-02-01
77.1%
#23136: fix: lookupContextTokens should handle provider/model refs
by patchguardio · 2026-02-22
76.3%
#16176: Fix Groq TPM limits misidentified as context overflow
by dddabtc · 2026-02-14
75.5%
#17604: fix(context): use getAvailable() to prevent cross-provider model ID...
by aldoeliacim · 2026-02-16
75.1%
#16962: fix: make auth error detection contextual to prevent false positives
by StressTestor · 2026-02-15
74.6%