Skip to content

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

6.5 KB
Raw Blame
## AI usage log table

databaseChangeLog:
  - changeSet:
      id: v61.t42424d
      author: nvoxland
      comment: Create metabot_instance_limit table
      changes:
        - createTable:
            tableName: metabot_instance_limit
            remarks: Metabot usage limits at the instance or tenant level
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: tenant_id
                  type: int
                  remarks: The tenant ID, or null for instance-wide limit
                  constraints:
                    nullable: true
                    unique: true
              - column:
                  name: max_usage
                  type: int
                  remarks: The maximum usage value, interpreted according to the metabot-limit-unit setting. Null is unlimited

  - changeSet:
      id: v61.44ga4
      author: nvoxland
      comment: Add foreign key on metabot_instance_limit.tenant_id referencing tenant
      changes:
        - addForeignKeyConstraint:
            baseTableName: metabot_instance_limit
            baseColumnNames: tenant_id
            referencedTableName: tenant
            referencedColumnNames: id
            constraintName: fk_metabot_instance_limit_ref_tenant
            onDelete: CASCADE

  - changeSet:
      id: v61.gr34fp
      author: nvoxland
      comment: Create metabot_group_limit table
      changes:
        - createTable:
            tableName: metabot_group_limit
            remarks: Metabot usage limits per group
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: group_id
                  type: int
                  remarks: The ID of the associated permissions group
                  constraints:
                    nullable: false
                    unique: true
              - column:
                  name: max_usage
                  type: int
                  remarks: The maximum usage value, interpreted according to the metabot-limit-unit setting
                  constraints:
                    nullable: false

  - changeSet:
      id: v61.gr34fp-fk
      author: nvoxland
      comment: Add foreign key on metabot_group_limit.group_id referencing permissions_group
      changes:
        - addForeignKeyConstraint:
            baseTableName: metabot_group_limit
            baseColumnNames: group_id
            referencedTableName: permissions_group
            referencedColumnNames: id
            constraintName: fk_metabot_group_limit_ref_permissions_group
            onDelete: CASCADE

  - changeSet:
      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
            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'
      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
            tableName: ai_usage_log
            columns:
              - column:
                  name: user_id
      rollback: ""