discourse-ai/db/migrate/20250102035341_add_llm_quota_tables.rb
Sam d07cf51653
FEATURE: llm quotas (#1047)
Adds a comprehensive quota management system for LLM models that allows:

- Setting per-group (applied per user in the group) token and usage limits with configurable durations
- Tracking and enforcing token/usage limits across user groups
- Quota reset periods (hourly, daily, weekly, or custom)
-  Admin UI for managing quotas with real-time updates

This system provides granular control over LLM API usage by allowing admins
to define limits on both total tokens and number of requests per group.
Supports multiple concurrent quotas per model and automatically handles
quota resets.


Co-authored-by: Keegan George <kgeorge13@gmail.com>
2025-01-14 15:54:09 +11:00

32 lines
949 B
Ruby

# frozen_string_literal: true
class AddLlmQuotaTables < ActiveRecord::Migration[7.2]
def change
create_table :llm_quotas do |t|
t.bigint :group_id, null: false
t.bigint :llm_model_id, null: false
t.integer :max_tokens
t.integer :max_usages
t.integer :duration_seconds, null: false
t.timestamps
end
add_index :llm_quotas, :llm_model_id
add_index :llm_quotas, %i[group_id llm_model_id], unique: true
create_table :llm_quota_usages do |t|
t.bigint :user_id, null: false
t.bigint :llm_quota_id, null: false
t.integer :input_tokens_used, null: false
t.integer :output_tokens_used, null: false
t.integer :usages, null: false
t.datetime :started_at, null: false
t.datetime :reset_at, null: false
t.timestamps
end
add_index :llm_quota_usages, :llm_quota_id
add_index :llm_quota_usages, %i[user_id llm_quota_id], unique: true
end
end