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.
This commit is contained in:
ZaneTung 2026-03-12 09:59:53 +08:00
parent ddfa437f9c
commit 94c7d879f8
2 changed files with 38 additions and 19 deletions

View file

@ -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

View file

@ -28,10 +28,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "user", Content: "Hello, world!"},
},
model: "test-model",
options: map[string]any{},
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",
@ -47,10 +49,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "assistant", Content: "4"},
},
model: "test-model",
options: map[string]any{},
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",
@ -75,10 +79,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "user", Content: "Hello"},
},
model: "test-model",
options: map[string]any{},
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{
@ -133,10 +149,12 @@ func TestBuildRequestBody(t *testing.T) {
},
},
model: "test-model",
options: map[string]any{},
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",