Skip to content

fix(model): treat Anthropic context window exhaustion as truncation - #4567

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
ningmao-hlyz:fix/context-window-truncation
Dec 2, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
ningmao-hlyz:fix/context-window-truncation

Conversation

@ningmao-hlyz

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT, based on main at 7b87b5969.

Description

Anthropic reports model_context_window_exceeded as stop_reason when the response fills the model's context window instead of reaching its own output limit, and their stop-reason guidance lists it next to max_tokens as a truncated response. FinishReasons.indicatesOutputLimit recognized length, max_tokens and the Responses API incomplete cause only.

That helper decides whether a compaction summary may be used. ConversationCompactor.toValidatedSummary rejects a summary that stopped at a limit, and the rejection falls through to lightweightResult, 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. MemoryConsolidator and ReasoningContext.isTruncated read the same helper.

Checked the other adapters for the same gap: DashScope reports length, Gemini reports MAX_TOKENS, OpenAI Chat Completions reports length, and the Responses API reports incomplete with the cause in metadata. Anthropic is the only adapter with this value, and the SDK this module builds against (3.14.0) exposes it as StopReason.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:

mvn -B -o -pl agentscope-core -Dtest=FinishReasonsTest test
mvn -B -o -T1C -pl agentscope-core test

FinishReasonsTest passes (3 tests). agentscope-core in full: 3631 tests, 0 failures, 9 skipped. Reverting the source change fails recognizesExplicitOutputLimitFinishReasons with expected: <true> but was: <false>, so the new case is actually covered.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests in agentscope-core are passing
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (class Javadoc of FinishReasons)
  • Code is ready for review

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
jujn merged commit 26f8b94 into agentscope-ai:main Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants