Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(agent): handle context window exhaustion as a length finish (#4589)

alexcastgonzb3fa2d9c4b9b
diff --git a/internal/agent/observe.go b/internal/agent/observe.go
index 7373104a2..c50b13f38 100644
--- a/internal/agent/observe.go
+++ b/internal/agent/observe.go
@@ -358,7 +358,7 @@ func isNaturalStopFinishReason(reason string) bool {
// validation (missing path, unexpected end of JSON, etc.).
func isLengthFinishReason(reason string) bool {
switch strings.ToLower(strings.TrimSpace(reason)) {
- case "length", "max_tokens", "max_output_tokens":
+ case "length", "max_tokens", "max_output_tokens", "model_context_window_exceeded":
return true
default:
return false
diff --git a/internal/agent/observe_test.go b/internal/agent/observe_test.go
index c19382832..21332606f 100644
--- a/internal/agent/observe_test.go
+++ b/internal/agent/observe_test.go
@@ -505,6 +505,7 @@ func TestIsLengthFinishReason(t *testing.T) {
assert.True(t, isLengthFinishReason("length"))
assert.True(t, isLengthFinishReason("max_tokens"))
assert.True(t, isLengthFinishReason("MAX_OUTPUT_TOKENS"))
+ assert.True(t, isLengthFinishReason("model_context_window_exceeded"))
assert.False(t, isLengthFinishReason("stop"))
assert.False(t, isLengthFinishReason("tool_calls"))
}
diff --git a/internal/models/api/anthropicmessages/anthropicmessages_test.go b/internal/models/api/anthropicmessages/anthropicmessages_test.go
index 94582e058..994d419db 100644
--- a/internal/models/api/anthropicmessages/anthropicmessages_test.go
+++ b/internal/models/api/anthropicmessages/anthropicmessages_test.go
@@ -661,3 +661,28 @@ func TestRedactedThinkingSurvivesAnUnsignedNeighbour(t *testing.T) {
require.Equal(t, []string{"redacted_thinking", "tool_use"}, blockTypes(blocks))
assert.Equal(t, "opaque-1", blocks[0].(map[string]any)["data"])
}
+
+func TestMapStopReason(t *testing.T) {
+ tests := []struct {
+ name string
+ reason string
+ toolCalls int
+ incomplete bool
+ want string
+ }{
+ {"max tokens is length", "max_tokens", 0, false, "length"},
+ {"context window exceeded is length", "model_context_window_exceeded", 0, false, "length"},
+ {"context window exceeded with tool calls is length", "model_context_window_exceeded", 2, false, "length"},
+ {"tool use", "tool_use", 1, false, "tool_calls"},
+ {"end turn with tool calls", "end_turn", 1, false, "tool_calls"},
+ {"end turn", "end_turn", 0, false, "end_turn"},
+ {"refusal", "refusal", 0, false, "content_filter"},
+ {"incomplete stream", "max_tokens", 0, true, types.FinishReasonIncomplete},
+ {"empty reason", "", 0, false, types.FinishReasonIncomplete},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.want, mapStopReason(tt.reason, tt.toolCalls, tt.incomplete))
+ })
+ }
+}
diff --git a/internal/models/api/anthropicmessages/request.go b/internal/models/api/anthropicmessages/request.go
index 65ac9e632..047d32da9 100644
--- a/internal/models/api/anthropicmessages/request.go
+++ b/internal/models/api/anthropicmessages/request.go
@@ -527,7 +527,7 @@ func mapStopReason(reason string, toolCalls int, incomplete bool) string {
switch {
case incomplete, reason == "":
return types.FinishReasonIncomplete
- case reason == "max_tokens":
+ case reason == "max_tokens", reason == "model_context_window_exceeded":
return "length"
case reason == "tool_use" || (toolCalls > 0 && reason == "end_turn"):
return "tool_calls"