Skip to content

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

BOT-1321 Add ai_usage_log migration to v60 (#72651)

applebyddc4e3604bf7
diff --git a/resources/migrations/060/20260415_ai_usage_log.yaml b/resources/migrations/060/20260415_ai_usage_log.yaml
new file mode 100644
index 00000000000..b185dc5dc2b
--- /dev/null
+++ b/resources/migrations/060/20260415_ai_usage_log.yaml
@@ -0,0 +1,104 @@
+## Backport of ai_usage_log table from v61
+
+databaseChangeLog:
+ - logicalFilePath: migrations/060_update_migrations.yaml
+
+ - changeSet:
+ id: v60.gresdvx
+ author: nvoxland
+ comment: Create the ai_usage_log
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ changeSetExecuted:
+ id: v61.gresdvx
+ author: nvoxland
+ changeLogFile: migrations/061/20260330_ai_usage_log.yaml
+ changes:
+ - createTable:
+ tableName: ai_usage_log
+ remarks: Universal ledger for all LLM API call usage tracking
+ columns:
+ - column:
+ name: id
+ type: bigint
+ autoIncrement: true
+ constraints:
+ primaryKey: true
+ nullable: false
+ - column:
+ name: created_at
+ type: ${timestamp_type}
+ defaultValueComputed: current_timestamp
+ constraints:
+ nullable: false
+ remarks: 'When the LLM call was made'
+ - column:
+ name: source
+ type: varchar(100)
+ constraints:
+ nullable: false
+ remarks: 'Origin of the LLM call, e.g. agent, slackbot, document-gen, intent-classification, example-question-gen, oss-sql-gen, semantic-search-embedding'
+ - column:
+ name: model
+ type: varchar(255)
+ constraints:
+ nullable: false
+ remarks: 'Full model identifier, e.g. anthropic/claude-sonnet-4-20250514'
+ - column:
+ name: prompt_tokens
+ type: int
+ constraints:
+ nullable: false
+ remarks: 'Number of input/prompt tokens'
+ - column:
+ name: completion_tokens
+ type: int
+ constraints:
+ nullable: false
+ remarks: 'Number of output/completion tokens'
+ - column:
+ name: total_tokens
+ type: int
+ constraints:
+ nullable: false
+ remarks: 'Sum of prompt_tokens + completion_tokens'
+ - column:
+ name: user_id
+ type: int
+ remarks: 'Metabase user who triggered the call. NULL for system/background calls.'
+ - column:
+ name: tenant_id
+ type: int
+ remarks: 'Snapshot of user tenant at request time. NULL for non-tenant users.'
+ - column:
+ name: conversation_id
+ type: varchar(36)
+ remarks: 'Reference to metabot_conversation. NULL for non-conversational calls.'
+ - column:
+ name: profile_id
+ type: varchar(100)
+ remarks: 'AI profile used, e.g. nlq, sql, slackbot'
+ - column:
+ name: request_id
+ type: varchar(36)
+ remarks: 'UUID for correlating with Snowplow events'
+
+ - changeSet:
+ id: v60.8g8esw
+ author: nvoxland
+ comment: Index on ai_usage_log.user_id
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ changeSetExecuted:
+ id: v61.8g8esw
+ author: nvoxland
+ changeLogFile: migrations/061/20260330_ai_usage_log.yaml
+ changes:
+ - createIndex:
+ indexName: idx_ai_usage_log_user_id
+ tableName: ai_usage_log
+ columns:
+ - column:
+ name: user_id
diff --git a/resources/migrations/060/20260415_ai_usage_log_ai_proxied.yaml b/resources/migrations/060/20260415_ai_usage_log_ai_proxied.yaml
new file mode 100644
index 00000000000..b6a437230de
--- /dev/null
+++ b/resources/migrations/060/20260415_ai_usage_log_ai_proxied.yaml
@@ -0,0 +1,18 @@
+## Track whether ai_usage_log entries were routed through the AI proxy
+
+databaseChangeLog:
+ - logicalFilePath: migrations/060_update_migrations.yaml
+ - changeSet:
+ id: v60.kdhxne
+ author: mappleby
+ comment: Add ai_proxied flag to ai_usage_log to track proxy vs BYOK routing
+ changes:
+ - addColumn:
+ tableName: ai_usage_log
+ columns:
+ - column:
+ name: ai_proxied
+ type: ${boolean.type}
+ remarks: Whether this call was routed through the Metabase Cloud AI proxy (true) or directly via BYOK keys (false). Null for rows created before this migration.
+ constraints:
+ nullable: true
diff --git a/resources/migrations/061/20260330_ai_usage_log.yaml b/resources/migrations/061/20260330_ai_usage_log.yaml
index cbbb04d5c66..14807f55af0 100644
--- a/resources/migrations/061/20260330_ai_usage_log.yaml
+++ b/resources/migrations/061/20260330_ai_usage_log.yaml
@@ -89,6 +89,13 @@ databaseChangeLog:
id: v61.gresdvx
author: nvoxland
comment: Create the ai_usage_log
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ changeSetExecuted:
+ id: v60.gresdvx
+ author: nvoxland
+ changeLogFile: migrations/060_update_migrations.yaml
changes:
- createTable:
tableName: ai_usage_log
@@ -158,11 +165,19 @@ databaseChangeLog:
name: request_id
type: varchar(36)
remarks: 'UUID for correlating with Snowplow events'
+ rollback: ""
- changeSet:
id: v61.8g8esw
author: nvoxland
comment: Index on ai_usage_log.user_id
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ changeSetExecuted:
+ id: v60.8g8esw
+ author: nvoxland
+ changeLogFile: migrations/060_update_migrations.yaml
changes:
- createIndex:
indexName: idx_ai_usage_log_user_id
@@ -170,3 +185,4 @@ databaseChangeLog:
columns:
- column:
name: user_id
+ rollback: ""