diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 6b3d278..c3a1b61 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -87,10 +87,28 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me if sys := buildAnthropicSystem(req); len(sys) > 0 { params.System = sys } - if req.Temperature > 0 { + if req.Temperature > 0 && !anthropicAdaptiveThinkingOnly(model) { params.Temperature = anthropic.Float(req.Temperature) } - if cfg, ok := p.BuildThinkingConfig(model, req.Reasoning); ok { + if anthropicAdaptiveThinkingOnly(model) { + // Fable 5 / Mythos 5 / Opus 4.7+ reject the legacy + // enabled+budget_tokens config (400 "thinking.type.enabled is + // not supported"). They take {type:"adaptive"} plus + // output_config.effort; sampling params are also removed. + // Reasoning off → omit thinking entirely (Fable 5 rejects an + // explicit {type:"disabled"} too). + if req.Reasoning != llm.ReasoningOff { + params.Thinking = anthropic.ThinkingConfigParamUnion{ + OfAdaptive: &anthropic.ThinkingConfigAdaptiveParam{}, + } + params.OutputConfig = anthropic.OutputConfigParam{ + Effort: anthropicEffort(req.Reasoning), + } + slog.Info("anthropic adaptive thinking", + "model", model, + "effort", string(anthropicEffort(req.Reasoning))) + } + } else if cfg, ok := p.BuildThinkingConfig(model, req.Reasoning); ok { required := cfg.BudgetTokens + 4096 if maxTokens < required { maxTokens = required @@ -374,6 +392,42 @@ func (p *AnthropicProvider) BuildThinkingConfig(model string, mode llm.Reasoning } } +// anthropicAdaptiveThinkingOnly reports whether the model accepts ONLY +// adaptive thinking: Claude Fable 5, Claude Mythos 5, and Opus 4.7+. +// On these the legacy {type:"enabled", budget_tokens:N} config and +// sampling params (temperature/top_p/top_k) return HTTP 400. Matched by +// substring so gateway model-group names (claude-fable-5-global) and +// Bedrock-style IDs (us.anthropic.claude-opus-4-8-v1) are covered. +func anthropicAdaptiveThinkingOnly(model string) bool { + adaptiveOnly := []string{ + "claude-fable-5", + "claude-mythos-5", + "claude-mythos-preview", + "claude-opus-4-7", + "claude-opus-4-8", + "claude-opus-4-9", // future-proof within the 4.x line + } + for _, marker := range adaptiveOnly { + if strings.Contains(model, marker) { + return true + } + } + return false +} + +// anthropicEffort maps the unified ReasoningMode onto output_config.effort +// for adaptive-thinking-only models. +func anthropicEffort(mode llm.ReasoningMode) anthropic.OutputConfigEffort { + switch mode { + case llm.ReasoningLow: + return anthropic.OutputConfigEffortLow + case llm.ReasoningMedium: + return anthropic.OutputConfigEffortMedium + default: + return anthropic.OutputConfigEffortHigh + } +} + // anthropicSupportsThinking returns true for Claude models that accept // extended thinking. Conservative — unknown IDs default to true so we // let the API decide rather than silently drop a knob the user set. diff --git a/providers/anthropic/anthropic_test.go b/providers/anthropic/anthropic_test.go index 9d84261..6088ac9 100644 --- a/providers/anthropic/anthropic_test.go +++ b/providers/anthropic/anthropic_test.go @@ -521,3 +521,109 @@ data: {"type":"message_stop"} assert.Equal(t, `{}`, string(toolDone.ToolCall.Input), "argument-less tool call must serialize as {} so MCP transport accepts it") } + +// --- Adaptive-thinking model mapping --------------------------------- +// +// Claude Fable 5, Claude Mythos 5, and Opus 4.7/4.8 reject the legacy +// thinking config: `thinking: {type:"enabled", budget_tokens:N}` returns +// HTTP 400 ("thinking.type.enabled is not supported for this model"). +// They also removed sampling params — sending `temperature` is a 400. +// For these models the provider must emit `thinking: {type:"adaptive"}` +// plus `output_config.effort`, and drop temperature. + +func wireJSON(t *testing.T, p *AnthropicProvider, req llm.ChatRequest) string { + t.Helper() + params := p.buildMessageParams(req) + b, err := json.Marshal(params) + require.NoError(t, err) + return string(b) +} + +func TestAdaptiveThinkingModels_EmitAdaptiveNotEnabled(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + models := []string{ + "claude-fable-5", + "claude-fable-5-global", // LiteLLM / gateway model-group names + "claude-mythos-5", + "claude-opus-4-7", + "claude-opus-4-8", + "us.anthropic.claude-opus-4-8-v1", // Bedrock-style ID + } + for _, model := range models { + t.Run(model, func(t *testing.T) { + got := wireJSON(t, p, llm.ChatRequest{ + Model: model, + Reasoning: llm.ReasoningHigh, + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.Contains(t, got, `"thinking":{"type":"adaptive"}`, + "adaptive-only models must send thinking.type=adaptive") + assert.NotContains(t, got, `"budget_tokens"`, + "budget_tokens is rejected with 400 on adaptive-only models") + assert.Contains(t, got, `"output_config":{"effort":"high"}`, + "reasoning mode maps to output_config.effort on adaptive-only models") + }) + } +} + +func TestAdaptiveThinkingModels_EffortMapping(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + cases := []struct { + mode llm.ReasoningMode + effort string + }{ + {llm.ReasoningLow, "low"}, + {llm.ReasoningMedium, "medium"}, + {llm.ReasoningHigh, "high"}, + } + for _, c := range cases { + t.Run(string(c.mode), func(t *testing.T) { + got := wireJSON(t, p, llm.ChatRequest{ + Model: "claude-opus-4-8", + Reasoning: c.mode, + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.Contains(t, got, `"output_config":{"effort":"`+c.effort+`"}`) + }) + } +} + +func TestAdaptiveThinkingModels_ReasoningOffOmitsThinking(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + got := wireJSON(t, p, llm.ChatRequest{ + Model: "claude-fable-5", + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.NotContains(t, got, `"thinking"`, + "reasoning off must omit the thinking param entirely — Fable 5 "+ + "rejects an explicit {type:disabled} with 400") + assert.NotContains(t, got, `"output_config"`) +} + +func TestAdaptiveThinkingModels_TemperatureDropped(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + for _, model := range []string{"claude-fable-5", "claude-opus-4-7", "claude-opus-4-8"} { + t.Run(model, func(t *testing.T) { + got := wireJSON(t, p, llm.ChatRequest{ + Model: model, + Temperature: 0.7, + Reasoning: llm.ReasoningMedium, + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.NotContains(t, got, `"temperature"`, + "sampling params are removed on these models — sending temperature is a 400") + }) + } +} + +func TestLegacyThinkingModels_KeepBudgetTokens(t *testing.T) { + p := NewAnthropicProvider("test-key", "") + got := wireJSON(t, p, llm.ChatRequest{ + Model: "claude-sonnet-4-5", + Reasoning: llm.ReasoningHigh, + Messages: []llm.Message{{Role: "user", Content: "hi"}}, + }) + assert.Contains(t, got, `"budget_tokens":16384`, + "pre-4.7 models keep the enabled+budget_tokens config") + assert.NotContains(t, got, `"output_config"`) +}