Repository navigation
fix(model): treat Anthropic context window exhaustion as truncation - #4567
Merged
jujn merged 1 commit intoDec 2, 2026
Merged
Conversation
Anthropic reports stop_reason "model_context_window_exceeded" when the response fills the model's context window instead of reaching its own output limit, and the API guidance is to treat that response as truncated. FinishReasons.indicatesOutputLimit only recognized "length", "max_tokens" and the Responses API "incomplete" cause. ConversationCompactor uses this helper to reject a summary that stopped at a limit and keep the original history, so a summary cut at the window was accepted as complete and replaced the history it was summarizing. Memory consolidation and the reasoning truncation flag read the same helper.
jujn
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.1.0-SNAPSHOT, based on
mainat7b87b5969.Description
Anthropic reports
model_context_window_exceededasstop_reasonwhen the response fills the model's context window instead of reaching its own output limit, and their stop-reason guidance lists it next tomax_tokensas a truncated response.FinishReasons.indicatesOutputLimitrecognizedlength,max_tokensand the Responses APIincompletecause only.That helper decides whether a compaction summary may be used.
ConversationCompactor.toValidatedSummaryrejects a summary that stopped at a limit, and the rejection falls through tolightweightResult, which keeps the original history. With this reason unrecognized, a summary cut at the window was accepted as complete and replaced the history it was summarizing.MemoryConsolidatorandReasoningContext.isTruncatedread the same helper.Checked the other adapters for the same gap: DashScope reports
length, Gemini reportsMAX_TOKENS, OpenAI Chat Completions reportslength, and the Responses API reportsincompletewith the cause in metadata. Anthropic is the only adapter with this value, and the SDK this module builds against (3.14.0) exposes it asStopReason.MODEL_CONTEXT_WINDOW_EXCEEDED.The class Javadoc now says the helper covers both an output-token limit and a filled context window.
Validation on Windows with Java 17:
FinishReasonsTestpasses (3 tests).agentscope-corein full: 3631 tests, 0 failures, 9 skipped. Reverting the source change failsrecognizesExplicitOutputLimitFinishReasonswithexpected: <true> but was: <false>, so the new case is actually covered.Checklist
mvn spotless:applyagentscope-coreare passingFinishReasons)