The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[Metabot] Increase `max_tokens` for anthropic models (#79030)
Sloan Sparger0d1a8948b79e
diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.cljindex b94309c82df..07f84fe38a2 100644--- a/src/metabase/metabot/self/claude.clj+++ b/src/metabase/metabot/self/claude.clj@@ -330,19 +330,23 @@(tru "Anthropic API error (HTTP {0})" status))))(def ^:private supported-models- "Anthropic chat models offered in the Metabot model picker, as a map of model id -> display name.+ "Anthropic chat models offered in the Metabot model picker, keyed by model id.`list-models` returns the intersection of this map with the account's `/v1/models` catalog."- {"claude-fable-5" "Claude Fable 5"- "claude-opus-5" "Claude Opus 5"- "claude-opus-4-8" "Claude Opus 4.8"- "claude-opus-4-7" "Claude Opus 4.7"- "claude-opus-4-6" "Claude Opus 4.6"- "claude-opus-4-5-20251101" "Claude Opus 4.5"- "claude-opus-4-1-20250805" "Claude Opus 4.1"- "claude-sonnet-5" "Claude Sonnet 5"- "claude-sonnet-4-6" "Claude Sonnet 4.6"- "claude-sonnet-4-5-20250929" "Claude Sonnet 4.5"- "claude-haiku-4-5-20251001" "Claude Haiku 4.5"})+ {"claude-fable-5" {:display-name "Claude Fable 5" :max-tokens 128000}+ "claude-opus-5" {:display-name "Claude Opus 5" :max-tokens 128000}+ "claude-opus-4-8" {:display-name "Claude Opus 4.8" :max-tokens 128000}+ "claude-opus-4-7" {:display-name "Claude Opus 4.7" :max-tokens 128000}+ "claude-opus-4-6" {:display-name "Claude Opus 4.6" :max-tokens 128000}+ "claude-opus-4-5-20251101" {:display-name "Claude Opus 4.5" :max-tokens 64000}+ "claude-opus-4-1-20250805" {:display-name "Claude Opus 4.1" :max-tokens 32000}+ "claude-sonnet-5" {:display-name "Claude Sonnet 5" :max-tokens 128000}+ "claude-sonnet-4-6" {:display-name "Claude Sonnet 4.6" :max-tokens 128000}+ "claude-sonnet-4-5-20250929" {:display-name "Claude Sonnet 4.5" :max-tokens 64000}+ "claude-haiku-4-5-20251001" {:display-name "Claude Haiku 4.5" :max-tokens 64000}})++(def ^:private default-max-tokens+ "`max_tokens` for an unresolved model — low enough to be safe on any of them."+ 64000)(defn- supported-model?"Whether a `/v1/models` catalog entry is one of the [[supported-models]]."@@ -376,21 +380,30 @@(filter supported-model?)(sort-by :id)(mapv (fn [{:keys [id display_name]}]- {:id id :display_name (or display_name (supported-models id))})))}))+ {:id id :display_name (or display_name (get-in supported-models [id :display-name]))})))}))++(defn- strip-vendor-prefix+ "`model` without an optional vendor prefix (e.g. Bedrock's `anthropic.`)."+ [model]+ (str/replace-first (str model) #"^anthropic\." ""))++(defn- model-max-tokens+ "The `max_tokens` ceiling for `model`, or nil when it isn't one we know."+ [model]+ (get-in supported-models [(strip-vendor-prefix model) :max-tokens]))(defn- claude-model-version- "`[family major minor]` for a Claude opus/sonnet model id, or nil. Strips an- optional vendor prefix (e.g. Bedrock's `anthropic.`)."+ "`[family major minor]` for a Claude opus/sonnet model id, or nil."[model](when-let [[_ family major minor] (re-find #"^claude-(opus|sonnet)-(\d+)(?:-(\d+))?"- (str/replace-first (str model) #"^anthropic\." ""))]+ (strip-vendor-prefix model))][family (parse-long major) (or (some-> minor parse-long) 0)]))(defn- model-current-gen?"Current-generation Claude (Fable, Opus >=4.7, Sonnet >=5): no sampling params;thinking streams via `display: summarized`."[model]- (or (str/starts-with? (str/replace-first (str model) #"^anthropic\." "") "claude-fable")+ (or (str/starts-with? (strip-vendor-prefix model) "claude-fable")(when-let [[family major minor] (claude-model-version model)](case family"opus" (or (> major 4) (and (= major 4) (>= minor 7)))@@ -432,11 +445,7 @@(add-tools-cache-breakpoint all-tools)all-tools)](cond-> {:model model- ;; thinking draws from the same max_tokens budget as the answer, so with- ;; the 4096 default the model can spend it all thinking and truncate its- ;; reply — give thinking requests more headroom.- :max_tokens (cond-> (or max-tokens 4096)- thinking (max 16384))+ :max_tokens (or max-tokens (model-max-tokens model) default-max-tokens):stream true:cache_control {:type "ephemeral"}:messages messages}diff --git a/test/metabase/metabot/self/azure_test.clj b/test/metabase/metabot/self/azure_test.cljindex 4e6a154b948..4e68a30c004 100644--- a/test/metabase/metabot/self/azure_test.clj+++ b/test/metabase/metabot/self/azure_test.clj@@ -125,7 +125,9 @@:stream true:system [{:type "text" :text "be brief" :cache_control {:type "ephemeral"}}]:messages [{:role "user" :content [{:type "text" :text "hi"}]}]}- body)))))+ body)))+ (testing "a deployment name matches no model, so max_tokens falls back rather than being omitted"+ (is (= 64000 (:max_tokens body))))))(deftest openai-family-dispatches-to-responses-api-test(let [req (captured-raw-request! {:model "openai/gpt-5-deployment"diff --git a/test/metabase/metabot/self/bedrock_test.clj b/test/metabase/metabot/self/bedrock_test.cljindex be89f7359fe..3580e51180d 100644--- a/test/metabase/metabot/self/bedrock_test.clj+++ b/test/metabase/metabot/self/bedrock_test.clj@@ -197,6 +197,19 @@(testing "temperature is omitted for openai.-prefixed reasoning models"(is (not (contains? body :temperature))))))+(defn- captured-body!+ "The decoded request body `bedrock-raw` would send for `opts`, with a stock user message."+ [opts]+ (json/decode+kw (:body (captured-raw-request! (merge {:input [{:role :user :content "hi"}]} opts)))))++(deftest anthropic-model-max-tokens-test+ (testing "the `anthropic.` prefix is stripped so the model's own ceiling resolves"+ (are [opts tokens] (= tokens (:max_tokens (captured-body! opts)))+ {:model "anthropic.claude-opus-4-8"} 128000+ {:model "anthropic.claude-opus-4-8" :max-tokens 128} 128))+ (testing "openai.* models omit the field entirely"+ (is (not (contains? (captured-body! {:model "openai.gpt-5.5"}) :max_output_tokens)))))+(deftest reasoning-is-disabled-test(testing "anthropic models get no thinking config and reasoning parts are stripped"(let [body (json/decode+kwdiff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.cljindex 680a8f2e122..a5b112d6df0 100644--- a/test/metabase/metabot/self/claude_test.clj+++ b/test/metabase/metabot/self/claude_test.clj@@ -438,9 +438,6 @@(testing "older budget-token models get no thinking (off in v1)"(is (nil? (thinking {:model "claude-haiku-4-5"})))(is (nil? (thinking {:model "claude-sonnet-4-5"}))))- (testing "thinking raises the max_tokens floor to leave room for the answer"- (is (= 16384 (:max_tokens (capture-claude-request-body! {:input input :model "claude-opus-4-8"}))))- (is (= 32000 (:max_tokens (capture-claude-request-body! {:input input :model "claude-opus-4-8" :max-tokens 32000})))))(testing "forced tool choice is incompatible with thinking, so it is suppressed"(is (nil? (thinking {:model "claude-opus-4-8":schema {:type "object" :properties {:answer {:type "string"}}}})))@@ -448,6 +445,23 @@:tools [(metabot.tu/get-time-tool)]:tool_choice "required"})))))))+(deftest ^:parallel every-supported-model-has-a-ceiling-test+ (doseq [[id {:keys [display-name max-tokens]}] @#'claude/supported-models]+ (is (pos-int? max-tokens) id)+ (is (seq display-name) id)))++(deftest claude-max-tokens-test+ (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-test"]+ (let [max-tokens #(:max_tokens (capture-claude-request-body!+ (merge {:input [{:role :user :content "hi"}]} %)))]+ (are [opts tokens] (= tokens (max-tokens opts))+ {:model "claude-opus-4-8"} 128000+ {:model "claude-haiku-4-5-20251001"} 64000+ {:model "claude-opus-4-8" :max-tokens 32000} 32000+ ;; Bedrock ids reach us vendor-prefixed+ {:model "anthropic.claude-opus-4-8"} 128000+ {:model "my-deployment-3"} @#'claude/default-max-tokens))))+(deftest claude-auto-cache-breakpoint-test(mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-test"](let [input [{:role :user :content "hi"}]]