Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

BOT-2018: Support Anthropic fast mode for Metabot (#80511)

Andrei Simionescua1059fff575e
diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.ts
index e85885b9340..1d2c4142ee4 100644
--- a/frontend/src/metabase-types/api/settings.ts
+++ b/frontend/src/metabase-types/api/settings.ts
@@ -774,6 +774,7 @@ export interface EnterpriseSettings extends Settings {
"llm-openai-model"?: string;
"llm-metabot-configured?"?: boolean | null;
"llm-metabot-supports-reasoning?"?: boolean | null;
+ "llm-metabot-supports-fast-mode?"?: boolean | null;
"llm-openrouter-api-key"?: string | null;
"llm-zai-api-key"?: string | null;
"llm-mistral-api-key"?: string | null;
@@ -837,6 +838,7 @@ export interface EnterpriseSettings extends Settings {
"python-runner-test-run-timeout-seconds"?: number | null;
"llm-metabot-provider"?: string | null;
"llm-mini-model"?: string | null;
+ "llm-fast-mode"?: boolean | null;
"llm-anthropic-api-key"?: string | null;
"llm-anthropic-model": string;
"llm-proxy-configured?"?: boolean | null;
diff --git a/frontend/src/metabase/admin/ai/AIModelSettingsSection.tsx b/frontend/src/metabase/admin/ai/AIModelSettingsSection.tsx
index 54ea78bd10b..986e4676740 100644
--- a/frontend/src/metabase/admin/ai/AIModelSettingsSection.tsx
+++ b/frontend/src/metabase/admin/ai/AIModelSettingsSection.tsx
@@ -1,10 +1,14 @@
import { t } from "ttag";
import { SettingsSection } from "metabase/admin/components/SettingsSection";
+import { AdminSettingInput } from "metabase/admin/settings/components/widgets/AdminSettingInput";
import { LlmModelPicker } from "metabase/metabot";
+import { useSetting } from "metabase/settings";
import { Stack } from "metabase/ui";
export function AIModelSettingsSection({ id }: { id?: string }) {
+ const supportsFastMode = useSetting("llm-metabot-supports-fast-mode?");
+
return (
<SettingsSection
id={id}
@@ -17,6 +21,14 @@ export function AIModelSettingsSection({ id }: { id?: string }) {
label={t`Default model`}
description={t`Metabot, AI explorations and SQL generation all run on this model.`}
/>
+ {supportsFastMode && (
+ <AdminSettingInput
+ name="llm-fast-mode"
+ title={t`Fast mode`}
+ description={t`Get faster responses from the default model at a higher price per token. On Anthropic, this requires an account enrolled in the fast mode research preview, and isn't available with a Priority Tier commitment.`}
+ inputType="boolean"
+ />
+ )}
<LlmModelPicker
settingKey="llm-mini-model"
label={t`Mini model`}
diff --git a/frontend/src/metabase/admin/ai/AIModelSettingsSection.unit.spec.tsx b/frontend/src/metabase/admin/ai/AIModelSettingsSection.unit.spec.tsx
index 8f870d18985..37cb6343ec1 100644
--- a/frontend/src/metabase/admin/ai/AIModelSettingsSection.unit.spec.tsx
+++ b/frontend/src/metabase/admin/ai/AIModelSettingsSection.unit.spec.tsx
@@ -27,6 +27,10 @@ const CONNECTION_MODELS = [
name: "Anthropic",
type: "anthropic",
models: [
+ createMockLlmModel({
+ id: "claude-opus-5",
+ display_name: "Claude Opus 5",
+ }),
createMockLlmModel({
id: "claude-sonnet-4-5",
display_name: "Claude Sonnet 4.5",
@@ -50,6 +54,7 @@ type SetupOpts = {
metabotModel?: string | null;
metabotModelEnvVar?: string;
miniModel?: string | null;
+ supportsFastMode?: boolean;
};
function renderSection({
@@ -57,6 +62,7 @@ function renderSection({
metabotModel = "anthropic/claude-sonnet-4-5",
metabotModelEnvVar,
miniModel = "anthropic/claude-haiku-4-5",
+ supportsFastMode = false,
}: SetupOpts = {}) {
fetchMock.removeRoutes();
fetchMock.clearHistory();
@@ -64,6 +70,7 @@ function renderSection({
const sessionProperties = createMockSettings({
"llm-metabot-provider": metabotModel,
"llm-mini-model": miniModel,
+ "llm-metabot-supports-fast-mode?": supportsFastMode,
});
setupPropertiesEndpoints(sessionProperties);
@@ -75,6 +82,7 @@ function renderSection({
env_name: metabotModelEnvVar,
}),
createMockSettingDefinition({ key: "llm-mini-model", value: miniModel }),
+ createMockSettingDefinition({ key: "llm-fast-mode", value: false }),
]);
setupUpdateSettingEndpoint();
setupLlmModelsEndpoint(models);
@@ -201,4 +209,53 @@ describe("AIModelSettingsSection", () => {
}),
).toBe(false);
});
+
+ it("hides fast mode when the selected model doesn't support it", async () => {
+ await setup();
+
+ expect(screen.queryByText("Fast mode")).not.toBeInTheDocument();
+ });
+
+ it("offers fast mode when the selected model supports it and saves the toggle", async () => {
+ await setup({
+ metabotModel: "anthropic/claude-opus-5",
+ supportsFastMode: true,
+ });
+
+ await userEvent.click(await screen.findByText("Fast mode"));
+
+ await waitFor(() => {
+ expect(
+ fetchMock.callHistory.called("path:/api/setting/llm-fast-mode", {
+ method: "PUT",
+ body: { value: true },
+ }),
+ ).toBe(true);
+ });
+ });
+
+ it("hides fast mode after switching to a model without it", async () => {
+ await setup({
+ metabotModel: "anthropic/claude-opus-5",
+ supportsFastMode: true,
+ });
+
+ expect(await screen.findByText("Fast mode")).toBeInTheDocument();
+
+ setupPropertiesEndpoints(
+ createMockSettings({
+ "llm-metabot-provider": "azure-prod/gpt-5",
+ "llm-mini-model": "anthropic/claude-haiku-4-5",
+ "llm-metabot-supports-fast-mode?": false,
+ }),
+ );
+ const listbox = await openPicker("Default model");
+ await userEvent.click(
+ within(listbox).getByRole("option", { name: "GPT-5" }),
+ );
+
+ await waitFor(() => {
+ expect(screen.queryByText("Fast mode")).not.toBeInTheDocument();
+ });
+ });
});
diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.clj
index 6999359231b..c8613bf208f 100644
--- a/src/metabase/metabot/self.clj
+++ b/src/metabase/metabot/self.clj
@@ -505,7 +505,8 @@
:tool-choice tool-choice :ai-proxy? ai-proxy?})
(let [tracking-opts (assoc tracking-opts :model provider-and-model :ai-proxy? ai-proxy?)
streaming-opts (cond-> {:model model :input parts :tools (vals tools)
- :credentials credentials :ai-proxy? ai-proxy?}
+ :credentials credentials :ai-proxy? ai-proxy?
+ :fast? (metabot.settings/llm-fast-mode)}
system-msg (assoc :system system-msg)
(and (seq tools)
tool-choice) (assoc :tool_choice tool-choice)
diff --git a/src/metabase/metabot/self/azure.clj b/src/metabase/metabot/self/azure.clj
index 38899120cf9..9d0160bc71c 100644
--- a/src/metabase/metabot/self/azure.clj
+++ b/src/metabase/metabot/self/azure.clj
@@ -212,7 +212,7 @@
throws when they are missing. `:ai-proxy?` is not supported for Azure and throws when true."
[{:keys [model input tools credentials ai-proxy?] :as opts} :- core/LLMRequestOpts]
(let [family (model->family model)
- opts (assoc opts :model (model->deployment model) :reasoning? false)
+ opts (assoc opts :model (model->deployment model) :reasoning? false :fast? false)
{:keys [path headers req]}
(case family
:anthropic {:path "/v1/messages"
diff --git a/src/metabase/metabot/self/bedrock.clj b/src/metabase/metabot/self/bedrock.clj
index 100fcf8961b..5a7cf1717a7 100644
--- a/src/metabase/metabot/self/bedrock.clj
+++ b/src/metabase/metabot/self/bedrock.clj
@@ -250,7 +250,7 @@
`:ai-proxy?` is not supported for Bedrock and throws when true."
[{:keys [model input tools credentials ai-proxy?] :as opts
:or {model default-model}} :- core/LLMRequestOpts]
- (let [opts (assoc opts :model model :reasoning? false)
+ (let [opts (assoc opts :model model :reasoning? false :fast? false)
family (model->family model)
{:keys [path headers req]}
(case family
diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.clj
index 433c7f442e6..df3674ff31f 100644
--- a/src/metabase/metabot/self/claude.clj
+++ b/src/metabase/metabot/self/claude.clj
@@ -7,6 +7,7 @@
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.json :as json]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.o11y :refer [with-span]]))
@@ -442,19 +443,31 @@
[model]
(some? (model-thinking-config model)))
+(def ^:private fast-mode-models
+ "The models Anthropic documents fast mode for: https://code.claude.com/docs/en/fast-mode"
+ #{"claude-opus-4-8" "claude-opus-5"})
+
+(defn fast-mode-model?
+ "Whether `model` supports Anthropic fast mode."
+ [model]
+ (contains? fast-mode-models (strip-vendor-prefix model)))
+
(mu/defn claude-request-body
"Build the Anthropic Messages API request body for an LLM request.
A caller-supplied `:reasoning-config` is this dialect's `thinking` block and wins outright: an
adapter re-hosting a non-Claude model here knows its own provider's thinking shape and
restrictions, which the model-id-derived config and the suppression rules below cannot describe."
- [{:keys [model system input tools schema tool_choice temperature max-tokens reasoning? reasoning-config]
+ [{:keys [model system input tools schema tool_choice temperature max-tokens reasoning? reasoning-config fast? ai-proxy?]
:or {model "claude-haiku-4-5" reasoning? true}} :- core/LLMRequestOpts]
(let [;; forced tool choice (structured output, or "required") is incompatible
;; with thinking — suppress it there.
thinking (or reasoning-config
(when-not (or (not reasoning?) schema (= "required" (some-> tool_choice name)))
(model-thinking-config model)))
+ ;; fast mode is premium-priced, so only honor it on BYOK connections;
+ ;; proxied requests bill through Metabase Cloud.
+ fast? (and fast? (not ai-proxy?) (fast-mode-model? model))
input (cond->> input
(nil? thinking) (remove #(= :reasoning (:type %))))
messages (parts->claude-messages input)
@@ -482,17 +495,43 @@
thinking (assoc :thinking thinking)
+ fast? (assoc :speed "fast")
+
;; sampling params are rejected alongside thinking
(and temperature (not thinking) (model-supports-temperature? model))
(assoc :temperature temperature))))
+(defn- fast-mode-rejection?
+ "Whether a decoded 400 reads as Anthropic rejecting fast mode itself (account not in the
+ research preview, beta header not recognized) rather than some unrelated malformed request."
+ [res]
+ (boolean (re-find #"(?i)fast[ _-]?mode|\bspeed\b"
+ (str (get-in res [:body :error :message])))))
+
+(def ^:private fast-mode-cooldown-ms
+ "How long to stop requesting fast mode after Anthropic rejects a fast-mode request.
+ Fast and standard speed don't share prompt-cache prefixes, so flapping between them
+ rewrites the conversation cache on every flip; holding standard for a window keeps
+ the speed (and the cache) stable, and spares doomed fast attempts while the account
+ is over its fast-mode limits or not enrolled at all."
+ (* 5 60 1000))
+
+(def ^:private fast-mode-cooldown-until
+ "Epoch millis until which fast mode is skipped. Process-local, resets on restart."
+ (atom 0))
+
+(defn- fast-mode-cooling-down?
+ []
+ (< (System/currentTimeMillis) @fast-mode-cooldown-until))
+
(mu/defn claude-raw
"Perform a streaming request to Claude API.
Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and
throws when they are missing."
[{:keys [model input tools credentials ai-proxy?] :as opts
:or {model "claude-haiku-4-5"}} :- core/LLMRequestOpts]
- (let [req (claude-request-body opts)]
+ (let [opts (cond-> opts (fast-mode-cooling-down?) (assoc :fast? false))
+ req (claude-request-body opts)]
(with-span :info {:name :metabot.claude/request
:model model
:msg-count (count input)
@@ -508,8 +547,9 @@
{:method :post
:url "/v1/messages"
:as :stream
- :headers {"anthropic-version" "2023-06-01"
- "content-type" "application/json"}
+ :headers (cond-> {"anthropic-version" "2023-06-01"
+ "content-type" "application/json"}
+ (:speed req) (assoc "anthropic-beta" "fast-mode-2026-02-01"))
:body (json/encode req)})]
;; The SSE body is consumed lazily, after this `try` has exited — wrap
;; the reducible so mid-stream IO/timeout failures get the same
@@ -521,7 +561,25 @@
:request req})
(core/reducible-with-api-errors "anthropic" anthropic-error-msg)))
(catch Exception e
- (core/rethrow-api-error! "anthropic" anthropic-error-msg e))))))
+ ;; decoding the error body also closes the streamed response, so the connection is
+ ;; not leaked when the exception is swallowed by the retry below. Fast mode has its
+ ;; own rate-limit pool, so a 429 here doesn't imply standard speed is limited;
+ ;; a 400 needs its message checked to keep unrelated malformed requests failing fast.
+ (let [status (:status (ex-data e))
+ res (when (and (:speed req) (contains? #{400 429 529} status))
+ (core/decode-error-body e))
+ rejected? (and res (or (not= 400 status) (fast-mode-rejection? res)))]
+ (when rejected?
+ (reset! fast-mode-cooldown-until (+ (System/currentTimeMillis) fast-mode-cooldown-ms))
+ (log/warn "Anthropic rejected the fast-mode request; falling back to standard speed"
+ {:status status}))
+ ;; 529 means the API itself is overloaded, so no immediate retry: surface it and
+ ;; let the caller's retry loop pace the next attempt, which the armed cooldown
+ ;; keeps at standard speed.
+ (if (and rejected? (not= 529 status))
+ (claude-raw (assoc opts :fast? false))
+ (core/rethrow-api-error! "anthropic" anthropic-error-msg
+ (if res (ex-info (str (ex-message e)) res e) e)))))))))
(defn claude
"Call Claude API, return AISDK stream"
diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.clj
index a04054232fd..6e601788420 100644
--- a/src/metabase/metabot/self/core.clj
+++ b/src/metabase/metabot/self/core.clj
@@ -63,6 +63,9 @@
it is the `thinking` block, sent verbatim. When set it wins over both
the derived config and the suppression rules, and :reasoning parts
survive into the replayed input.
+ :fast? - When true, request the provider's fast mode where the model
+ supports it (Anthropic Opus fast mode); adapters without one
+ ignore it
:prompt-cache-key - prompt-cache affinity hint (the conversation id); adapters whose
provider caches opt-in per key forward it (Mistral), others ignore it"
[:map
@@ -78,6 +81,7 @@
[:ai-proxy? {:optional true} [:maybe :boolean]]
[:reasoning? {:optional true} [:maybe :boolean]]
[:reasoning-config {:optional true} [:maybe :map]]
+ [:fast? {:optional true} [:maybe :boolean]]
[:prompt-cache-key {:optional true} [:maybe :string]]])
(defn mkid
@@ -950,6 +954,13 @@
body preview into the message the caller sees."
#{401 403})
+(defn decode-error-body
+ "The response map on a provider HTTP exception's ex-data, with its body decoded for
+ inspection. Consumes and closes a streamed body, so a caller that swallows the
+ exception (e.g. to retry) does not leak the connection."
+ [e]
+ (decode-bounded-body (ex-data e)))
+
(defn rethrow-api-error!
"Rethrow a provider HTTP exception with a translated, user-facing message.
`res->message` receives the decoded response map and returns the provider-specific message.
diff --git a/src/metabase/metabot/settings.clj b/src/metabase/metabot/settings.clj
index c0e8cc7e825..69010f0065a 100644
--- a/src/metabase/metabot/settings.clj
+++ b/src/metabase/metabot/settings.clj
@@ -264,6 +264,35 @@
:getter #(llm-provider-streams-reasoning? (llm-metabot-provider))
:doc false)
+(defn- llm-provider-supports-fast-mode?
+ "Whether a model reference names a model we can serve in Anthropic fast mode.
+
+ Anthropic-only and BYOK-only: fast mode is premium-priced, and proxied connections bill through
+ Metabase Cloud rather than the instance's own API key."
+ [model-ref]
+ (let [{:keys [type model ai-proxy?]} (llm.provider/resolve-model-ref model-ref)]
+ (boolean (and (= type "anthropic")
+ (not ai-proxy?)
+ (claude/fast-mode-model? model)))))
+
+(defsetting llm-metabot-supports-fast-mode?
+ "Whether the selected Metabot model can run in fast mode. Settings-manager rather than public:
+ only the admin page reads it, and a public value would tell unauthenticated callers which
+ provider and model tier serves Metabot."
+ :type :boolean
+ :visibility :settings-manager
+ :setter :none
+ :export? false
+ :getter #(llm-provider-supports-fast-mode? (llm-metabot-provider))
+ :doc false)
+
+(defsetting llm-fast-mode
+ (deferred-tru "Run Metabot in the provider''s fast mode when the selected model supports it. Fast mode responds faster at a higher price per token; on Anthropic it requires an account enrolled in the fast-mode research preview and is not available with a Priority Tier commitment.")
+ :type :boolean
+ :default false
+ :visibility :settings-manager
+ :export? false)
+
(def ^:private metabot-llm-setting-keys
#{:metabot-enabled? :embedded-metabot-enabled? :llm-metabot-provider})
diff --git a/test/metabase/metabot/self/azure_test.clj b/test/metabase/metabot/self/azure_test.clj
index 9516558ca6e..36027bdb1d6 100644
--- a/test/metabase/metabot/self/azure_test.clj
+++ b/test/metabase/metabot/self/azure_test.clj
@@ -179,6 +179,14 @@
(is (not (contains? body :reasoning)))
(is (not (contains? body :include))))))
+(deftest fast-mode-is-disabled-test
+ (testing "a fast-mode request is stripped before the anthropic body is built"
+ (let [body (json/decode+kw
+ (:body (captured-raw-request! {:model "anthropic/claude-opus-4-8"
+ :fast? true
+ :input [{:role :user :content "hi"}]})))]
+ (is (not (contains? body :speed))))))
+
(deftest unsupported-family-throws-test
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
diff --git a/test/metabase/metabot/self/bedrock_test.clj b/test/metabase/metabot/self/bedrock_test.clj
index bb508d952d9..9a346bfda34 100644
--- a/test/metabase/metabot/self/bedrock_test.clj
+++ b/test/metabase/metabot/self/bedrock_test.clj
@@ -242,6 +242,14 @@
(is (not (contains? body :reasoning)))
(is (not (contains? body :include))))))
+(deftest fast-mode-is-disabled-test
+ (testing "a fast-mode request is stripped before the anthropic body is built"
+ (let [body (json/decode+kw
+ (:body (captured-raw-request! {:model "anthropic.claude-opus-4-8"
+ :fast? true
+ :input [{:role :user :content "hi"}]})))]
+ (is (not (contains? body :speed))))))
+
(deftest unsupported-model-throws-test
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.clj
index 8acfe6ab94e..45081aa0b4c 100644
--- a/test/metabase/metabot/self/claude_test.clj
+++ b/test/metabase/metabot/self/claude_test.clj
@@ -321,6 +321,151 @@
[{:type :reasoning :id "r1" :text "unsigned"}
{:type :tool-input :id "call-1" :function "search" :arguments {}}])))))
+(deftest ^:parallel claude-request-body-fast-mode-test
+ (let [input [{:role :user :content "hi"}]
+ speed #(:speed (claude/claude-request-body (merge {:input input} %)))]
+ (testing ":fast? requests fast mode on models that support it"
+ (is (= "fast" (speed {:model "claude-opus-5" :fast? true})))
+ (is (= "fast" (speed {:model "claude-opus-4-8" :fast? true}))))
+ (testing "no speed without :fast?"
+ (is (nil? (speed {:model "claude-opus-5"}))))
+ (testing "no speed on models without fast mode"
+ (is (nil? (speed {:model "claude-opus-4-7" :fast? true})))
+ (is (nil? (speed {:model "claude-opus-6" :fast? true})))
+ (is (nil? (speed {:model "claude-sonnet-4-6" :fast? true})))
+ (is (nil? (speed {:model "claude-haiku-4-5" :fast? true}))))
+ (testing "no speed through the AI proxy"
+ (is (nil? (speed {:model "claude-opus-5" :fast? true :ai-proxy? true}))))))
+
+(defn- close-tracking-json-body
+ "A streamed JSON error body that flips `closed?` when closed, like the real `:as :stream`
+ response body the adapter must not leak."
+ [closed? m]
+ ;; ByteArrayInputStream.close is documented as having no effect, so there is nothing to
+ ;; pass on to the parent
+ (proxy [java.io.ByteArrayInputStream] [(.getBytes (json/encode m) "UTF-8")]
+ (close []
+ (reset! closed? true))))
+
+(defn- rejection-ex
+ [closed? status message]
+ (ex-info (str "clj-http: status " status)
+ {:status status
+ :headers {"content-type" "application/json"}
+ :body (close-tracking-json-body
+ closed?
+ {:type "error"
+ :error {:type ({400 "invalid_request_error"
+ 429 "rate_limit_error"
+ 529 "overloaded_error"} status)
+ :message message}})}))
+
+(deftest claude-raw-fast-mode-fallback-test
+ (testing "a fast-mode rejection is retried once at standard speed, closing the failed body"
+ (doseq [[status message] [[400 "Unexpected value(s) `fast-mode-2026-02-01` for the `anthropic-beta` header"]
+ [429 "This request would exceed the rate limit for your organization"]]]
+ (testing (str "HTTP " status)
+ (with-redefs [claude/fast-mode-cooldown-until (atom 0)]
+ (let [requests (atom [])
+ closed? (atom false)]
+ (mt/with-dynamic-fn-redefs [self.core/sse-reducible identity
+ http/request (fn [req]
+ (swap! requests conj req)
+ (if (:speed (json/decode+kw (:body req)))
+ (throw (rejection-ex closed? status message))
+ {:body req}))]
+ (claude/claude-raw {:model "claude-opus-5"
+ :fast? true
+ :credentials byok-credentials
+ :input [{:role :user :content "hi"}]})
+ (let [[fast-req retry-req] @requests]
+ (is (= 2 (count @requests)))
+ (is (true? @closed?))
+ (is (= "fast" (:speed (json/decode+kw (:body fast-req)))))
+ (is (= "fast-mode-2026-02-01" (get-in fast-req [:headers "anthropic-beta"])))
+ (is (nil? (:speed (json/decode+kw (:body retry-req)))))
+ (is (nil? (get-in retry-req [:headers "anthropic-beta"])))))))))))
+
+(deftest claude-raw-unrelated-400-not-retried-test
+ (testing "a 400 that does not read as a fast-mode rejection surfaces instead of retrying"
+ (with-redefs [claude/fast-mode-cooldown-until (atom 0)]
+ (let [requests (atom [])
+ closed? (atom false)]
+ (mt/with-dynamic-fn-redefs [self.core/sse-reducible identity
+ http/request (fn [req]
+ (swap! requests conj req)
+ (throw (rejection-ex closed? 400 "max_tokens: Input should be greater than 0")))]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Anthropic API error \(HTTP 400\)"
+ (claude/claude-raw {:model "claude-opus-5"
+ :fast? true
+ :credentials byok-credentials
+ :input [{:role :user :content "hi"}]})))
+ (is (= 1 (count @requests)))
+ (is (true? @closed?)))))))
+
+(deftest claude-raw-fast-mode-529-test
+ (testing "a fast-mode 529 arms the cooldown but surfaces for the caller's retry loop to pace"
+ (with-redefs [claude/fast-mode-cooldown-until (atom 0)]
+ (let [requests (atom [])
+ closed? (atom false)
+ call! #(claude/claude-raw {:model "claude-opus-5"
+ :fast? true
+ :credentials byok-credentials
+ :input [{:role :user :content "hi"}]})]
+ (mt/with-dynamic-fn-redefs [self.core/sse-reducible identity
+ http/request (fn [req]
+ (swap! requests conj req)
+ (if (:speed (json/decode+kw (:body req)))
+ (throw (rejection-ex closed? 529 "Overloaded"))
+ {:body req}))]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Anthropic API is overloaded"
+ (call!)))
+ (is (true? @closed?))
+ (call!)
+ (is (= ["fast" nil]
+ (map (comp :speed json/decode+kw :body) @requests))))))))
+
+(deftest claude-raw-fast-mode-cooldown-test
+ (let [cooldown (atom 0)
+ requests (atom [])]
+ (with-redefs [claude/fast-mode-cooldown-until cooldown]
+ (mt/with-dynamic-fn-redefs [self.core/sse-reducible identity
+ http/request (fn [req]
+ (swap! requests conj req)
+ (if (:speed (json/decode+kw (:body req)))
+ (throw (rejection-ex (atom false) 429 "This request would exceed the rate limit for your organization"))
+ {:body req}))]
+ (let [call! #(claude/claude-raw {:model "claude-opus-5"
+ :fast? true
+ :credentials byok-credentials
+ :input [{:role :user :content "hi"}]})
+ speeds #(map (comp :speed json/decode+kw :body) @requests)]
+ (testing "a rejection arms the cooldown, so the next call goes straight to standard speed"
+ (call!)
+ (call!)
+ (is (= ["fast" nil nil] (speeds))))
+ (testing "fast mode is attempted again once the cooldown expires"
+ (reset! cooldown 0)
+ (call!)
+ (is (= ["fast" nil nil "fast" nil] (speeds)))))))))
+
+(deftest claude-fast-mode-beta-header-test
+ (let [headers (fn [opts]
+ (let [captured (atom nil)]
+ (mt/with-dynamic-fn-redefs [self.core/sse-reducible identity
+ http/request (fn [req]
+ (reset! captured (:headers req))
+ {:body req})]
+ (claude/claude-raw (merge {:credentials byok-credentials
+ :input [{:role :user :content "hi"}]}
+ opts)))
+ @captured))]
+ (testing "fast-mode requests carry the beta header"
+ (is (= "fast-mode-2026-02-01"
+ (get (headers {:model "claude-opus-5" :fast? true}) "anthropic-beta"))))
+ (testing "other requests don't"
+ (is (nil? (get (headers {:model "claude-opus-5"}) "anthropic-beta"))))))
+
(deftest ^:parallel claude-request-body-reasoning-disabled-test
(testing ":reasoning? false disables thinking and strips reasoning replay"
(let [body (claude/claude-request-body
diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.clj
index 18e694a2f1c..976be744dc1 100644
--- a/test/metabase/metabot/self_test.clj
+++ b/test/metabase/metabot/self_test.clj
@@ -137,6 +137,28 @@
(throw e))))
(is (= expected (:tool_choice @captured)))))))))))
+(deftest call-llm-fast-mode-test
+ (llm.tu/with-default-connections
+ (testing "the llm-fast-mode setting reaches the Anthropic wire for a fast-capable model"
+ (let [captured (atom nil)]
+ (mt/with-dynamic-fn-redefs [http/request (fn [opts]
+ (when (:body opts)
+ (reset! captured {:body (json/decode+kw (:body opts))
+ :headers (:headers opts)}))
+ (throw (ex-info "stop" {::skip true :api-error true})))]
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"]
+ (doseq [fast? [true false]]
+ (testing (str "llm-fast-mode " fast?)
+ (mt/with-temporary-setting-values [llm-fast-mode fast?]
+ (try
+ (run! identity (self/call-llm "anthropic/claude-opus-5" nil [] {} {:tag "agent"}))
+ (catch Exception e
+ (when-not (::skip (ex-data e))
+ (throw e))))
+ (is (= (when fast? "fast") (get-in @captured [:body :speed])))
+ (is (= (when fast? "fast-mode-2026-02-01")
+ (get-in @captured [:headers "anthropic-beta"]))))))))))))
+
(deftest request-timeout-settings-test
(testing "request seeds timeouts from the llm-*-timeout-ms settings, read at call time"
(let [captured (atom nil)]
diff --git a/test/metabase/metabot/settings_test.clj b/test/metabase/metabot/settings_test.clj
index e04416b19cc..2bfca0e56f9 100644
--- a/test/metabase/metabot/settings_test.clj
+++ b/test/metabase/metabot/settings_test.clj
@@ -32,8 +32,11 @@
(defn- do-with-selected-model!
[model-ref thunk]
- (mt/with-temporary-raw-setting-values [llm-metabot-provider model-ref]
- (thunk)))
+ ;; Env vars outrank raw setting values, so mask any MB_LLM_METABOT_PROVIDER the host
+ ;; carries (dev machines pin one in mise.local.toml) before selecting the model.
+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil]
+ (mt/with-temporary-raw-setting-values [llm-metabot-provider model-ref]
+ (thunk))))
(defmacro ^:private with-selected-model
[model-ref & body]
@@ -203,6 +206,27 @@
(with-selected-model model-ref
(is (= expected (metabot.settings/llm-metabot-supports-reasoning?)))))))))
+(deftest metabot-supports-fast-mode-test
+ (testing "only BYOK anthropic connections serving a fast-capable model report support"
+ (with-connections [(connection "anthropic" "anthropic")
+ (connection "bedrock" "bedrock")
+ (connection "openai" "openai")]
+ (doseq [[model-ref expected]
+ {"anthropic/claude-opus-5" true
+ "anthropic/claude-opus-4-8" true
+ "anthropic/claude-opus-4-7" false
+ "anthropic/claude-sonnet-4-6" false
+ "bedrock/anthropic.claude-opus-4-8" false
+ "openai/gpt-5.4" false}]
+ (testing model-ref
+ (with-selected-model model-ref
+ (is (= expected (metabot.settings/llm-metabot-supports-fast-mode?))))))))
+ (testing "the managed connection reports no support even for a fast-capable model"
+ (mt/with-premium-features #{:metabase-ai-managed}
+ (with-connections [(connection "metabase" "metabase")]
+ (with-selected-model "metabase/anthropic/claude-opus-5"
+ (is (false? (metabot.settings/llm-metabot-supports-fast-mode?))))))))
+
(deftest metabot-supports-reasoning-vllm-test
(testing "vLLM answers from what the connect-time probe recorded on the connection, since neither its catalog
nor its model names carry a reasoning field"