Merge pull request #2161 from ksylvan/anthropic-max-tokens-fix

fix: respect Anthropic chat option max token overrides
This commit is contained in:
Kayvan Sylvan 2026-07-12 12:19:38 -07:00 committed by GitHub
commit c7f07b6f1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 1 deletions

View file

@ -0,0 +1,11 @@
### PR [#2161](https://github.com/danielmiessler/Fabric/pull/2161) by [ksylvan](https://github.com/ksylvan): fix: respect Anthropic chat option max token overrides
- Fix: respect Anthropic chat option max token overrides
- Use configured Anthropic max tokens as default
- Apply chat option max tokens when provided
- Preserve existing behavior for missing token overrides
- Add tests for default max token selection
- Add tests for explicit max token overrides

View file

@ -233,9 +233,14 @@ func (an *Client) SendStream(
func (an *Client) buildMessageParams(msgs []anthropic.MessageParam, opts *domain.ChatOptions) (
params anthropic.MessageNewParams) {
maxTokens := an.maxTokens
if opts.MaxTokens > 0 {
maxTokens = opts.MaxTokens
}
params = anthropic.MessageNewParams{
Model: anthropic.Model(opts.Model),
MaxTokens: int64(an.maxTokens),
MaxTokens: int64(maxTokens),
Messages: msgs,
}

View file

@ -99,6 +99,43 @@ func TestBuildMessageParams_WithoutSearch(t *testing.T) {
}
}
func TestBuildMessageParams_UsesConfiguredMaxTokensByDefault(t *testing.T) {
client := NewClient()
opts := &domain.ChatOptions{
Model: "claude-3-5-sonnet-latest",
Temperature: domain.DefaultTemperature,
TopP: domain.DefaultTopP,
}
messages := []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Hello")),
}
params := client.buildMessageParams(messages, opts)
if params.MaxTokens != int64(client.maxTokens) {
t.Errorf("Expected default max_tokens %d, got %d", client.maxTokens, params.MaxTokens)
}
}
func TestBuildMessageParams_UsesChatOptionsMaxTokens(t *testing.T) {
client := NewClient()
opts := &domain.ChatOptions{
Model: "claude-3-5-sonnet-latest",
Temperature: domain.DefaultTemperature,
TopP: domain.DefaultTopP,
MaxTokens: 8192,
}
messages := []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Hello")),
}
params := client.buildMessageParams(messages, opts)
if params.MaxTokens != int64(opts.MaxTokens) {
t.Errorf("Expected max_tokens %d, got %d", opts.MaxTokens, params.MaxTokens)
}
}
func TestBuildMessageParams_WithSearch(t *testing.T) {
client := NewClient()
opts := &domain.ChatOptions{