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
Send Moonshot's output cap as max_completion_tokens (#83984)
Paolo Rascunà25bd0c1b3ce8
diff --git a/src/metabase/metabot/self/moonshot.clj b/src/metabase/metabot/self/moonshot.cljindex 01d23403aa1..ba4bb0e988c 100644--- a/src/metabase/metabot/self/moonshot.clj+++ b/src/metabase/metabot/self/moonshot.clj@@ -5,6 +5,7 @@https://platform.kimi.ai/docs"(:require+ [clojure.set :as set][metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core][metabase.metabot.self.openai.chat-completions :as chat-completions]@@ -83,17 +84,19 @@(adapter/model-listing supported-models (adapter/fetch-catalog provider opts))))(def ^:private forced-tool-call-token-floor- "Smallest `max_tokens` a forced tool call on a thinking-only model may be capped at.+ "Smallest `max_completion_tokens` a forced tool call on a thinking-only model may be capped at.- kimi-k3 cannot stop thinking and Chat Completions bills thinking and the tool call against one budget, so a small- caller cap (the conversation-title path sends 512) risks a `length` finish before the tool call is emitted.+ kimi-k3 cannot stop thinking. The Kimi docs say that thinking and the tool call share one `max_tokens` budget+ (https://platform.kimi.ai/docs/guide/use-thinking-models). They do not say if `max_completion_tokens` also counts+ reasoning (https://platform.kimi.ai/docs/api/chat). If it does, a small caller cap (the conversation-title path+ sends 512) risks a `length` finish before the tool call is emitted. The floor is correct in both cases.Matches vLLM's probe-proven floor."2048)-(defn- floor-max-tokens- "Raises the `max_tokens` cap to [[forced-tool-call-token-floor]]."+(defn- floor-max-completion-tokens+ "Raises the `max_completion_tokens` cap to [[forced-tool-call-token-floor]]."[body]- (update body :max_tokens max forced-tool-call-token-floor))+ (update body :max_completion_tokens max forced-tool-call-token-floor))(defn- reasoning-message"The replayed assistant message for a coalesced in-turn :reasoning part.@@ -123,9 +126,11 @@- **`reasoning_effort`** for [[thinking-only-models]] (kimi-k3, which explicitly does not support `thinking`and is the only model accepting `reasoning_effort`): \"max\" when the chat path renders reasoning, \"low\"otherwise; their in-turn reasoning is replayed (see [[reasoning-message]]) and forced tool calls get a- `max_tokens` floor (see [[forced-tool-call-token-floor]]).+ `max_completion_tokens` floor (see [[forced-tool-call-token-floor]]).- **`prompt_cache_key`.** Moonshot caching is automatic and hits without it, but a `:prompt-cache-key` — theconversation id — is forwarded when present.+ - **`max_completion_tokens`** carries the output cap instead of `max_tokens`, which Moonshot deprecates+ (https://platform.kimi.ai/docs/api/chat).A caller that names no `:max-tokens` gets [[core/chat-max-output-tokens]]."[{:keys [model prompt-cache-key reasoning? schema tool_choice max-tokens] :as opts@@ -159,15 +164,16 @@;; reasoning_content — a replay hook there would only burn prompt tokens.;; https://platform.kimi.ai/docs/guide/use-kimi-k2-thinking-model(when (and thinking? thinking-only?) {:reasoning-part->message reasoning-message}))+ (set/rename-keys {:max_tokens :max_completion_tokens})(dissoc :temperature)(cond-> thinking-only? (assoc :reasoning_effort (if thinking? "max" "low"));; Every forced tool call — schema or tool_choice "required" — must survive the- ;; thinking spend: reasoning and the tool call share one max_tokens budget ("the- ;; sum of tokens in reasoning_content and content must be <= max_tokens", and the- ;; guide recommends >= 16000 for tool calls —- ;; https://platform.kimi.ai/docs/guide/use-thinking-models). A tool call cut off- ;; at `length` fails the whole turn.- (and thinking-only? forced?) floor-max-tokens+ ;; thinking spend. A tool call cut off at `length` fails the whole turn. See+ ;; [[forced-tool-call-token-floor]] for the budget scope. The guide recommends+ ;; >= 16000 for multi-step tool calls on kimi-k2.6 and kimi-k2.7-code+ ;; (https://platform.kimi.ai/docs/guide/use-thinking-models). This is one forced+ ;; call on kimi-k3, so the floor stays at 2048.+ (and thinking-only? forced?) floor-max-completion-tokens(not thinking-only?) (assoc :thinking {:type (if thinking? "enabled" "disabled")})prompt-cache-key (assoc :prompt_cache_key prompt-cache-key)))))diff --git a/test/metabase/metabot/self/chat_max_tokens_test.clj b/test/metabase/metabot/self/chat_max_tokens_test.cljindex b2d358a4962..e3b86ea918c 100644--- a/test/metabase/metabot/self/chat_max_tokens_test.clj+++ b/test/metabase/metabot/self/chat_max_tokens_test.clj@@ -68,7 +68,8 @@:openrouter (get (openrouter/openrouter-request-body opts) :max_tokens omitted):zai (get (zai/zai-request-body opts) :max_tokens omitted)- :moonshot (get (moonshot/moonshot-request-body opts) :max_tokens omitted)+ ;; Moonshot sends the cap as max_completion_tokens+ :moonshot (get (moonshot/moonshot-request-body opts) :max_completion_tokens omitted):mistral (get (mistral/mistral-request-body opts) :max_tokens omitted):vllm (get (vllm/vllm-request-body opts) :max_tokens omitted):ollama (get (ollama/ollama-request-body opts) :max_tokens omitted)diff --git a/test/metabase/metabot/self/moonshot_test.clj b/test/metabase/metabot/self/moonshot_test.cljindex 3a2623246f5..d53d12093ba 100644--- a/test/metabase/metabot/self/moonshot_test.clj+++ b/test/metabase/metabot/self/moonshot_test.clj@@ -9,7 +9,8 @@[metabase.metabot.self.moonshot :as moonshot][metabase.metabot.test-util :as metabot.tu][metabase.premium-features.core :as premium-features]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.util.json :as json]))(set! *warn-on-reflection* true)@@ -50,13 +51,15 @@:temperature)))))(deftest ^:parallel request-body-max-tokens-test- (testing "max-tokens passes through"- ;; a capped k2.6 chat call would share this budget with thinking (reasoning_content counts- ;; toward max_tokens); unreachable today — the only :max-tokens producer also sets :schema,- ;; which disables k2.6's thinking- (is (= 128 (:max_tokens (moonshot/moonshot-request-body {:model "kimi-k2.6"- :input [{:role :user :content "hi"}]- :max-tokens 128}))))))+ (testing "max-tokens passes through as max_completion_tokens"+ ;; A capped k2.6 chat call can share this budget with thinking. The guide counts reasoning_content+ ;; toward max_tokens. It does not give the scope of max_completion_tokens. No caller reaches this+ ;; today: the only :max-tokens producer also sets :schema, and :schema turns off k2.6 thinking.+ (let [body (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :max-tokens 128})]+ (is (= 128 (:max_completion_tokens body)))+ (is (not (contains? body :max_tokens))))))(deftest ^:parallel request-body-thinking-switch-test(testing "kimi-k2.6's thinking switch follows the path"@@ -113,22 +116,51 @@:schema {:type "object" :properties {:title {:type "string"}}}})))(deftest ^:parallel request-body-forced-tool-call-floors-max-tokens-test- (testing "a forced tool call on a thinking-only model gets its max_tokens cap floored"+ (testing "a forced tool call on a thinking-only model gets its max_completion_tokens cap floored";; k3 bills thinking and the forced tool call against one budget — a small cap risks a `length`;; finish before the tool call is emitted (the conversation-title path sends 512). Both forcing;; shapes count: a schema and a plain tool_choice "required".(let [schema {:type "object" :properties {:title {:type "string"}}}]- (are [expected opts] (= expected (:max_tokens (moonshot/moonshot-request-body opts)))+ (are [expected opts] (= expected (:max_completion_tokens (moonshot/moonshot-request-body opts)))2048 {:model "kimi-k3" :input [{:role :user :content "hi"}] :schema schema :max-tokens 512}2048 {:model "kimi-k3" :input [{:role :user :content "hi"}] :max-tokens 512:tools [(metabot.tu/get-time-tool)] :tool_choice "required"}+ ;; at the floor, the cap is sent as is+ 2048 {:model "kimi-k3" :input [{:role :user :content "hi"}] :schema schema :max-tokens 2048}4096 {:model "kimi-k3" :input [{:role :user :content "hi"}] :schema schema :max-tokens 4096};; uncapped by the caller, so the default cap applies — already above the floor32000 {:model "kimi-k3" :input [{:role :user :content "hi"}] :schema schema}512 {:model "kimi-k3" :input [{:role :user :content "hi"}] :max-tokens 512}512 {:model "kimi-k2.6" :input [{:role :user :content "hi"}] :schema schema :max-tokens 512}512 {:model "kimi-k2.6" :input [{:role :user :content "hi"}] :max-tokens 512- :tools [(metabot.tu/get-time-tool)] :tool_choice "required"}))))+ :tools [(metabot.tu/get-time-tool)] :tool_choice "required"})+ (testing "the floor does not send max_tokens"+ (is (not (contains? (moonshot/moonshot-request-body {:model "kimi-k3"+ :input [{:role :user :content "hi"}]+ :schema schema+ :max-tokens 512})+ :max_tokens)))))))++(deftest moonshot-raw-sends-max-completion-tokens-test+ (testing "the request Moonshot receives carries the cap as max_completion_tokens, not the deprecated max_tokens"+ ;; https://platform.kimi.ai/docs/api/chat marks max_tokens "Deprecated, please refer to max_completion_tokens"+ (with-redefs [self.core/sse-reducible identity+ self.core/reducible-with-api-errors (fn [r _ _] r)+ debug/capture-stream (fn [r _] r)+ http/request (fn [req] {:body req})]+ (let [sent-body (fn [opts] (json/decode+kw (:body (moonshot/moonshot-raw opts))))]+ (testing "a chat call with no caller cap sends the chat cap"+ (let [body (sent-body {:input [{:role :user :content "hi"}] :credentials byok-credentials})]+ (is (= 32000 (:max_completion_tokens body)))+ (is (not (contains? body :max_tokens)))))+ (testing "a forced kimi-k3 call under the floor sends the floor"+ (let [body (sent-body {:model "kimi-k3"+ :input [{:role :user :content "hi"}]+ :schema {:type "object" :properties {:title {:type "string"}}}+ :max-tokens 512+ :credentials byok-credentials})]+ (is (= 2048 (:max_completion_tokens body)))+ (is (not (contains? body :max_tokens)))))))))(deftest ^:parallel request-body-replays-reasoning-test(let [reasoning-round [{:role :user :content "q"}