From 9d338dd370bb2b179335770fdda5aab5bc3368fd Mon Sep 17 00:00:00 2001 From: sausheong Date: Tue, 27 Oct 2026 11:32:26 +0800 Subject: [PATCH] Anthropic: provider from SDK request options; Sonnet 5.5 reasoning off sends between_tools - NewAnthropicProviderWithOptions builds a provider from Anthropic SDK request options, for gateways NewAnthropicProvider can't express: a bearer token (option.WithAuthToken) instead of X-Api-Key, or no anthropic-version header (option.WithHeaderDel). - Sonnet 5.5 rejects thinking {type:"disabled"} with a 400; with reasoning off it now gets {type:"between_tools"}. Sonnet 5 keeps disabled. Co-Authored-By: Claude --- providers/anthropic/anthropic.go | 23 +++++++++++++ providers/anthropic/anthropic_test.go | 49 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 1475ea8..0bd731e 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -29,6 +29,16 @@ func NewAnthropicProvider(apiKey, baseURL string) *AnthropicProvider { return &AnthropicProvider{client: client} } +// NewAnthropicProviderWithOptions creates a provider from Anthropic SDK request +// options, for endpoints NewAnthropicProvider can't express: gateways that want +// a bearer token (option.WithAuthToken) instead of X-Api-Key, or that reject the +// SDK's version header (option.WithHeaderDel("anthropic-version")). The SDK's +// environment defaults (ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, +// ANTHROPIC_BASE_URL) still apply first; opts override them. +func NewAnthropicProviderWithOptions(opts ...option.RequestOption) *AnthropicProvider { + return &AnthropicProvider{client: anthropic.NewClient(opts...)} +} + // SupportsPromptCaching implements llm.PromptCachingProvider: the // Anthropic Messages API honours cache_control breakpoints regardless // of what the provider block is named in user config. @@ -160,6 +170,13 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me slog.Info("anthropic adaptive thinking", "model", model, "effort", string(anthropicEffort(req.Reasoning))) + } else if anthropicThinksBetweenTools(model) { + // Sonnet 5.5 rejects {type:"disabled"}: its "off" is + // {type:"between_tools"} (no thinking before responding; the + // short updates between tool calls come back as thinking blocks). + params.Thinking = anthropic.ThinkingConfigParamUnion{ + OfBetweenTools: &anthropic.ThinkingConfigBetweenToolsParam{}, + } } else if anthropicRejectsDisabledThinking(model) { // Opus 5.5 can't turn thinking off: {type:"disabled"} is a 400 // ("use thinking.type.adaptive and output_config.effort"). @@ -567,6 +584,12 @@ func anthropicThinksByDefault(model string) bool { return false } +// anthropicThinksBetweenTools reports models whose reasoning-off setting is +// {type:"between_tools"} rather than {type:"disabled"}. +func anthropicThinksBetweenTools(model string) bool { + return strings.Contains(model, "claude-sonnet-5-5") +} + // anthropicRejectsDisabledThinking reports whether the model returns a 400 // for thinking {type:"disabled"}. Checked before anthropicThinksByDefault, // whose "claude-opus-5" marker also matches Opus 5.5 IDs. diff --git a/providers/anthropic/anthropic_test.go b/providers/anthropic/anthropic_test.go index 3b6f555..094be56 100644 --- a/providers/anthropic/anthropic_test.go +++ b/providers/anthropic/anthropic_test.go @@ -10,6 +10,7 @@ import ( "testing" anthropic "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/option" "github.com/sausheong/harness/llm" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -522,6 +523,34 @@ data: {"type":"message_stop"} require.Equal(t, 69, done.Usage.InputTokens) // 10 uncached + 42 cache creation + 17 cache read } +// TestNewAnthropicProviderWithOptionsGatewayAuth checks the gateway shape: +// a bearer token instead of X-Api-Key, and no anthropic-version header. +func TestNewAnthropicProviderWithOptionsGatewayAuth(t *testing.T) { + t.Setenv("ANTHROPIC_API_KEY", "") + t.Setenv("ANTHROPIC_AUTH_TOKEN", "") + var got http.Header + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got = r.Header.Clone() + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")) + })) + t.Cleanup(srv.Close) + + p := NewAnthropicProviderWithOptions( + option.WithAuthToken("gw-token"), + option.WithBaseURL(srv.URL), + option.WithHeaderDel("anthropic-version"), + ) + stream, err := p.ChatStream(context.Background(), llm.ChatRequest{Model: "claude-test"}) + require.NoError(t, err) + for range stream { + } + require.NotNil(t, got, "expected a request") + require.Equal(t, "Bearer gw-token", got.Get("Authorization")) + require.Empty(t, got.Get("X-Api-Key")) + require.Empty(t, got.Get("Anthropic-Version")) +} + // TestBuildMessageParamsIsPure protects the cache-prefix invariant // behind the stream-fallback feature: when a stream dies mid-flight // the runtime retries via ChatNonStreaming with the SAME llm.ChatRequest, @@ -875,6 +904,26 @@ func TestOpus5_5_ReasoningOffSendsAdaptiveLow(t *testing.T) { } } +// TestSonnet5_5_ReasoningOffSendsBetweenTools guards against a 400 from +// Sonnet 5.5, which rejects {type:"disabled"} ("To turn thinking off on this +// model, send thinking: {type: between_tools}"). Gateway aliases match too. +func TestSonnet5_5_ReasoningOffSendsBetweenTools(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + for _, model := range []string{"claude-sonnet-5-5", "claude-sonnet-5-5-global"} { + t.Run(model, func(t *testing.T) { + got := wireJSON(t, p, llm.ChatRequest{ + Model: model, + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.NotContains(t, got, `"disabled"`) + assert.Contains(t, got, `"thinking":{"type":"between_tools"}`) + }) + } + // Sonnet 5 keeps its explicit disabled config. + got := wireJSON(t, p, llm.ChatRequest{Model: "claude-sonnet-5", Messages: []llm.Message{{Role: "user", Content: "hi"}}}) + assert.Contains(t, got, `"thinking":{"type":"disabled"}`) +} + func TestLegacyThinkingModels_KeepBudgetTokens(t *testing.T) { p := NewAnthropicProvider("test-key", "") got := wireJSON(t, p, llm.ChatRequest{