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",