Skip to content

Commit 26f8b94

Browse files
authored
fix(model): treat Anthropic context window exhaustion as truncation (#4567)
1 parent 7b87b59 commit 26f8b94

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎agentscope-core/src/main/java/io/agentscope/core/model/FinishReasons.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@
2323
*
2424
* <p>Providers report termination with raw strings (for example {@code "stop"}, {@code "length"},
2525
* {@code "MAX_TOKENS"}), and some report no reason at all. This helper identifies the one case
26-
* callers must treat as incomplete output: the provider explicitly stopped at an output-token
27-
* limit. A missing or unrecognized reason never indicates truncation, so callers keep accepting
28-
* providers that do not report one.
26+
* callers must treat as incomplete output: the provider stopped at an output-token limit or filled
27+
* the context window. A missing or unrecognized reason never indicates truncation, so callers keep
28+
* accepting providers that do not report one.
2929
*/
3030
public final class FinishReasons {
3131

3232
private FinishReasons() {}
3333

3434
/**
35-
* Returns whether the reported termination means the output was cut at an output-token limit.
35+
* Returns whether the reported termination means the output was cut short.
3636
*
3737
* @param finishReason the last reported finish reason, or {@code null} when none was
3838
* @param responseMetadata response metadata accompanying that report, or {@code null}
3939
*/
4040
public static boolean indicatesOutputLimit(
4141
String finishReason, Map<String, Object> responseMetadata) {
4242
// OpenAI Chat Completions and DashScope report "length"; Anthropic and Gemini report
43-
// "max_tokens" or "MAX_TOKENS".
43+
// "max_tokens" or "MAX_TOKENS"; Anthropic reports "model_context_window_exceeded" when the
44+
// response fills the context window instead of reaching its own output limit.
4445
if ("length".equalsIgnoreCase(finishReason)
45-
|| "max_tokens".equalsIgnoreCase(finishReason)) {
46+
|| "max_tokens".equalsIgnoreCase(finishReason)
47+
|| "model_context_window_exceeded".equalsIgnoreCase(finishReason)) {
4648
return true;
4749
}
4850
// The OpenAI Responses API reports "incomplete" and names the cause in metadata; only the

‎agentscope-core/src/test/java/io/agentscope/core/model/FinishReasonsTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void recognizesExplicitOutputLimitFinishReasons() {
2929
assertTrue(FinishReasons.indicatesOutputLimit("length", Map.of()));
3030
assertTrue(FinishReasons.indicatesOutputLimit("MAX_TOKENS", Map.of()));
3131
assertTrue(FinishReasons.indicatesOutputLimit("max_tokens", Map.of()));
32+
assertTrue(FinishReasons.indicatesOutputLimit("model_context_window_exceeded", Map.of()));
3233
}
3334

3435
@Test

0 commit comments

Comments
 (0)