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
Count user messages, not LLM calls, in the AI message limit (#85342)
lbrdnkcfa42cbaf498
diff --git a/enterprise/backend/src/metabase_enterprise/metabot/db.clj b/enterprise/backend/src/metabase_enterprise/metabot/db.cljindex 4cd872780ec..514e0cdf9a8 100644--- a/enterprise/backend/src/metabase_enterprise/metabot/db.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot/db.clj@@ -152,13 +152,22 @@:where (usage-window-expr period-start user-id tenant-id)}))(mu/defn usage-message-count- "The `:cnt` row of messages logged since `period-start`, narrowed by the optional `user-id` and `tenant-id`."+ "The `:cnt` row of messages sent since `period-start`, narrowed by the optional `user-id` and `tenant-id`.++ A message is one agent loop. The LLM calls of a loop log one `ai_usage_log` row each, under the loop's+ `request_id`, so the count is the number of distinct ids among the rows from `sources`. The loop that asks,+ `request-id`, is left out, so a turn's earlier calls do not count against its later ones; nil leaves every loop+ in. Rows without a `request_id` do not count."[period-start :- ms/TemporalInstant+ sources :- [:set :string]+ request-id :- [:maybe :string]user-id :- [:maybe ::lib.schema.id/user]tenant-id :- [:maybe ms/PositiveInt]]- (t2/query-one {:select [[[:count :*] :cnt]]+ (t2/query-one {:select [[[:count [:distinct :request_id]] :cnt]]:from [:ai_usage_log]- :where (usage-window-expr period-start user-id tenant-id)}))+ :where (cond-> (usage-window-expr period-start user-id tenant-id)+ (seq sources) (conj [:in :source sources])+ request-id (conj [:not= :request_id request-id]))}))(mu/defn delete-usage-logs-created-before!"Delete the AiUsageLogs created before `cutoff`, returning the number deleted."diff --git a/enterprise/backend/src/metabase_enterprise/metabot/usage.clj b/enterprise/backend/src/metabase_enterprise/metabot/usage.cljindex 08c83a3e552..eb48211643a 100644--- a/enterprise/backend/src/metabase_enterprise/metabot/usage.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot/usage.clj@@ -35,6 +35,12 @@"unknown""user-intent-classification"})+(def ^:private message-sources+ "The sources whose agent loops count as messages under a message limit: a chat turn, a Slack turn and a document+ generation, each a prompt the user sent. Every other source runs through the limit check but never adds to the+ count."+ #{"metabot_agent" "slackbot" "document_generate_content"})+(def ^:private known-profile-ids#{"internal""embedding_next"@@ -103,8 +109,9 @@(defn- usage-query"Query ai_usage_log for current-period usage, narrowed by the optional `user-id` and `tenant-id`.- Returns a token sum or row count depending on `metabot-limit-unit`."- [& {:keys [user-id tenant-id]}]+ Returns a token sum or a message count depending on `metabot-limit-unit`. A message count leaves out the agent+ loop that asks, `request-id`, see [[metabot.db/usage-message-count]]."+ [& {:keys [user-id tenant-id request-id]}](let [limit-type (metabot.settings/metabot-limit-unit)start (period-start)](case limit-type@@ -114,31 +121,31 @@1000000):messages- (:cnt (metabot.db/usage-message-count start user-id tenant-id)))))+ (:cnt (metabot.db/usage-message-count start message-sources request-id user-id tenant-id)))))(defn- check-instance-limit"Check the instance-wide limit. Returns an error message string if exceeded, nil otherwise."- []+ [request-id](when-let [max-usage (:max_usage (instance-limit/instance-limit nil))]- (let [usage (usage-query)]+ (let [usage (usage-query :request-id request-id)](when (>= usage max-usage)(metabot.settings/metabot-quota-reached-message)))))(defn- check-tenant-limit"Check the tenant-level limit. Returns an error message string if exceeded, nil otherwise."- [tenant-id]+ [tenant-id request-id](when tenant-id(when-let [max-usage (:max_usage (instance-limit/instance-limit tenant-id))]- (let [usage (usage-query :tenant-id tenant-id)]+ (let [usage (usage-query :tenant-id tenant-id :request-id request-id)](when (>= usage max-usage)(metabot.settings/metabot-quota-reached-message))))))(defn- check-user-limit"Check the user-level limit (max across all their groups). Returns an error message if exceeded, nil otherwise."- [user-id]+ [user-id request-id](when user-id(when-let [max-usage (group-limit/limit-for-user user-id)]- (let [usage (usage-query :user-id user-id)]+ (let [usage (usage-query :user-id user-id :request-id request-id)](when (>= usage max-usage)(metabot.settings/metabot-quota-reached-message))))))@@ -148,7 +155,7 @@(def ^:private check-instance-limit*(memoize/ttl- (fn [_cache-key] (check-instance-limit))+ check-instance-limit:ttl/threshold cache-ttl-ms))(def ^:private check-tenant-limit*@@ -178,11 +185,16 @@"Check all usage limits for the current user. Returns nil if all limits are within bounds,or a user-friendly error message string if any limit is exceeded.- Results are cached for `cache-ttl-ms`."+ `request-id` is the id of the agent loop the call belongs to. A message limit does not count that loop's earlier+ calls, so a turn that fits when it starts runs to its end. Without it, every loop counts.++ Results are cached for `cache-ttl-ms`, per `request-id`.":feature :none- []- (let [user-id api/*current-user-id*- tenant-id (some-> api/*current-user* deref :tenant_id)]- (or (check-instance-limit* :instance)- (check-tenant-limit* tenant-id)- (check-user-limit* user-id))))+ ([]+ (check-usage-limits! nil))+ ([request-id]+ (let [user-id api/*current-user-id*+ tenant-id (some-> api/*current-user* deref :tenant_id)]+ (or (check-instance-limit* request-id)+ (check-tenant-limit* tenant-id request-id)+ (check-user-limit* user-id request-id)))))diff --git a/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/api_test.cljindex 91698367153..6b6ac6bbeca 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj@@ -5,13 +5,18 @@[metabase-enterprise.metabot.settings :as ee.metabot.settings][metabase-enterprise.metabot.usage :as ee.metabot.usage][metabase.config.core :as config]+ [metabase.llm.settings :as llm.settings]+ [metabase.llm.test-util :as llm.tu]+ [metabase.metabot.conversation-title :as conversation-title][metabase.metabot.scope :as scope][metabase.metabot.self.openrouter :as openrouter][metabase.metabot.settings :as metabot.settings][metabase.metabot.test-util :as mut]+ [metabase.metabot.tools.search :as metabot-search][metabase.premium-features.core :as premium-features][metabase.test :as mt]- [metabase.util.json :as json]))+ [metabase.util.json :as json]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -58,6 +63,77 @@(finally(ee.metabot.usage/clear-limit-cache!)))))))))+(defn- sse-events+ "The decoded events of the SSE `response` of the agent-streaming endpoint."+ [response]+ (->> (str/split-lines response)+ (filter #(str/starts-with? % "data: "))+ (remove #(= "data: [DONE]" %))+ (map #(json/decode+kw (subs % 6)))))++(deftest agent-streaming-message-limit-counts-turns-test+ (testing "a message limit counts turns: a turn that calls a tool takes one message, not one per LLM call"+ (mt/with-premium-features #{:ai-controls}+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider+ metabot-limit-unit "messages"]+ (binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions]+ (with-redefs [config/is-dev? true]+ (let [conversation-ids (repeatedly 3 #(str (random-uuid)))+ llm-calls (atom 0)+ ;; the first call of a turn asks for a search, the call after the tool result answers+ mock-llm (fn [{:keys [input]}]+ (swap! llm-calls inc)+ (if (= :tool-output (:type (last input)))+ (mut/mock-llm-response+ [{:type :start :id "msg-2"}+ {:type :text :text "Found it."}+ {:type :usage :id "msg-2" :model "test-model"+ :usage {:promptTokens 20 :completionTokens 5}}])+ (mut/mock-llm-response+ [{:type :start :id "msg-1"}+ {:type :tool-input :id "call-1" :function "search"+ :arguments {:keyword_queries ["orders"]}}+ {:type :usage :id "msg-1" :model "test-model"+ :usage {:promptTokens 10 :completionTokens 5}}])))+ send-message (fn [conversation-id]+ (sse-events+ (mt/user-http-request :rasta :post 202 "metabot/agent-streaming"+ {:message "How many orders?"+ :context {}+ :conversation_id conversation-id+ :state {}})))+ error-text (fn [events]+ (:errorText (first (filter #(= "error" (:type %)) events))))]+ (try+ (mt/with-temp [:model/MetabotInstanceLimit _ {:tenant_id nil :max_usage 2}]+ (ee.metabot.usage/clear-limit-cache!)+ (mt/with-dynamic-fn-redefs [openrouter/openrouter mock-llm+ metabot-search/search (constantly [])+ conversation-title/ensure-title! (constantly {:status :ready+ :title "Orders"})]+ (mt/with-model-cleanup [:model/MetabotMessage+ [:model/MetabotConversation :created_at]]+ (testing "the first two turns stream their answer, two LLM calls each"+ (doseq [conversation-id (take 2 conversation-ids)]+ (ee.metabot.usage/clear-limit-cache!)+ (let [events (send-message conversation-id)]+ (is (nil? (error-text events)))+ (is (= "Found it." (apply str (map :delta (filter #(= "text-delta" (:type %)) events)))))))+ (is (= 4 @llm-calls)))+ (testing "the third turn is blocked with the quota message before any LLM call"+ (ee.metabot.usage/clear-limit-cache!)+ (let [events (send-message (last conversation-ids))]+ (is (= (ee.metabot.settings/metabot-quota-reached-message) (error-text events)))+ (is (= 4 @llm-calls))))+ (testing "the log holds more LLM calls than messages"+ (let [rows (t2/select :model/AiUsageLog :conversation_id [:in conversation-ids])]+ (is (= 4 (count rows)))+ (is (= 2 (count (distinct (map :request_id rows))))))))))+ (finally+ (t2/delete! :model/AiUsageLog :conversation_id [:in conversation-ids])+ (ee.metabot.usage/clear-limit-cache!))))))))))+(deftest usage-get-returns-token-status-usage-test(mt/with-premium-features #{:metabot-v3}(mt/with-dynamic-fn-redefs [premium-features/token-status (constantly {:meters {:anthropic:claude-sonnet-4-6:tokens {:meter-value 12345diff --git a/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/usage_test.cljindex 2f59a0a25e6..707f8f24b02 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.metabot.usage-test(:require[clojure.java.io :as io]+ [clojure.set :as set][clojure.string :as str][clojure.test :refer [deftest is testing]][metabase-enterprise.metabot.usage :as ee.usage]@@ -415,36 +416,92 @@;;; ------------------------------------------ Messages limit type -------------------------------------------(deftest messages-limit-type-counts-rows-test- (mt/with-premium-features #{:ai-controls}- (ee.usage/clear-limit-cache!)- (mt/with-temporary-setting-values [metabot-limit-unit "messages"- metabot-quota-reached-message "test limit reached"]- (testing "Instance limit with :messages type counts rows, not tokens"- (mt/with-temp [:model/MetabotInstanceLimit _ {:tenant_id nil :max_usage 2}]- (let [user-id (mt/user->id :rasta)]- ;; Insert 3 rows each with 1 token — row count (3) exceeds limit (2)- (dotimes [_ 3]- (insert-usage! user-id 1))- (try- (mt/with-test-user :rasta- (is (= "test limit reached" (usage/check-usage-limits!))))- (finally- (cleanup-test-usage! user-id)))))))))+;;; A message limit counts agent loops, not LLM calls: the calls of one loop share a `request_id`, and only the+;;; sources of a chat or Slack turn count. See `usage-message-count` in `metabase-enterprise.metabot.db`.++(def ^:private message-test-model "test/messages")++(defn- insert-llm-call!+ "Insert the ai_usage_log row of one LLM call from `source` in the agent loop `request-id`, for `user-id`."+ [user-id source request-id]+ (t2/insert! :model/AiUsageLog {:source source+ :model message-test-model+ :request_id request-id+ :prompt_tokens 0+ :completion_tokens 0+ :total_tokens 1+ :user_id user-id}))++(defn- cleanup-llm-calls! [user-id]+ (t2/delete! :model/AiUsageLog :user_id user-id :model message-test-model))++(defn- check-message-limit!+ "The verdict of `check-usage-limits!` for rasta's call in the loop `request-id`, under an instance message limit of+ `max-usage` with the quota message \"test limit reached\". The cache is cleared first, so the verdict is fresh."+ [max-usage request-id]+ (ee.usage/clear-limit-cache!)+ (mt/with-temporary-setting-values [metabot-limit-unit "messages"+ metabot-quota-reached-message "test limit reached"]+ (mt/with-temp [:model/MetabotInstanceLimit _ {:tenant_id nil :max_usage max-usage}]+ (mt/with-test-user :rasta+ (usage/check-usage-limits! request-id)))))-(deftest messages-under-limit-returns-nil-test+(deftest message-sources-are-known-test+ (is (set/subset? @#'ee.usage/message-sources @#'ee.usage/known-sources)))++(deftest messages-limit-counts-agent-loops-not-llm-calls-test(mt/with-premium-features #{:ai-controls}- (ee.usage/clear-limit-cache!)- (mt/with-temporary-setting-values [metabot-limit-unit "messages"]- (testing "Instance limit with :messages type: under limit returns nil"- (mt/with-temp [:model/MetabotInstanceLimit _ {:tenant_id nil :max_usage 100}]- (let [user-id (mt/user->id :rasta)]- (insert-usage! user-id 1)- (try+ (let [user-id (mt/user->id :rasta)]+ (try+ (testing "three LLM calls of one chat loop count as one message"+ (dotimes [_ 3]+ (insert-llm-call! user-id "metabot_agent" "loop-1"))+ (is (nil? (check-message-limit! 2 nil))))+ (testing "a Slack loop counts as a message too, and the limit is reached at the count"+ (insert-llm-call! user-id "slackbot" "loop-2")+ (is (= "test limit reached" (check-message-limit! 2 nil))))+ (testing "a document generation counts as a message"+ (insert-llm-call! user-id "document_generate_content" "document-1")+ (is (= "test limit reached" (check-message-limit! 3 nil))))+ (finally+ (cleanup-llm-calls! user-id))))))++(deftest messages-limit-ignores-other-sources-test+ (mt/with-premium-features #{:ai-controls}+ (let [user-id (mt/user->id :rasta)]+ (try+ (testing "a title, an example-question and an interestingness loop add nothing to the count"+ (insert-llm-call! user-id "conversation_title" "title-1")+ (insert-llm-call! user-id "example_question_generation_batch" "examples-1")+ (insert-llm-call! user-id "contextual_interestingness" "interest-1")+ (is (nil? (check-message-limit! 1 nil))))+ (testing "a row without a request_id adds nothing either"+ (insert-llm-call! user-id "metabot_agent" nil)+ (is (nil? (check-message-limit! 1 nil))))+ (finally+ (cleanup-llm-calls! user-id))))))++(deftest messages-limit-leaves-the-calling-loop-out-test+ (mt/with-premium-features #{:ai-controls}+ (let [user-id (mt/user->id :rasta)]+ (try+ (insert-llm-call! user-id "metabot_agent" "loop-1")+ (insert-llm-call! user-id "metabot_agent" "loop-2")+ (testing "at the limit, a later call of a counted loop still runs"+ (is (nil? (check-message-limit! 2 "loop-2"))))+ (testing "a call outside any loop, and the first call of a new loop, are blocked"+ (is (= "test limit reached" (check-message-limit! 2 nil)))+ (is (= "test limit reached" (check-message-limit! 2 "loop-3"))))+ (testing "the verdict of one loop is not served to another: loop-2 fits, then loop-3 is blocked, uncached"+ (ee.usage/clear-limit-cache!)+ (mt/with-temporary-setting-values [metabot-limit-unit "messages"+ metabot-quota-reached-message "test limit reached"]+ (mt/with-temp [:model/MetabotInstanceLimit _ {:tenant_id nil :max_usage 2}](mt/with-test-user :rasta- (is (nil? (usage/check-usage-limits!))))- (finally- (cleanup-test-usage! user-id)))))))))+ (is (nil? (usage/check-usage-limits! "loop-2")))+ (is (= "test limit reached" (usage/check-usage-limits! "loop-3")))))))+ (finally+ (cleanup-llm-calls! user-id))))));;; ------------------------------------------ Custom quota message ------------------------------------------diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex 7a70551326d..dab2b71f0a7 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -528,6 +528,15 @@(log/warnf "%s invoked without :required-permission (source=%s tag=%s) — every LLM call should declare which metabot permission gates it."fn-name (pr-str (:source opts)) (pr-str (:tag opts)))))+(defn- warn-when-missing-request-id+ "Every LLM call should carry the `:request-id` of its agent loop: a message limit leaves that loop out of its count,+ so a call without one counts its own loop's earlier calls and can be blocked mid-turn. Logs a warn pointing at the+ source/tag when the caller forgets. Shared by [[call-llm]] and [[call-llm-structured-with-trace]]."+ [fn-name opts]+ (when-not (:request-id opts)+ (log/warnf "%s invoked without :request-id (source=%s tag=%s) — every LLM call should carry the id of its agent loop."+ fn-name (pr-str (:source opts)) (pr-str (:tag opts)))))+(defn llm-call-unavailable-reason"Single pre-flight gate for callers that want to *skip* an LLM call cleanly instead ofattempting one and catching the failure it would throw. Bundles every check that decides@@ -591,7 +600,8 @@(call-llm provider-and-model system-msg parts tools tracking-opts nil))([provider-and-model system-msg parts tools tracking-opts {:keys [tool-choice]}](warn-when-missing-required-permission "call-llm" tracking-opts)- (or (when-let [limit-msg (usage/check-usage-limits!)]+ (warn-when-missing-request-id "call-llm" tracking-opts)+ (or (when-let [limit-msg (usage/check-usage-limits! (:request-id tracking-opts))](error-reducible limit-msg "ai_usage_limit_reached"))(when-let [missing (missing-required-permission (:required-permission tracking-opts))](error-reducible (format "Permission denied: %s required" missing) "permission_denied"))@@ -714,7 +724,8 @@provider system prompt, keeping untrusted content in the user channel."[provider-and-model messages json-schema temperature max-tokens opts](warn-when-missing-required-permission "call-llm-structured-with-trace" opts)- (when-let [limit-msg (usage/check-usage-limits!)]+ (warn-when-missing-request-id "call-llm-structured-with-trace" opts)+ (when-let [limit-msg (usage/check-usage-limits! (:request-id opts))](throw (ex-info limit-msg{:type :metabot/usage-limit-reached:error-code "ai_usage_limit_reached"diff --git a/src/metabase/metabot/usage.clj b/src/metabase/metabot/usage.cljindex 6bcc7e4815f..11d3190747f 100644--- a/src/metabase/metabot/usage.clj+++ b/src/metabase/metabot/usage.clj@@ -48,12 +48,13 @@(defenterprise check-usage-limits!"Check all usage limits for the current user. Returns nil if all limits are within bounds,- or a user-friendly error message string if any limit is exceeded.+ or a user-friendly error message string if any limit is exceeded. `request-id` is the id of the agent loop the+ call belongs to; a message limit does not count that loop's earlier calls.In OSS, always returns nil (no limits enforced)."metabase-enterprise.metabot.usage- []- nil)+ ([] nil)+ ([_request-id] nil))(defn- meter-value[meters meter-key]diff --git a/src/metabase/testing_api/api.clj b/src/metabase/testing_api/api.cljindex 9697de1335e..4124e7b8862 100644--- a/src/metabase/testing_api/api.clj+++ b/src/metabase/testing_api/api.clj@@ -364,7 +364,11 @@;;;; Metabot AI usage seeding-(def ^:private e2e-usage-source "e2e-test")+(defn- seed-ai-usage-conversation-id+ "The `conversation_id` of the `ai_usage_log` rows the `seed-ai-usage` endpoint inserts for `user-id`. Fixed per+ user, so the DELETE finds them."+ [user-id]+ (str (java.util.UUID/nameUUIDFromBytes (.getBytes (str "e2e-seed-ai-usage-" user-id) "UTF-8"))))(def ^:private e2e-usage-auditing-group-name "E2E Usage Auditing")@@ -577,21 +581,25 @@(api.macros/defendpoint :post "/metabot/seed-ai-usage":- [:map [:inserted :int]]- "Insert `count` rows into `ai_usage_log` for the given `user_id`, then clear the metabot limit- cache so limit checks re-evaluate immediately. Intended only for E2E tests."+ "Insert `count` messages of AI usage for the given `user_id`: one `ai_usage_log` row each, from the chat source and+ under its own `request_id`, so a message limit counts each row as one message. Then clear the metabot limit cache+ so limit checks re-evaluate immediately. Intended only for E2E tests."[_route-params_query-params{:keys [user_id count]} :- [:map {:closed true}[:user_id ms/PositiveInt][:count ms/PositiveInt]]]- (dotimes [_ count]- (testing-api.db/insert-ai-usage-log!- {:source e2e-usage-source- :model "test/model"- :prompt_tokens 0- :completion_tokens 0- :total_tokens 0- :user_id user_id}))+ (let [conversation-id (seed-ai-usage-conversation-id user_id)]+ (dotimes [_ count]+ (testing-api.db/insert-ai-usage-log!+ {:source "metabot_agent"+ :model "test/model"+ :conversation_id conversation-id+ :request_id (str (random-uuid))+ :prompt_tokens 0+ :completion_tokens 0+ :total_tokens 0+ :user_id user_id})))(clear-metabot-limit-cache!){:inserted count})@@ -603,7 +611,8 @@_query-params{:keys [user_id]} :- [:map {:closed true}[:user_id ms/PositiveInt]]]- (let [deleted (testing-api.db/delete-ai-usage-logs-for-user-and-source! user_id e2e-usage-source)]+ (let [deleted (testing-api.db/delete-ai-usage-logs-for-user-and-conversation!+ user_id (seed-ai-usage-conversation-id user_id))](clear-metabot-limit-cache!){:deleted deleted}))diff --git a/src/metabase/testing_api/db.clj b/src/metabase/testing_api/db.cljindex a635f26c53b..0fb77ffefd9 100644--- a/src/metabase/testing_api/db.clj+++ b/src/metabase/testing_api/db.clj@@ -77,8 +77,8 @@(t2/delete! :model/MetabotConversation {:where [:in :id conversation-ids]}))(mu/defn insert-metabot-conversation!- "Insert the MetabotConversation `row`."- [row :- ::metabot.schema/metabot-conversation.update]+ "Insert the MetabotConversation `row`. A conversation carries its own id: the app DB does not mint one."+ [row :- ::metabot.schema/metabot-conversation](t2/insert! :model/MetabotConversation row))(mu/defn insert-metabot-message!@@ -97,8 +97,9 @@tenant-id :- ms/PositiveInt](t2/update! :model/User user-id {:tenant_id tenant-id}))-(mu/defn delete-ai-usage-logs-for-user-and-source!- "Delete the AiUsageLog rows of the User with `user-id` from `source`, returning the number deleted."- [user-id :- ::lib.schema.id/user- source :- :string]- (t2/delete! :model/AiUsageLog :user_id user-id :source source))+(mu/defn delete-ai-usage-logs-for-user-and-conversation!+ "Delete the AiUsageLog rows of the User with `user-id` in the conversation `conversation-id`, returning the number+ deleted."+ [user-id :- ::lib.schema.id/user+ conversation-id :- :string]+ (t2/delete! :model/AiUsageLog :user_id user-id :conversation_id conversation-id))diff --git a/test/metabase/explorations/runner_test.clj b/test/metabase/explorations/runner_test.cljindex 4f1799b29ee..60995f5d8f3 100644--- a/test/metabase/explorations/runner_test.clj+++ b/test/metabase/explorations/runner_test.clj@@ -297,7 +297,7 @@(lib/->legacy-MBQL (let [mp (mt/metadata-provider)] (-> (lib/query mp (lib.metadata/table mp (mt/id :venues))) (lib/aggregate (lib/count)) (lib/breakout (lib.metadata/field mp (mt/id :venues :category_id)))))))llm-calls (atom 0)](mt/with-dynamic-fn-redefs [metabase.metabot.usage/check-usage-limits!- (fn [] "you've hit your AI usage limit")+ (fn [& _] "you've hit your AI usage limit")metabase.metabot.self.claude/claude(fn [& _] (swap! llm-calls inc) [])](drain-until-terminal! (:id row))diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.cljindex d143f5e70af..87a36188d12 100644--- a/test/metabase/metabot/self_test.clj+++ b/test/metabase/metabot/self_test.clj@@ -2210,7 +2210,7 @@(let [adapter-calls (atom 0)](mt/with-dynamic-fn-redefs[usage/check-usage-limits!- (fn [] "you've used all of your AI tokens")+ (fn [& _] "you've used all of your AI tokens")self.claude/claude(fn [& _] (swap! adapter-calls inc) [])](let [ex (try-structured-call {})]@@ -2296,7 +2296,7 @@(let [adapter-calls (atom 0)](mt/with-dynamic-fn-redefs[usage/check-usage-limits!- (fn [] "you've used all of your AI tokens")+ (fn [& _] "you've used all of your AI tokens")self.claude/claude(fn [& _] (swap! adapter-calls inc) [])](let [parts (into [] (self/call-llm "anthropic/claude-haiku-4-5"diff --git a/test/metabase/testing_api/api_test.clj b/test/metabase/testing_api/api_test.cljindex 051b8b90821..1cc8ef56a9f 100644--- a/test/metabase/testing_api/api_test.clj+++ b/test/metabase/testing_api/api_test.clj@@ -13,7 +13,8 @@[metabase.test :as mt][metabase.testing-api.api :as testing][metabase.util :as u]- [metabase.util.date-2 :as u.date]))+ [metabase.util.date-2 :as u.date]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -154,3 +155,20 @@:model-reasoning "true"}}]](is (nil? (mt/user-http-request :rasta :put 204 "testing/llm-providers" {:value vllm})))(is (= vllm (vec (llm.settings/llm-providers))))))))))++(deftest seed-ai-usage-test+ (testing "POST seeds one chat LLM call per count, each in its own agent loop, and DELETE removes them"+ (let [user-id (mt/user->id :rasta)+ seed-rows #(t2/select :model/AiUsageLog :user_id user-id :model "test/model" :source "metabot_agent")]+ (try+ (is (= {:inserted 3}+ (mt/user-http-request :crowberto :post 200 "testing/metabot/seed-ai-usage"+ {:user_id user-id :count 3})))+ (let [rows (seed-rows)]+ (is (= 3 (count rows)))+ (is (= 3 (count (distinct (map :request_id rows)))) "each row is its own agent loop")+ (is (every? :request_id rows)))+ (finally+ (is (= {:deleted 3}+ (mt/user-http-request :crowberto :delete 200 "testing/metabot/seed-ai-usage" {:user_id user-id})))+ (is (empty? (seed-rows))))))))