From 94c7d879f8e4edfff8adb0deb4087066dece23af Mon Sep 17 00:00:00 2001 From: ZaneTung Date: Thu, 12 Mar 2026 09:59:53 +0800 Subject: [PATCH] fix(providers): remove hardcoded max_tokens in anthropic-messages provider - remove hardcoded max_tokens value (4096) from buildRequestBody - read max_tokens directly from options parameter - add error handling when max_tokens is missing from options - update test cases to include max_tokens in options This fix ensures the provider respects the config default value (32768) or system fallback (8192) instead of always using the hardcoded 4096. --- pkg/providers/anthropic_messages/provider.go | 15 +++---- .../anthropic_messages/provider_test.go | 42 +++++++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/pkg/providers/anthropic_messages/provider.go b/pkg/providers/anthropic_messages/provider.go index 6ef74a5b1..ba00c9c89 100644 --- a/pkg/providers/anthropic_messages/provider.go +++ b/pkg/providers/anthropic_messages/provider.go @@ -140,15 +140,16 @@ func buildRequestBody( model string, options map[string]any, ) (map[string]any, error) { - result := map[string]any{ - "model": model, - "max_tokens": int64(4096), - "messages": []any{}, + // max_tokens is required and guaranteed by agent loop + maxTokens, ok := asInt(options["max_tokens"]) + if !ok { + return nil, fmt.Errorf("max_tokens is required in options") } - // Set max_tokens from options - if mt, ok := asInt(options["max_tokens"]); ok { - result["max_tokens"] = int64(mt) + result := map[string]any{ + "model": model, + "max_tokens": int64(maxTokens), + "messages": []any{}, } // Set temperature from options diff --git a/pkg/providers/anthropic_messages/provider_test.go b/pkg/providers/anthropic_messages/provider_test.go index 3fa062360..cea8e1e6c 100644 --- a/pkg/providers/anthropic_messages/provider_test.go +++ b/pkg/providers/anthropic_messages/provider_test.go @@ -27,11 +27,13 @@ func TestBuildRequestBody(t *testing.T) { messages: []Message{ {Role: "user", Content: "Hello, world!"}, }, - model: "test-model", - options: map[string]any{}, + model: "test-model", + options: map[string]any{ + "max_tokens": 8192, + }, want: map[string]any{ "model": "test-model", - "max_tokens": int64(4096), + "max_tokens": int64(8192), "messages": []any{ map[string]any{ "role": "user", @@ -46,11 +48,13 @@ func TestBuildRequestBody(t *testing.T) { {Role: "user", Content: "What is 2+2?"}, {Role: "assistant", Content: "4"}, }, - model: "test-model", - options: map[string]any{}, + model: "test-model", + options: map[string]any{ + "max_tokens": 8192, + }, want: map[string]any{ "model": "test-model", - "max_tokens": int64(4096), + "max_tokens": int64(8192), "messages": []any{ map[string]any{ "role": "user", @@ -74,11 +78,13 @@ func TestBuildRequestBody(t *testing.T) { {Role: "system", Content: "You are a helpful assistant."}, {Role: "user", Content: "Hello"}, }, - model: "test-model", - options: map[string]any{}, + model: "test-model", + options: map[string]any{ + "max_tokens": 8192, + }, want: map[string]any{ "model": "test-model", - "max_tokens": int64(4096), + "max_tokens": int64(8192), "system": "You are a helpful assistant.", "messages": []any{ map[string]any{ @@ -110,6 +116,16 @@ func TestBuildRequestBody(t *testing.T) { }, }, }, + { + name: "missing max_tokens returns error", + messages: []Message{ + {Role: "user", Content: "Test"}, + }, + model: "test-model", + options: map[string]any{}, + want: nil, + wantErr: true, + }, { name: "with tools", messages: []Message{ @@ -132,11 +148,13 @@ func TestBuildRequestBody(t *testing.T) { }, }, }, - model: "test-model", - options: map[string]any{}, + model: "test-model", + options: map[string]any{ + "max_tokens": 8192, + }, want: map[string]any{ "model": "test-model", - "max_tokens": int64(4096), + "max_tokens": int64(8192), "messages": []any{ map[string]any{ "role": "user",