A platform for community discussion. Free, open, simple.
FEATURE: add cost limits to LLM quotas (#41277)
Sam6fc567359811
diff --git a/db/structure.sql b/db/structure.sqlindex 88e9935df6a..c90c396ae86 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -518,7 +518,8 @@ CREATE TABLE public.ai_api_audit_logs (cache_read_tokens integer,llm_id bigint,response_status integer,- request_attempts jsonb+ request_attempts jsonb,+ estimated_cost numeric(20,10));@@ -560,7 +561,8 @@ CREATE TABLE public.ai_api_request_stats (usage_count integer DEFAULT 1 NOT NULL,rolled_up boolean DEFAULT false NOT NULL,created_at timestamp(6) without time zone NOT NULL,- updated_at timestamp(6) without time zone NOT NULL+ updated_at timestamp(6) without time zone NOT NULL,+ estimated_cost numeric(20,10));@@ -6529,7 +6531,10 @@ CREATE TABLE public.llm_quota_usages (started_at timestamp(6) without time zone NOT NULL,reset_at timestamp(6) without time zone NOT NULL,created_at timestamp(6) without time zone NOT NULL,- updated_at timestamp(6) without time zone NOT NULL+ updated_at timestamp(6) without time zone NOT NULL,+ cost_used numeric(20,10) DEFAULT 0.0 NOT NULL,+ cache_read_tokens_used integer DEFAULT 0 NOT NULL,+ cache_write_tokens_used integer DEFAULT 0 NOT NULL);@@ -6564,7 +6569,8 @@ CREATE TABLE public.llm_quotas (max_usages integer,duration_seconds integer NOT NULL,created_at timestamp(6) without time zone NOT NULL,- updated_at timestamp(6) without time zone NOT NULL+ updated_at timestamp(6) without time zone NOT NULL,+ max_cost numeric(20,10));@@ -22275,6 +22281,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groupsSET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES+('20260629233141'),('20260629022603'),('20260626055145'),('20260624140945'),diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjsindex 27b84d0230b..af610fa1d53 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs@@ -12,6 +12,7 @@ import {addUniqueValueToArray,removeValueFromArray,} from "discourse/lib/array-tools";+import { groupPath } from "discourse/lib/url";import { eq, gt, not } from "discourse/truth-helpers";import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";import dBoundAvatarTemplate from "discourse/ui-kit/helpers/d-bound-avatar-template";@@ -179,10 +180,6 @@ export default class AiLlmEditorForm extends Component {});}- get showAddQuotaButton() {- return !this.args.model.isNew;- }-computeProviderParams(provider, currentParams = {}) {const params = this.args.llms.resultSetMeta.provider_params[provider] ?? {};return Object.fromEntries(@@ -581,92 +578,96 @@ export default class AiLlmEditorForm extends Component {{{/unless}}{{#if (gt data.llm_quotas.length 0)}}- <form.Container @title={{i18n "discourse_ai.llms.quotas.title"}}>- <table class="ai-llm-quotas__table">- <thead class="ai-llm-quotas__table-head">- <tr class="ai-llm-quotas__header-row">- <th class="ai-llm-quotas__header">{{i18n- "discourse_ai.llms.quotas.group"- }}</th>- <th class="ai-llm-quotas__header">{{i18n- "discourse_ai.llms.quotas.max_tokens"- }}</th>- <th class="ai-llm-quotas__header">{{i18n- "discourse_ai.llms.quotas.max_usages"- }}</th>- <th class="ai-llm-quotas__header">{{i18n- "discourse_ai.llms.quotas.duration"- }}</th>- <th- class="ai-llm-quotas__header ai-llm-quotas__header--actions"- ></th>- <th></th>- </tr>- </thead>- <tbody class="ai-llm-quotas__table-body">- <form.Collection- @name="llm_quotas"- as |collection index collectionData|- >- <tr class="ai-llm-quotas__row">- <td- class="ai-llm-quotas__cell"- >{{collectionData.group_name}}</td>- <td class="ai-llm-quotas__cell">- <collection.Field- @name="max_tokens"- @title="max_tokens"- @showTitle={{false}}- @type="input-number"- as |field|- >- <field.Control- class="ai-llm-quotas__input ai-llm-quotas__input--tokens"- min="1"- />- </collection.Field>- </td>- <td class="ai-llm-quotas__cell">- <collection.Field- @name="max_usages"- @title="max_usages"- @showTitle={{false}}- @type="input-number"- as |field|- >- <field.Control- class="ai-llm-quotas__input ai-llm-quotas__input--usages"- min="1"- />- </collection.Field>- </td>- <td class="ai-llm-quotas__cell">- <collection.Field- @name="duration_seconds"- @title="duration_seconds"- @showTitle={{false}}- @type="custom"- as |field|- >- <field.Control>- <DurationSelector- @value={{collectionData.duration_seconds}}- @onChange={{field.set}}- />- </field.Control>- </collection.Field>- </td>- <td>- <form.Button- @icon="trash-can"- @action={{fn collection.remove index}}- class="btn-danger ai-llm-quotas__delete-btn"+ <form.Container+ @title={{i18n "discourse_ai.llms.quotas.title"}}+ @format="full"+ >+ <div class="ai-llm-quotas">+ <form.Collection+ @name="llm_quotas"+ as |collection index collectionData|+ >+ <div class="ai-llm-quotas__item">+ <div class="ai-llm-quotas__item-header">+ <dl class="ai-llm-quotas__group">+ <dt class="ai-llm-quotas__group-label">{{i18n+ "discourse_ai.llms.quotas.group"+ }}</dt>+ <dd class="ai-llm-quotas__group-name">+ <a+ href={{groupPath collectionData.group_name}}+ class="ai-llm-quotas__group-link"+ >+ {{dIcon "users"}}+ <span>{{collectionData.group_name}}</span>+ </a>+ </dd>+ </dl>++ <form.Button+ @icon="trash-can"+ @action={{fn collection.remove index}}+ class="btn-danger ai-llm-quotas__delete-btn"+ />+ </div>++ <div class="ai-llm-quotas__limits">+ <collection.Field+ @name="max_tokens"+ @title={{i18n "discourse_ai.llms.quotas.max_tokens"}}+ @type="input-number"+ as |field|+ >+ <field.Control+ class="ai-llm-quotas__input ai-llm-quotas__input--tokens"+ min="1"+ />+ </collection.Field>++ <collection.Field+ @name="max_usages"+ @title={{i18n "discourse_ai.llms.quotas.max_usages"}}+ @type="input-number"+ as |field|+ >+ <field.Control+ class="ai-llm-quotas__input ai-llm-quotas__input--usages"+ min="1"/>- </td>- </tr>- </form.Collection>- </tbody>- </table>+ </collection.Field>++ <collection.Field+ @name="max_cost"+ @title={{i18n "discourse_ai.llms.quotas.max_cost"}}+ @type="input-number"+ as |field|+ >+ <field.Control+ class="ai-llm-quotas__input ai-llm-quotas__input--cost"+ min="0.01"+ step="0.01"+ />+ </collection.Field>++ <collection.Field+ @name="duration_seconds"+ @title={{i18n "discourse_ai.llms.quotas.duration"}}+ @validation="required"+ @type="custom"+ as |field|+ >+ <field.Control>+ <DurationSelector+ @value={{collectionData.duration_seconds}}+ @onChange={{field.set}}+ class="ai-llm-quotas__duration"+ />+ </field.Control>+ </collection.Field>+ </div>+ </div>+ </form.Collection>+ </div></form.Container><form.Buttondiff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-usage.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-usage.gjsindex a02748b9f69..902cf0d050b 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-usage.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-usage.gjs@@ -1,4 +1,3 @@-/* eslint-disable ember/no-tracked-properties-from-args */import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";import { fn, hash } from "@ember/helper";@@ -39,7 +38,7 @@ export default class AiUsage extends Component {@tracked startDate = moment().subtract(30, "days").toDate();@tracked endDate = new Date();- @tracked data = this.args.model;+ @tracked data;@tracked selectedFeature;@tracked selectedModel;@tracked selectedPeriod = DEFAULT_PERIOD;@@ -49,6 +48,7 @@ export default class AiUsage extends Component {constructor() {super(...arguments);+ this.data = this.args.model;this.initializeFilters(this.args.queryParams);this.fetchData();if (this.selectedFeature || this.selectedModel) {@@ -400,11 +400,7 @@ export default class AiUsage extends Component {} else if (costType === "mixed") {totalSpending += this.getCostOnlySpending(f.feature_name);} else {- totalSpending +=- (f.input_spending || 0) +- (f.cache_read_spending || 0) +- (f.cache_write_spending || 0) +- (f.output_spending || 0);+ totalSpending += this.spendingValue(f);}});@@ -421,12 +417,7 @@ export default class AiUsage extends Component {usage_count: models.reduce((sum, m) => sum + (m.usage_count || 0), 0),total_tokens: models.reduce((sum, m) => sum + (m.total_tokens || 0), 0),total_spending: models.reduce(- (sum, m) =>- sum +- (m.input_spending || 0) +- (m.cache_read_spending || 0) +- (m.cache_write_spending || 0) +- (m.output_spending || 0),+ (sum, model) => sum + this.spendingValue(model),0),};@@ -438,17 +429,22 @@ export default class AiUsage extends Component {usage_count: users.reduce((sum, u) => sum + (u.usage_count || 0), 0),total_tokens: users.reduce((sum, u) => sum + (u.total_tokens || 0), 0),total_spending: users.reduce(- (sum, u) =>- sum +- (u.input_spending || 0) +- (u.cache_read_spending || 0) +- (u.cache_write_spending || 0) +- (u.output_spending || 0),+ (sum, user) => sum + this.spendingValue(user),0),};}+ spendingValue(row) {+ return (+ row.total_spending ??+ (row.input_spending || 0) ++ (row.cache_read_spending || 0) ++ (row.cache_write_spending || 0) ++ (row.output_spending || 0)+ );+ }+@bindformatSpending(value) {return `$${(value || 0).toFixed(2)}`;@@ -491,12 +487,7 @@ export default class AiUsage extends Component {getCostOnlySpending(featureName) {const costModels = this.getCostModelsForFeature(featureName);return costModels.reduce(- (sum, m) =>- sum +- (m.input_spending || 0) +- (m.cache_read_spending || 0) +- (m.cache_write_spending || 0) +- (m.output_spending || 0),+ (sum, model) => sum + this.spendingValue(model),0);}@@ -609,13 +600,15 @@ export default class AiUsage extends Component {inputSpending,cacheReadSpending,cacheWriteSpending,- outputSpending+ outputSpending,+ estimatedSpending) {const total =+ estimatedSpending ??(inputSpending || 0) +- (cacheReadSpending || 0) +- (cacheWriteSpending || 0) +- (outputSpending || 0);+ (cacheReadSpending || 0) ++ (cacheWriteSpending || 0) ++ (outputSpending || 0);return `$${total.toFixed(2)}`;}@@ -777,6 +770,7 @@ export default class AiUsage extends Component {feature.cache_read_spendingfeature.cache_write_spendingfeature.output_spending+ feature.total_spending}}{{else}}<span class="ai-usage__mixed-cost">@@ -903,6 +897,7 @@ export default class AiUsage extends Component {model.cache_read_spendingmodel.cache_write_spendingmodel.output_spending+ model.total_spending}}</td></tr>{{/each}}@@ -1009,6 +1004,7 @@ export default class AiUsage extends Component {model.cache_read_spendingmodel.cache_write_spendingmodel.output_spending+ model.total_spending}}</td></tr>@@ -1095,6 +1091,7 @@ export default class AiUsage extends Component {user.cache_read_spendinguser.cache_write_spendinguser.output_spending+ user.total_spending}}</td></tr>@@ -1161,6 +1158,7 @@ export default class AiUsage extends Component {user.cache_read_spendinguser.cache_write_spendinguser.output_spending+ user.total_spending}}</td></tr>diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llm_quotas_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llm_quotas_controller.rbindex 725d2fd5cb6..c2b868a54ae 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llm_quotas_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llm_quotas_controller.rb@@ -51,6 +51,7 @@ module DiscourseAi:llm_model_id,:max_tokens,:max_usages,+ :max_cost,:duration_seconds,)enddiff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rbindex 561b88c93c9..c612ebdba10 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb@@ -68,21 +68,23 @@ module DiscourseAiinitial_quotas = llm_model.llm_quotas.map(&:attributes)if params[:ai_llm].key?(:llm_quotas)- if quota_params- existing_quota_group_ids = llm_model.llm_quotas.pluck(:group_id)- new_quota_group_ids = quota_params.map { |q| q[:group_id] }-- llm_model- .llm_quotas- .where(group_id: existing_quota_group_ids - new_quota_group_ids)- .destroy_all-- quota_params.each do |quota_param|- quota = llm_model.llm_quotas.find_or_initialize_by(group_id: quota_param[:group_id])- quota.update!(quota_param)+ ActiveRecord::Base.transaction do+ if quota_params+ existing_quota_group_ids = llm_model.llm_quotas.pluck(:group_id)+ new_quota_group_ids = quota_params.map { |q| q[:group_id] }++ llm_model+ .llm_quotas+ .where(group_id: existing_quota_group_ids - new_quota_group_ids)+ .destroy_all++ quota_params.each do |quota_param|+ quota = llm_model.llm_quotas.find_or_initialize_by(group_id: quota_param[:group_id])+ quota.update!(quota_param)+ end+ else+ llm_model.llm_quotas.destroy_allend- else- llm_model.llm_quotas.destroy_allendend@@ -103,6 +105,8 @@ module DiscourseAielserender_json_error llm_modelend+ rescue ActiveRecord::RecordInvalid => e+ render_json_error e.recordenddef destroy@@ -180,14 +184,20 @@ module DiscourseAiparams[:ai_llm][:llm_quotas].map do |quota|mapped = {}mapped[:group_id] = quota[:group_id].to_i- mapped[:max_tokens] = quota[:max_tokens].to_i if quota[:max_tokens].present?- mapped[:max_usages] = quota[:max_usages].to_i if quota[:max_usages].present?+ %i[max_tokens max_usages].each do |key|+ mapped[key] = optional_integer_param(quota[key]) if quota.key?(key)+ end+ mapped[:max_cost] = quota[:max_cost].presence if quota.key?(:max_cost)mapped[:duration_seconds] = quota[:duration_seconds].to_imappedendendend+ def optional_integer_param(value)+ value.presence&.to_i+ end+def credit_allocation_paramsreturn nil if params[:ai_llm][:llm_credit_allocation].blank?@@ -316,7 +326,13 @@ module DiscourseAientity_details[:quotas] = llm_model.llm_quotas.map do |quota|- "Group #{quota.group_id}: #{quota.max_tokens} tokens, #{quota.max_usages} usages, #{quota.duration_seconds}s"+ quota_summary(+ quota.group_id,+ quota.max_tokens,+ quota.max_usages,+ quota.max_cost,+ quota.duration_seconds,+ )end.join("; ")end@@ -333,11 +349,27 @@ module DiscourseAiif initial_quotas != current_quotasinitial_quota_summary =initial_quotas- .map { |q| "Group #{q["group_id"]}: #{q["max_tokens"]} tokens" }+ .map do |q|+ quota_summary(+ q["group_id"],+ q["max_tokens"],+ q["max_usages"],+ q["max_cost"],+ q["duration_seconds"],+ )+ end.join("; ")current_quota_summary =current_quotas- .map { |q| "Group #{q["group_id"]}: #{q["max_tokens"]} tokens" }+ .map do |q|+ quota_summary(+ q["group_id"],+ q["max_tokens"],+ q["max_usages"],+ q["max_cost"],+ q["duration_seconds"],+ )+ end.join("; ")entity_details[:quotas_changed] = trueentity_details[:quotas] = "#{initial_quota_summary} → #{current_quota_summary}"@@ -352,6 +384,20 @@ module DiscourseAi)end+ def quota_summary(group_id, max_tokens, max_usages, max_cost, duration_seconds)+ limits = []+ limits << "#{max_tokens} tokens" if max_tokens.present?+ limits << "#{max_usages} usages" if max_usages.present?+ limits << "$#{formatted_decimal(max_cost)} cost" if max_cost.present?+ limits << "#{duration_seconds}s" if duration_seconds.present?++ "Group #{group_id}: #{limits.join(", ")}"+ end++ def formatted_decimal(value)+ value.is_a?(BigDecimal) ? value.to_s("F") : value+ end+def log_llm_model_deletion(model_details)logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)model_details[:subject] = model_details[:display_name]diff --git a/plugins/discourse-ai/app/models/ai_api_audit_log.rb b/plugins/discourse-ai/app/models/ai_api_audit_log.rbindex 175df4f4315..4dbea109e5e 100644--- a/plugins/discourse-ai/app/models/ai_api_audit_log.rb+++ b/plugins/discourse-ai/app/models/ai_api_audit_log.rb@@ -24,15 +24,20 @@ class AiApiAuditLog < ActiveRecord::Baseenddef self.token_and_spending_stats(scope)- spending_expr = LlmModel.spending_sql(table_name)- request_tokens, response_tokens, cache_read_tokens, cache_write_tokens, spending, model_count =+ spending_expr = LlmModel.estimated_or_calculated_spending_sql(table_name)+ request_tokens,+ response_tokens,+ cache_read_tokens,+ cache_write_tokens,+ spending,+ spending_count =scope.joins("LEFT JOIN llm_models ON llm_models.id = #{table_name}.llm_id").pick(Arel.sql("COALESCE(SUM(COALESCE(#{table_name}.request_tokens, 0)), 0)"),Arel.sql("COALESCE(SUM(COALESCE(#{table_name}.response_tokens, 0)), 0)"),Arel.sql("COALESCE(SUM(COALESCE(#{table_name}.cache_read_tokens, 0)), 0)"),Arel.sql("COALESCE(SUM(COALESCE(#{table_name}.cache_write_tokens, 0)), 0)"),- Arel.sql("COALESCE(SUM(#{spending_expr}), 0) / 1000000.0"),- Arel.sql("COUNT(llm_models.id)"),+ Arel.sql("SUM(#{spending_expr})"),+ Arel.sql("COUNT(#{spending_expr})"),){@@ -40,7 +45,7 @@ class AiApiAuditLog < ActiveRecord::Baseresponse_tokens: response_tokens.to_i,cache_read_tokens: cache_read_tokens.to_i,cache_write_tokens: cache_write_tokens.to_i,- spending: model_count.to_i.positive? ? spending.to_f.round(6) : nil,+ spending: spending_count.to_i.positive? ? spending.to_f.round(6) : nil,}end@@ -61,6 +66,7 @@ end# cache_read_tokens :integer# cache_write_tokens :integer# duration_msecs :integer+# estimated_cost :decimal(20, 10)# feature_context :jsonb# feature_name :string(255)# language_model :string(255)diff --git a/plugins/discourse-ai/app/models/ai_api_request_stat.rb b/plugins/discourse-ai/app/models/ai_api_request_stat.rbindex 31279a623a1..c20cada2eb1 100644--- a/plugins/discourse-ai/app/models/ai_api_request_stat.rb+++ b/plugins/discourse-ai/app/models/ai_api_request_stat.rb@@ -19,6 +19,7 @@ class AiApiRequestStat < ActiveRecord::Baseresponse_tokens: log.response_tokens || 0,cache_read_tokens: log.cache_read_tokens || 0,cache_write_tokens: log.cache_write_tokens || 0,+ estimated_cost: estimated_cost_for(log, llm_model),usage_count: 1,rolled_up: false,created_at: timestamp,@@ -28,6 +29,18 @@ class AiApiRequestStat < ActiveRecord::BaseRails.logger.warn("Failed to persist AiApiRequestStat: #{e.message}")end+ def estimated_cost_for(log, llm_model)+ return log.estimated_cost if !log.estimated_cost.nil?+ return if llm_model.blank?++ llm_model.estimated_cost_for_tokens(+ request_tokens: log.request_tokens,+ response_tokens: log.response_tokens,+ cache_read_tokens: log.cache_read_tokens,+ cache_write_tokens: log.cache_write_tokens,+ )+ end+def rollup!cutoff = SiteSetting.ai_usage_rollup_after_days.days.ago.end_of_day@@ -56,6 +69,7 @@ class AiApiRequestStat < ActiveRecord::Baseresponse_tokens,cache_read_tokens,cache_write_tokens,+ estimated_cost,usage_count,rolled_up,created_at,@@ -63,29 +77,31 @@ class AiApiRequestStat < ActiveRecord::Base)SELECT:start_time,- user_id,- provider_id,- llm_id,- language_model,- feature_name,- SUM(request_tokens),- SUM(response_tokens),- SUM(cache_read_tokens),- SUM(cache_write_tokens),+ ai_api_request_stats.user_id,+ ai_api_request_stats.provider_id,+ ai_api_request_stats.llm_id,+ ai_api_request_stats.language_model,+ ai_api_request_stats.feature_name,+ SUM(ai_api_request_stats.request_tokens),+ SUM(ai_api_request_stats.response_tokens),+ SUM(ai_api_request_stats.cache_read_tokens),+ SUM(ai_api_request_stats.cache_write_tokens),+ SUM(#{LlmModel.estimated_or_calculated_spending_sql(:ai_api_request_stats)}),SUM(usage_count),true,:start_time,:nowFROM ai_api_request_stats- WHERE bucket_date >= :start_time- AND bucket_date <= :end_time- AND rolled_up = false+ LEFT JOIN llm_models ON llm_models.id = ai_api_request_stats.llm_id+ WHERE ai_api_request_stats.bucket_date >= :start_time+ AND ai_api_request_stats.bucket_date <= :end_time+ AND ai_api_request_stats.rolled_up = falseGROUP BY- user_id,- provider_id,- llm_id,- language_model,- feature_name+ ai_api_request_stats.user_id,+ ai_api_request_stats.provider_id,+ ai_api_request_stats.llm_id,+ ai_api_request_stats.language_model,+ ai_api_request_stats.feature_nameSQLDB.exec(<<~SQL, start_time: start_time, end_time: end_time)@@ -114,6 +130,7 @@ end# bucket_date :datetime not null# cache_read_tokens :integer default(0), not null# cache_write_tokens :integer default(0), not null+# estimated_cost :decimal(20, 10)# feature_name :string(255)# language_model :string(255)# request_tokens :integer default(0), not nulldiff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rbindex 34b1dc5e2e1..da1dbe106fa 100644--- a/plugins/discourse-ai/app/models/llm_model.rb+++ b/plugins/discourse-ai/app/models/llm_model.rb@@ -45,12 +45,49 @@ class LlmModel < ActiveRecord::BaseCOST_COMPONENTS.keys.map { |k| spending_component_sql(k, table) }.join(" + ")end+ def self.spending_dollars_sql(table)+ "(#{spending_sql(table)}) / 1000000.0"+ end++ def self.estimated_or_calculated_spending_sql(table)+ qt = connection.quote_table_name(table.to_s)+ "COALESCE(#{qt}.estimated_cost, CASE WHEN llm_models.id IS NULL THEN NULL ELSE #{spending_dollars_sql(table)} END)"+ end++ def estimated_cost_for_tokens(+ request_tokens:,+ response_tokens:,+ cache_read_tokens: 0,+ cache_write_tokens: 0+ )+ return nil if !costs_configured?++ [+ [request_tokens, input_cost],+ [response_tokens, output_cost],+ [cache_read_tokens, cached_input_cost],+ [cache_write_tokens, cache_write_cost],+ ].sum(BigDecimal("0")) do |tokens, cost|+ BigDecimal(tokens.to_i.to_s) * BigDecimal((cost || 0).to_s) / 1_000_000+ end+ end+def spending_for(record)- total =- COST_COMPONENTS.values.sum do |info|- record.public_send(info[:tokens]).to_i * public_send(info[:cost]).to_f- end- (total / 1_000_000.0).round(6)+ if record.respond_to?(:estimated_cost) && record.estimated_cost.present?+ record.estimated_cost.to_d.round(6).to_f+ else+ estimated_cost_for_tokens(+ request_tokens: record.request_tokens,+ response_tokens: record.response_tokens,+ cache_read_tokens: record.cache_read_tokens,+ cache_write_tokens: record.cache_write_tokens,+ )&.round(6)&.to_f+ end+ end++ def costs_configured?+ [input_cost, output_cost, cached_input_cost].any? { |cost| !cost.nil? } ||+ cache_write_cost.to_f != 0endhas_many :llm_quotas, dependent: :destroydiff --git a/plugins/discourse-ai/app/models/llm_quota.rb b/plugins/discourse-ai/app/models/llm_quota.rbindex 185cc7e9ca9..ba4a8d62ac2 100644--- a/plugins/discourse-ai/app/models/llm_quota.rb+++ b/plugins/discourse-ai/app/models/llm_quota.rb@@ -13,6 +13,7 @@ class LlmQuota < ActiveRecord::Basevalidates :duration_seconds, presence: true, numericality: { greater_than: 0 }validates :max_tokens, numericality: { only_integer: true, greater_than: 0, allow_nil: true }validates :max_usages, numericality: { greater_than: 0, allow_nil: true }+ validates :max_cost, numericality: { greater_than: 0, allow_nil: true }validate :at_least_one_limit@@ -37,14 +38,36 @@ class LlmQuota < ActiveRecord::Baseraise errors.firstend- def self.log_usage(llm, user, input_tokens, output_tokens)+ def self.log_usage(+ llm,+ user,+ request_tokens:,+ response_tokens:,+ cache_read_tokens: 0,+ cache_write_tokens: 0,+ estimated_cost: nil+ )return if user.blank?+ estimated_cost ||=+ llm.estimated_cost_for_tokens(+ request_tokens: request_tokens,+ response_tokens: response_tokens,+ cache_read_tokens: cache_read_tokens,+ cache_write_tokens: cache_write_tokens,+ )+quotas = joins(:group).where(llm_model: llm).where(group: user.groups)quotas.each do |quota|usage = LlmQuotaUsage.find_or_create_for(user: user, llm_quota: quota)- usage.increment_usage!(input_tokens: input_tokens, output_tokens: output_tokens)+ usage.increment_usage!(+ input_tokens: request_tokens,+ output_tokens: response_tokens,+ cache_read_tokens: cache_read_tokens,+ cache_write_tokens: cache_write_tokens,+ cost: estimated_cost,+ )endend@@ -56,10 +79,14 @@ class LlmQuota < ActiveRecord::Basemax_usagesend+ def available_cost+ max_cost+ end+privatedef at_least_one_limit- if max_tokens.nil? && max_usages.nil?+ if max_tokens.nil? && max_usages.nil? && max_cost.nil?errors.add(:base, I18n.t("discourse_ai.errors.quota_required"))endend@@ -71,6 +98,7 @@ end## id :bigint not null, primary key# duration_seconds :integer not null+# max_cost :decimal(20, 10)# max_tokens :integer# max_usages :integer# created_at :datetime not nulldiff --git a/plugins/discourse-ai/app/models/llm_quota_usage.rb b/plugins/discourse-ai/app/models/llm_quota_usage.rbindex bdb5b9e7bba..2f62d07d8e9 100644--- a/plugins/discourse-ai/app/models/llm_quota_usage.rb+++ b/plugins/discourse-ai/app/models/llm_quota_usage.rb@@ -12,24 +12,26 @@ class LlmQuotaUsage < ActiveRecord::Basevalidates :llm_quota_id, presence: truevalidates :input_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 }validates :output_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 }+ validates :cache_read_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 }+ validates :cache_write_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 }+ validates :cost_used, presence: true, numericality: { greater_than_or_equal_to: 0 }validates :usages, presence: true, numericality: { greater_than_or_equal_to: 0 }validates :started_at, presence: truevalidates :reset_at, presence: truedef self.find_or_create_for(user:, llm_quota:)- usage = find_or_initialize_by(user: user, llm_quota: llm_quota)-- if usage.new_record?- now = Time.current- usage.started_at = now- usage.reset_at = now + llm_quota.duration_seconds.seconds- usage.input_tokens_used = 0- usage.output_tokens_used = 0- usage.usages = 0- usage.save!- end-- usage+ find_by(user: user, llm_quota: llm_quota) ||+ create_or_find_by!(user: user, llm_quota: llm_quota) do |usage|+ now = Time.current+ usage.started_at = now+ usage.reset_at = now + llm_quota.duration_seconds.seconds+ usage.input_tokens_used = 0+ usage.output_tokens_used = 0+ usage.cache_read_tokens_used = 0+ usage.cache_write_tokens_used = 0+ usage.cost_used = 0+ usage.usages = 0+ endenddef reset_if_needed!@@ -39,18 +41,34 @@ class LlmQuotaUsage < ActiveRecord::Baseupdate!(input_tokens_used: 0,output_tokens_used: 0,+ cache_read_tokens_used: 0,+ cache_write_tokens_used: 0,+ cost_used: 0,usages: 0,started_at: now,reset_at: now + llm_quota.duration_seconds.seconds,)end- def increment_usage!(input_tokens:, output_tokens:)- reset_if_needed!-- increment!(:usages)- increment!(:input_tokens_used, input_tokens)- increment!(:output_tokens_used, output_tokens)+ def increment_usage!(+ input_tokens:,+ output_tokens:,+ cache_read_tokens: 0,+ cache_write_tokens: 0,+ cost: nil+ )+ with_lock do+ reset_if_needed!++ self.usages += 1+ self.input_tokens_used += input_tokens.to_i+ self.output_tokens_used += output_tokens.to_i+ self.cache_read_tokens_used += cache_read_tokens.to_i+ self.cache_write_tokens_used += cache_write_tokens.to_i+ self.cost_used += BigDecimal(cost.to_s) if cost.present?++ save!+ endenddef check_quota!@@ -70,7 +88,8 @@ class LlmQuotaUsage < ActiveRecord::Basereturn false if !llm_quota(llm_quota.max_tokens.present? && total_tokens_used > llm_quota.max_tokens) ||- (llm_quota.max_usages.present? && usages > llm_quota.max_usages)+ (llm_quota.max_usages.present? && usages > llm_quota.max_usages) ||+ (llm_quota.max_cost.present? && cost_used > llm_quota.max_cost)enddef total_tokens_used@@ -87,6 +106,11 @@ class LlmQuotaUsage < ActiveRecord::Base[0, llm_quota.max_usages - usages].maxend+ def remaining_cost+ return nil if llm_quota.max_cost.nil?+ [0, llm_quota.max_cost - cost_used].max+ end+def percentage_tokens_usedreturn 0 if llm_quota.max_tokens.nil? || llm_quota.max_tokens.zero?[(total_tokens_used.to_f / llm_quota.max_tokens * 100).round, 100].min@@ -96,22 +120,30 @@ class LlmQuotaUsage < ActiveRecord::Basereturn 0 if llm_quota.max_usages.nil? || llm_quota.max_usages.zero?[(usages.to_f / llm_quota.max_usages * 100).round, 100].minend++ def percentage_cost_used+ return 0 if llm_quota.max_cost.nil? || llm_quota.max_cost.zero?+ [(cost_used.to_f / llm_quota.max_cost * 100).round, 100].min+ endend# == Schema Information## Table name: llm_quota_usages#-# id :bigint not null, primary key-# input_tokens_used :integer not null-# output_tokens_used :integer not null-# reset_at :datetime not null-# started_at :datetime not null-# usages :integer not null-# created_at :datetime not null-# updated_at :datetime not null-# llm_quota_id :bigint not null-# user_id :bigint not null+# id :bigint not null, primary key+# cache_read_tokens_used :integer default(0), not null+# cache_write_tokens_used :integer default(0), not null+# cost_used :decimal(20, 10) default(0.0), not null+# input_tokens_used :integer not null+# output_tokens_used :integer not null+# reset_at :datetime not null+# started_at :datetime not null+# usages :integer not null+# created_at :datetime not null+# updated_at :datetime not null+# llm_quota_id :bigint not null+# user_id :bigint not null## Indexes#diff --git a/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb b/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rbindex 33d705eeae5..07224f3a991 100644--- a/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb+++ b/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb@@ -36,6 +36,8 @@ class AiApiAuditLogSerializer < ApplicationSerializerenddef spending+ return object.estimated_cost.to_d.round(6).to_f if !object.estimated_cost.nil?+object.llm_model&.spending_for(object)enddiff --git a/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb b/plugins/discourse-ai/app/serializers/ai_usage_serializer.rbindex 87c8b7a1cec..63081891130 100644--- a/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb+++ b/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb@@ -33,6 +33,7 @@ class AiUsageSerializer < ApplicationSerializertotal_cache_write_tokenstotal_request_tokenstotal_response_tokens+ total_spendinginput_spendingoutput_spendingcache_read_spending@@ -90,6 +91,7 @@ class AiUsageSerializer < ApplicationSerializeroutput_spending: user.output_spending,cache_read_spending: user.cache_read_spending,cache_write_spending: user.cache_write_spending,+ total_spending: user.total_spending,}endend@@ -126,6 +128,7 @@ class AiUsageSerializer < ApplicationSerializer:total_cache_write_tokens => row.total_cache_write_tokens,:total_request_tokens => row.total_request_tokens,:total_response_tokens => row.total_response_tokens,+ :total_spending => row.total_spending,:input_spending => row.input_spending,:output_spending => row.output_spending,:cache_read_spending => row.cache_read_spending,diff --git a/plugins/discourse-ai/app/serializers/llm_quota_serializer.rb b/plugins/discourse-ai/app/serializers/llm_quota_serializer.rbindex 4db1fb7ffa3..696c2a60e8d 100644--- a/plugins/discourse-ai/app/serializers/llm_quota_serializer.rb+++ b/plugins/discourse-ai/app/serializers/llm_quota_serializer.rb@@ -1,7 +1,14 @@# frozen_string_literal: trueclass LlmQuotaSerializer < ApplicationSerializer- attributes :id, :group_id, :llm_model_id, :max_tokens, :max_usages, :duration_seconds, :group_name+ attributes :id,+ :group_id,+ :llm_model_id,+ :max_tokens,+ :max_usages,+ :max_cost,+ :duration_seconds,+ :group_namedef group_nameobject.group.namediff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-quota-duration-selector.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-quota-duration-selector.gjsindex 5a8d9373ba8..1f9b9f7394d 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-quota-duration-selector.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-quota-duration-selector.gjs@@ -84,7 +84,7 @@ export default class DurationSelector extends Component {}<template>- <div class="duration-selector">+ <div class="duration-selector" ...attributes><ComboBox@content={{this.presetOptions}}@value={{this.selectedPresetId}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-llm-quota-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-llm-quota-modal.gjsindex ce7df452600..b789d6bdfea 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-llm-quota-modal.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-llm-quota-modal.gjs@@ -47,6 +47,7 @@ export default class AiLlmQuotaModal extends Component {llm_model_id: null,max_tokens: null,max_usages: null,+ max_cost: null,duration_seconds: moment.duration(1, "day").asSeconds(),};}@@ -58,7 +59,7 @@ export default class AiLlmQuotaModal extends Component {@actionvalidateForm(data, { addError, removeError }) {- if (!data.max_tokens && !data.max_usages) {+ if (!data.max_tokens && !data.max_usages && !data.max_cost) {addError("max_tokens", {title: i18n("discourse_ai.llms.quotas.max_tokens"),message: i18n("discourse_ai.llms.quotas.max_tokens_required"),@@ -67,9 +68,14 @@ export default class AiLlmQuotaModal extends Component {title: i18n("discourse_ai.llms.quotas.max_usages"),message: i18n("discourse_ai.llms.quotas.max_usages_required"),});+ addError("max_cost", {+ title: i18n("discourse_ai.llms.quotas.max_cost"),+ message: i18n("discourse_ai.llms.quotas.max_cost_required"),+ });} else {removeError("max_tokens");removeError("max_usages");+ removeError("max_cost");}}@@ -126,6 +132,17 @@ export default class AiLlmQuotaModal extends Component {<field.Control min="1" /></form.Field>+ <form.Field+ @name="max_cost"+ @title={{i18n "discourse_ai.llms.quotas.max_cost"}}+ @tooltip={{i18n "discourse_ai.llms.quotas.max_cost_help"}}+ @format="large"+ @type="input-number"+ as |field|+ >+ <field.Control min="0.01" step="0.01" />+ </form.Field>+<form.Field@name="duration_seconds"@title={{i18n "discourse_ai.llms.quotas.duration"}}diff --git a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-llm-quotas.scss b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-llm-quotas.scssindex a8f7aa1ae98..dd38a225651 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-llm-quotas.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-llm-quotas.scss@@ -1,68 +1,86 @@.ai-llm-quotas {+ display: flex;+ flex-direction: column;+ gap: var(--space-3);+ width: 100%;margin: 1em 0;- &__table {- border-collapse: collapse;- margin-bottom: 1em;-- .form-kit__collection {- display: contents;- }+ > .form-kit__collection {+ display: flex;+ flex-direction: column;+ gap: var(--space-3);}- > .form-kit__container {- align-items: stretch;+ &__item {+ padding: var(--space-3);+ border: 1px solid var(--primary-low);+ background: var(--secondary);}- .form-kit__container-content {- width: 100%;+ &__item-header {+ display: flex;+ align-items: center;+ justify-content: space-between;+ gap: var(--space-2);+ margin-bottom: var(--space-3);}- &__table-head {- background-color: var(--primary-very-low);+ &__group {+ display: flex;+ flex-direction: column;+ gap: var(--space-1);+ min-width: 0;+ margin: 0;}- &__header {- text-align: left;- padding: 0.5em;- font-weight: bold;- border-bottom: 2px solid var(--primary-low);+ &__group-label {+ color: var(--primary);+ font-weight: 600;+ }- &--actions {- width: 50px;- }+ &__group-name {+ margin: 0;+ overflow-wrap: anywhere;}- &__row {- border-bottom: 1px solid var(--primary-low);+ &__group-link {+ display: flex;+ align-items: center;+ gap: var(--space-1);++ .d-icon {+ flex: 0 0 auto;+ color: var(--primary-medium);+ }}- &__cell {- vertical-align: middle;- padding: var(--space-2);- white-space: nowrap;- width: 1%;+ &__limits {+ display: grid;+ grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr));+ gap: var(--space-3);+ align-items: start;- .form-kit__container {- align-items: stretch;+ .form-kit__field {+ min-width: 0;}- &--actions {- text-align: center;+ .form-kit__container-content {+ width: 100% !important;+ min-width: 0 !important;}}&__input[type="number"] {- padding: 0.5em;+ width: 100%;margin-bottom: 0;}- &__delete-btn {- padding: 0.3em 0.5em;+ &__duration {+ min-width: max-content;}- &__add-btn {- padding: 0.3em 0.5em;+ &__delete-btn {+ flex: 0 0 auto;}}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex b578035c8fd..0865a42da09 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -918,6 +918,7 @@ en:group: "Group"max_tokens: "Max tokens"max_usages: "Max uses"+ max_cost: "Max cost"duration: "Duration"confirm_delete: "Are you sure you want to delete this quota?"add: "Add quota"@@ -929,9 +930,11 @@ en:custom: "Custom..."hours: "hours"max_tokens_help: "Maximum number of tokens (words and characters) that each user in this group can use within the specified duration. Tokens are the units used by AI models to process text - roughly 1 token = 4 characters or 3/4 of a word."- max_tokens_required: "Must be set if max usages is not set"+ max_tokens_required: "Must be set if max uses and max cost are not set"max_usages_help: "Maximum number of times each user in this group can use the AI model within the specified duration. This quota is tracked per individual user, not shared across the group."- max_usages_required: "Must be set if max tokens is not set"+ max_usages_required: "Must be set if max tokens and max cost are not set"+ max_cost_help: "Maximum estimated cost each user in this group can spend on this AI model within the specified duration. Cost is based on the model's configured input, output, cached input, and cache write prices."+ max_cost_required: "Must be set if max tokens and max uses are not set"credit_allocation:credits_remaining: "%{remaining} of %{total} credits remaining (%{percentage}%)"soft_limit_warning: "This model has %{percentage}% of AI credits remaining today"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 0563b342440..d5213392599 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -991,7 +991,7 @@ en:errors:quota_exceeded: "You have exceeded the quota for this model. Please try again in %{relative_time}."- quota_required: "You must specify maximum tokens or usages for this model"+ quota_required: "You must specify maximum tokens, uses, or cost for this model"no_query_specified: The query parameter is required, please specify it.no_user_for_agent: The agent specified does not have a user associated with it.agent_not_found: "The agent specified does not exist. Check the agent_name or agent_id params (legacy: persona_name or persona_id)."diff --git a/plugins/discourse-ai/db/migrate/20260629233141_add_cost_to_llm_quotas_and_ai_usage.rb b/plugins/discourse-ai/db/migrate/20260629233141_add_cost_to_llm_quotas_and_ai_usage.rbnew file mode 100644index 00000000000..729b84030fc--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260629233141_add_cost_to_llm_quotas_and_ai_usage.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++class AddCostToLlmQuotasAndAiUsage < ActiveRecord::Migration[8.0]+ def change+ add_column :llm_quotas, :max_cost, :decimal, precision: 20, scale: 10+ add_column :llm_quota_usages,+ :cost_used,+ :decimal,+ precision: 20,+ scale: 10,+ default: 0,+ null: false+ add_column :llm_quota_usages, :cache_read_tokens_used, :integer, default: 0, null: false+ add_column :llm_quota_usages, :cache_write_tokens_used, :integer, default: 0, null: false+ add_column :ai_api_audit_logs, :estimated_cost, :decimal, precision: 20, scale: 10+ add_column :ai_api_request_stats, :estimated_cost, :decimal, precision: 20, scale: 10+ end+enddiff --git a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rbindex 847dcd8c670..e73b4eb5e02 100644--- a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb@@ -192,6 +192,7 @@ module DiscourseAiprocessor.cache_write_input_tokens if processor.cache_write_input_tokenslog.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank?log.response_status ||= 200 if call_status == :success+ log.estimated_cost = estimated_cost_for(log)log.created_at = start_timelog.updated_at = Time.nowlog.duration_msecs = (Time.now - start_time) * 1000@@ -200,7 +201,15 @@ module DiscourseAiexecution_context&.token_usage_tracker&.add_from_audit_log(log)AiApiRequestStat.record_from_audit_log(log, llm_model: @llm_model)- LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens)+ LlmQuota.log_usage(+ @llm_model,+ user,+ request_tokens: log.request_tokens,+ response_tokens: log.response_tokens,+ cache_read_tokens: log.cache_read_tokens,+ cache_write_tokens: log.cache_write_tokens,+ estimated_cost: log.estimated_cost,+ )LlmCreditAllocation.deduct_credits!(@llm_model,feature_name,diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rbindex d9aa0b6dcb7..3412a61ae49 100644--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb@@ -474,6 +474,15 @@ module DiscourseAi# for people that need to overrideend+ def estimated_cost_for(log)+ llm_model.estimated_cost_for_tokens(+ request_tokens: log.request_tokens,+ response_tokens: log.response_tokens,+ cache_read_tokens: log.cache_read_tokens,+ cache_write_tokens: log.cache_write_tokens,+ )+ end+def default_optionsraise NotImplementedErrorend@@ -760,6 +769,7 @@ module DiscourseAifinal_log_update(log)log.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank?log.response_status ||= 200 if call_status == :success+ log.estimated_cost = estimated_cost_for(log)log.created_at = start_timelog.updated_at = Time.nowlog.duration_msecs = (Time.now - start_time) * 1000@@ -768,7 +778,15 @@ module DiscourseAiexecution_context&.token_usage_tracker&.add_from_audit_log(log)AiApiRequestStat.record_from_audit_log(log, llm_model: @llm_model)- LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens)+ LlmQuota.log_usage(+ @llm_model,+ user,+ request_tokens: log.request_tokens,+ response_tokens: log.response_tokens,+ cache_read_tokens: log.cache_read_tokens,+ cache_write_tokens: log.cache_write_tokens,+ estimated_cost: log.estimated_cost,+ )LlmCreditAllocation.deduct_credits!(@llm_model,feature_name,diff --git a/plugins/discourse-ai/lib/completions/report.rb b/plugins/discourse-ai/lib/completions/report.rbindex 3b89c2fe5e8..2dfb9003bff 100644--- a/plugins/discourse-ai/lib/completions/report.rb+++ b/plugins/discourse-ai/lib/completions/report.rb@@ -50,10 +50,7 @@ module DiscourseAienddef total_spending- total =- total_input_spending + total_output_spending + total_cache_read_spending +- total_cache_write_spending- total.round(2)+ (stats.total_spending || 0).to_f.round(2)enddef total_input_spending@@ -83,7 +80,14 @@ module DiscourseAienddef stats- @stats ||= base_query.select("SUM(usage_count) as total_requests", *token_total_columns)[0]+ @stats ||=+ base_query.joins(LLM_MODEL_JOIN).select(+ "SUM(usage_count) as total_requests",+ *token_total_columns,+ spending_total_column,+ )[+ 0+ ]enddef model_costs@@ -256,7 +260,11 @@ module DiscourseAiexpr = LlmModel.spending_component_sql(component, :ai_api_request_stats)"SUM(#{expr}) / 1000000.0 as #{component}_spending"end- [*token_total_columns, *spending_columns]+ [*token_total_columns, spending_total_column, *spending_columns]+ end++ def spending_total_column+ "SUM(#{LlmModel.estimated_or_calculated_spending_sql(:ai_api_request_stats)}) as total_spending"endendenddiff --git a/plugins/discourse-ai/spec/fabricators/llm_quota_usage_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_quota_usage_fabricator.rbindex cf6e14be8e4..9aecf3b6c89 100644--- a/plugins/discourse-ai/spec/fabricators/llm_quota_usage_fabricator.rb+++ b/plugins/discourse-ai/spec/fabricators/llm_quota_usage_fabricator.rb@@ -5,6 +5,9 @@ Fabricator(:llm_quota_usage) dollm_quotainput_tokens_used { 0 }output_tokens_used { 0 }+ cache_read_tokens_used { 0 }+ cache_write_tokens_used { 0 }+ cost_used { 0 }usages { 0 }started_at { Time.current }reset_at { 1.day.from_now }diff --git a/plugins/discourse-ai/spec/jobs/scheduled/rollup_ai_api_request_stats_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/rollup_ai_api_request_stats_spec.rbindex 5c8e9e09521..32689f133a4 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/rollup_ai_api_request_stats_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/rollup_ai_api_request_stats_spec.rb@@ -23,6 +23,7 @@ RSpec.describe Jobs::RollupAiApiRequestStats doresponse_tokens: 50,cache_read_tokens: 10,cache_write_tokens: 5,+ estimated_cost: 0.10,created_at: old_time,)end@@ -45,6 +46,7 @@ RSpec.describe Jobs::RollupAiApiRequestStats doexpect(rolled_up.response_tokens).to eq(100)expect(rolled_up.cache_read_tokens).to eq(20)expect(rolled_up.cache_write_tokens).to eq(10)+ expect(rolled_up.estimated_cost).to eq(BigDecimal("0.2"))expect(rolled_up.usage_count).to eq(2)expect(rolled_up.created_at).to eq_time(old_time.beginning_of_day)diff --git a/plugins/discourse-ai/spec/lib/completions/report_spec.rb b/plugins/discourse-ai/spec/lib/completions/report_spec.rbindex caad64a0bba..39f1de3163b 100644--- a/plugins/discourse-ai/spec/lib/completions/report_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/report_spec.rb@@ -122,6 +122,24 @@ RSpec.describe DiscourseAi::Completions::Report doexpect(report.total_cache_write_spending).to eq(0.01875)end+ it "uses stored estimated cost when present" do+ AiApiRequestStat.create!(+ provider_id: AiApiAuditLog::Provider::Anthropic,+ user_id: user.id,+ llm_id: claude_model.id,+ language_model: "claude-3-opus",+ request_tokens: 1000,+ response_tokens: 500,+ estimated_cost: 1.23,+ created_at: 1.day.ago,+ )++ report = described_class.new(start_date: 2.days.ago, end_date: Time.current)++ expect(report.total_spending).to eq(1.23)+ expect(report.model_breakdown.first.total_spending).to eq(BigDecimal("1.23"))+ end+it "handles logs without cache tokens" doAiApiRequestStat.create!(provider_id: AiApiAuditLog::Provider::Anthropic,diff --git a/plugins/discourse-ai/spec/models/ai_api_request_stat_spec.rb b/plugins/discourse-ai/spec/models/ai_api_request_stat_spec.rbindex ad4a821e5f6..de901fe0ec6 100644--- a/plugins/discourse-ai/spec/models/ai_api_request_stat_spec.rb+++ b/plugins/discourse-ai/spec/models/ai_api_request_stat_spec.rb@@ -14,6 +14,7 @@ describe AiApiRequestStat dofeature_name: "test",request_tokens: 10,response_tokens: 20,+ estimated_cost: 0.42,created_at: 1.hour.ago,)end@@ -27,6 +28,7 @@ describe AiApiRequestStat doexpect(stat.bucket_date).to eq_time(log.created_at)expect(stat.rolled_up).to eq(false)expect(stat.usage_count).to eq(1)+ expect(stat.estimated_cost).to eq(BigDecimal("0.42"))endend@@ -46,6 +48,7 @@ describe AiApiRequestStat dousage_count: 1,request_tokens: 10,response_tokens: 10,+ estimated_cost: 0.10,)_stat2 =AiApiRequestStat.create!(@@ -57,6 +60,7 @@ describe AiApiRequestStat dousage_count: 1,request_tokens: 20,response_tokens: 20,+ estimated_cost: 0.20,)# Old data - Day 2@@ -71,6 +75,7 @@ describe AiApiRequestStat dousage_count: 1,request_tokens: 5,response_tokens: 5,+ estimated_cost: 0.05,)# Recent data - should not be rolled up@@ -99,14 +104,53 @@ describe AiApiRequestStat doexpect(day1_rollup.rolled_up).to eq(true)expect(day1_rollup.usage_count).to eq(2)expect(day1_rollup.request_tokens).to eq(30)+ expect(day1_rollup.estimated_cost).to eq(BigDecimal("0.3"))day2_rollup = described_class.find_by(bucket_date: day2.beginning_of_day)expect(day2_rollup.rolled_up).to eq(true)expect(day2_rollup.usage_count).to eq(1)expect(day2_rollup.request_tokens).to eq(5)+ expect(day2_rollup.estimated_cost).to eq(BigDecimal("0.05"))recent = described_class.find_by(rolled_up: false)expect(recent.bucket_date).to eq_time(recent_time)+ expect(recent.estimated_cost).to be_nil+ end++ it "fills missing estimated costs during rollup when model costs are configured" do+ llm_model = Fabricate(:llm_model, input_cost: 1.0, output_cost: 2.0)+ day = 3.days.ago.beginning_of_day++ described_class.create!(+ bucket_date: day + 1.hour,+ created_at: day + 1.hour,+ llm_id: llm_model.id,+ language_model: llm_model.name,+ provider_id: 1,+ feature_name: "test",+ usage_count: 1,+ request_tokens: 100_000,+ response_tokens: 100_000,+ estimated_cost: nil,+ )+ described_class.create!(+ bucket_date: day + 2.hours,+ created_at: day + 2.hours,+ llm_id: llm_model.id,+ language_model: llm_model.name,+ provider_id: 1,+ feature_name: "test",+ usage_count: 1,+ request_tokens: 100_000,+ response_tokens: 100_000,+ estimated_cost: 0.50,+ )++ described_class.rollup!++ rollup = described_class.find_by(bucket_date: day.beginning_of_day, rolled_up: true)+ expect(rollup.usage_count).to eq(2)+ expect(rollup.estimated_cost).to eq(BigDecimal("0.8"))endendenddiff --git a/plugins/discourse-ai/spec/models/llm_model_spec.rb b/plugins/discourse-ai/spec/models/llm_model_spec.rbindex 3cc2e82982c..20e507a8af4 100644--- a/plugins/discourse-ai/spec/models/llm_model_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_model_spec.rb@@ -57,6 +57,49 @@ RSpec.describe LlmModel doendend+ describe "#estimated_cost_for_tokens" do+ it "calculates request, response, cache read, and cache write cost" do+ model =+ Fabricate.build(+ :llm_model,+ input_cost: 3.0,+ output_cost: 15.0,+ cached_input_cost: 0.3,+ cache_write_cost: 3.75,+ )++ cost =+ model.estimated_cost_for_tokens(+ request_tokens: 1_000_000,+ response_tokens: 100_000,+ cache_read_tokens: 10_000,+ cache_write_tokens: 1_000,+ )++ expect(cost).to eq(BigDecimal("4.50675"))+ end++ it "returns nil when no costs are configured" do+ model =+ Fabricate.build(+ :llm_model,+ input_cost: nil,+ output_cost: nil,+ cached_input_cost: nil,+ cache_write_cost: 0,+ )++ expect(+ model.estimated_cost_for_tokens(+ request_tokens: 1_000_000,+ response_tokens: 100_000,+ cache_read_tokens: 10_000,+ cache_write_tokens: 1_000,+ ),+ ).to be_nil+ end+ end+describe "allowed_attachment_types" doit "normalizes markdown attachments to md" domodel = Fabricate.build(:llm_model)diff --git a/plugins/discourse-ai/spec/models/llm_quota_spec.rb b/plugins/discourse-ai/spec/models/llm_quota_spec.rbindex 1a6db3579d5..3e41d160337 100644--- a/plugins/discourse-ai/spec/models/llm_quota_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_quota_spec.rb@@ -49,6 +49,23 @@ RSpec.describe LlmQuota do)end+ it "raises error when usage exceeds cost limit" do+ quota =+ Fabricate(+ :llm_quota,+ group: group,+ llm_model: llm_model,+ max_tokens: nil,+ max_usages: nil,+ max_cost: 0.01,+ )+ _usage = Fabricate(:llm_quota_usage, user: user, llm_quota: quota, cost_used: 0.02)++ expect { described_class.check_quotas!(llm_model, user) }.to raise_error(+ LlmQuotaUsage::QuotaExceededError,+ )+ end+it "checks all quotas from user's groups" dogroup2 = Fabricate(:group)group2.add(user)@@ -56,11 +73,11 @@ RSpec.describe LlmQuota doquota1 = Fabricate(:llm_quota, group: group, llm_model: llm_model, max_tokens: 1000)quota2 = Fabricate(:llm_quota, group: group2, llm_model: llm_model, max_tokens: 500)- described_class.log_usage(llm_model, user, 900, 0) # Should create usages for both quotas+ described_class.log_usage(llm_model, user, request_tokens: 900, response_tokens: 0) # Should create usages for both quotasexpect { described_class.check_quotas!(llm_model, user) }.not_to raise_error- described_class.log_usage(llm_model, user, 101, 0) # This should push quota2 over its limit+ described_class.log_usage(llm_model, user, request_tokens: 101, response_tokens: 0) # This should push quota2 over its limitexpect { described_class.check_quotas!(llm_model, user) }.to raise_error(LlmQuotaUsage::QuotaExceededError,@@ -74,19 +91,17 @@ RSpec.describe LlmQuota dodescribe ".log_usage" doit "does nothing when user is nil" do- expect { described_class.log_usage(llm_model, nil, 100, 50) }.not_to change(- LlmQuotaUsage,- :count,- )+ expect {+ described_class.log_usage(llm_model, nil, request_tokens: 100, response_tokens: 50)+ }.not_to change(LlmQuotaUsage, :count)endit "creates usage records when none exist" do_quota = Fabricate(:llm_quota, group: group, llm_model: llm_model)- expect { described_class.log_usage(llm_model, user, 100, 50) }.to change(- LlmQuotaUsage,- :count,- ).by(1)+ expect {+ described_class.log_usage(llm_model, user, request_tokens: 100, response_tokens: 50)+ }.to change(LlmQuotaUsage, :count).by(1)usage = LlmQuotaUsage.lastexpect(usage.input_tokens_used).to eq(100)@@ -94,6 +109,25 @@ RSpec.describe LlmQuota doexpect(usage.usages).to eq(1)end+ it "records cache tokens and estimated cost" do+ llm_model.update!(cache_write_cost: 5.0)+ _quota = Fabricate(:llm_quota, group: group, llm_model: llm_model)++ described_class.log_usage(+ llm_model,+ user,+ request_tokens: 100,+ response_tokens: 50,+ cache_read_tokens: 200,+ cache_write_tokens: 300,+ )++ usage = LlmQuotaUsage.last+ expect(usage.cache_read_tokens_used).to eq(200)+ expect(usage.cache_write_tokens_used).to eq(300)+ expect(usage.cost_used).to eq(BigDecimal("0.005"))+ end+it "updates existing usage records" doquota = Fabricate(:llm_quota, group: group, llm_model: llm_model)usage =@@ -106,7 +140,7 @@ RSpec.describe LlmQuota dousages: 1,)- described_class.log_usage(llm_model, user, 50, 25)+ described_class.log_usage(llm_model, user, request_tokens: 50, response_tokens: 25)usage.reloadexpect(usage.input_tokens_used).to eq(150)@@ -121,10 +155,9 @@ RSpec.describe LlmQuota do_quota1 = Fabricate(:llm_quota, group: group, llm_model: llm_model)_quota2 = Fabricate(:llm_quota, group: group2, llm_model: llm_model)- expect { described_class.log_usage(llm_model, user, 100, 50) }.to change(- LlmQuotaUsage,- :count,- ).by(2)+ expect {+ described_class.log_usage(llm_model, user, request_tokens: 100, response_tokens: 50)+ }.to change(LlmQuotaUsage, :count).by(2)expect(LlmQuotaUsage.where(user: user).count).to eq(2)enddiff --git a/plugins/discourse-ai/spec/models/llm_quota_usage_spec.rb b/plugins/discourse-ai/spec/models/llm_quota_usage_spec.rbindex 2516c0cc41c..633c40f6976 100644--- a/plugins/discourse-ai/spec/models/llm_quota_usage_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_quota_usage_spec.rb@@ -29,6 +29,9 @@ RSpec.describe LlmQuotaUsage doexpect(usage.reset_at).to eq_time(Time.current + llm_quota.duration_seconds.seconds)expect(usage.input_tokens_used).to eq(0)expect(usage.output_tokens_used).to eq(0)+ expect(usage.cache_read_tokens_used).to eq(0)+ expect(usage.cache_write_tokens_used).to eq(0)+ expect(usage.cost_used).to eq(0)expect(usage.usages).to eq(0)end@@ -39,6 +42,19 @@ RSpec.describe LlmQuotaUsage doexpect(usage.id).to eq(existing.id)end++ it "handles concurrent creation correctly" do+ threads = []++ 5.times do+ threads << Thread.new do+ described_class.find_or_create_for(user: user, llm_quota: llm_quota)+ end+ end+ threads.each(&:join)++ expect(described_class.where(user: user, llm_quota: llm_quota).count).to eq(1)+ endenddescribe "#reset_if_needed!" do@@ -48,6 +64,9 @@ RSpec.describe LlmQuotaUsage dousage.update!(input_tokens_used: 100,output_tokens_used: 200,+ cache_read_tokens_used: 300,+ cache_write_tokens_used: 400,+ cost_used: 0.12,usages: 5,reset_at: 1.minute.ago,)@@ -58,6 +77,9 @@ RSpec.describe LlmQuotaUsage doexpect(usage.reload.input_tokens_used).to eq(0)expect(usage.output_tokens_used).to eq(0)+ expect(usage.cache_read_tokens_used).to eq(0)+ expect(usage.cache_write_tokens_used).to eq(0)+ expect(usage.cost_used).to eq(0)expect(usage.usages).to eq(0)expect(usage.started_at).to eq_time(Time.current)expect(usage.reset_at).to eq_time(Time.current + llm_quota.duration_seconds.seconds)@@ -69,6 +91,9 @@ RSpec.describe LlmQuotaUsage dooriginal_values = {input_tokens_used: 100,output_tokens_used: 200,+ cache_read_tokens_used: 300,+ cache_write_tokens_used: 400,+ cost_used: 0.12,usages: 5,reset_at: 1.minute.from_now,}@@ -79,6 +104,9 @@ RSpec.describe LlmQuotaUsage dousage.reloadexpect(usage.input_tokens_used).to eq(original_values[:input_tokens_used])expect(usage.output_tokens_used).to eq(original_values[:output_tokens_used])+ expect(usage.cache_read_tokens_used).to eq(original_values[:cache_read_tokens_used])+ expect(usage.cache_write_tokens_used).to eq(original_values[:cache_write_tokens_used])+ expect(usage.cost_used).to eq(original_values[:cost_used])expect(usage.usages).to eq(original_values[:usages])expect(usage.reset_at).to eq_time(original_values[:reset_at])end@@ -88,10 +116,19 @@ RSpec.describe LlmQuotaUsage dolet(:usage) { Fabricate(:llm_quota_usage, user: user, llm_quota: llm_quota) }it "increments usage counts" do- usage.increment_usage!(input_tokens: 50, output_tokens: 30)+ usage.increment_usage!(+ input_tokens: 50,+ output_tokens: 30,+ cache_read_tokens: 20,+ cache_write_tokens: 10,+ cost: 0.123,+ )expect(usage.reload.input_tokens_used).to eq(50)expect(usage.output_tokens_used).to eq(30)+ expect(usage.cache_read_tokens_used).to eq(20)+ expect(usage.cache_write_tokens_used).to eq(10)+ expect(usage.cost_used).to eq(BigDecimal("0.123"))expect(usage.usages).to eq(1)end@@ -103,10 +140,39 @@ RSpec.describe LlmQuotaUsage doexpect(usage.usages).to eq(2)end+ it "handles concurrent increments correctly" do+ threads = []++ 5.times do+ threads << Thread.new do+ described_class.find(usage.id).increment_usage!(+ input_tokens: 10,+ output_tokens: 20,+ cache_read_tokens: 30,+ cache_write_tokens: 40,+ cost: 0.50,+ )+ end+ end++ threads.each(&:join)++ usage.reload+ expect(usage.input_tokens_used).to eq(50)+ expect(usage.output_tokens_used).to eq(100)+ expect(usage.cache_read_tokens_used).to eq(150)+ expect(usage.cache_write_tokens_used).to eq(200)+ expect(usage.cost_used).to eq(BigDecimal("2.5"))+ expect(usage.usages).to eq(5)+ end+it "resets counts if needed before incrementing" dousage.update!(input_tokens_used: 100,output_tokens_used: 200,+ cache_read_tokens_used: 300,+ cache_write_tokens_used: 400,+ cost_used: 0.12,usages: 5,reset_at: 1.minute.ago,)@@ -115,6 +181,9 @@ RSpec.describe LlmQuotaUsage doexpect(usage.reload.input_tokens_used).to eq(50)expect(usage.output_tokens_used).to eq(30)+ expect(usage.cache_read_tokens_used).to eq(0)+ expect(usage.cache_write_tokens_used).to eq(0)+ expect(usage.cost_used).to eq(0)expect(usage.usages).to eq(1)endend@@ -138,6 +207,13 @@ RSpec.describe LlmQuotaUsage doexpect { usage.check_quota! }.to raise_error(LlmQuotaUsage::QuotaExceededError, /exceeded/)end+ it "raises error when max_cost exceeded" do+ usage.llm_quota.update!(max_cost: 0.01)+ usage.update!(cost_used: 0.02)++ expect { usage.check_quota! }.to raise_error(LlmQuotaUsage::QuotaExceededError, /exceeded/)+ end+it "resets quota if needed before checking" dousage.update!(input_tokens_used: llm_quota.max_tokens + 1, reset_at: 1.minute.ago)@@ -163,6 +239,12 @@ RSpec.describe LlmQuotaUsage doexpect(usage.quota_exceeded?).to be trueend+ it "returns true when max_cost exceeded" do+ usage.llm_quota.update!(max_cost: 0.01)+ usage.update!(cost_used: 0.02)+ expect(usage.quota_exceeded?).to be true+ end+it "returns false when quota is nil" dotokens = llm_quota.max_tokens + 1usage.llm_quota.update!(max_tokens: nil)@@ -215,6 +297,25 @@ RSpec.describe LlmQuotaUsage doendend+ describe "#remaining_cost" do+ it "calculates remaining cost when under limit" do+ usage.llm_quota.update!(max_cost: 1.25)+ usage.update!(cost_used: 0.25)+ expect(usage.remaining_cost).to eq(BigDecimal("1.0"))+ end++ it "returns 0 when over limit" do+ usage.llm_quota.update!(max_cost: 1.25)+ usage.update!(cost_used: 1.50)+ expect(usage.remaining_cost).to eq(0)+ end++ it "returns nil when no max_cost set" do+ usage.llm_quota.update!(max_cost: nil)+ expect(usage.remaining_cost).to be_nil+ end+ end+describe "#percentage_tokens_used" doit "calculates percentage correctly" dousage.update!(input_tokens_used: 250, output_tokens_used: 250)@@ -248,5 +349,24 @@ RSpec.describe LlmQuotaUsage doexpect(usage.percentage_usages_used).to eq(0)endend++ describe "#percentage_cost_used" do+ it "calculates percentage correctly" do+ usage.llm_quota.update!(max_cost: 1.00)+ usage.update!(cost_used: 0.25)+ expect(usage.percentage_cost_used).to eq(25)+ end++ it "caps at 100%" do+ usage.llm_quota.update!(max_cost: 1.00)+ usage.update!(cost_used: 2.00)+ expect(usage.percentage_cost_used).to eq(100)+ end++ it "returns 0 when no max_cost set" do+ usage.llm_quota.update!(max_cost: nil)+ expect(usage.percentage_cost_used).to eq(0)+ end+ endendenddiff --git a/plugins/discourse-ai/spec/requests/admin/ai_llm_quotas_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llm_quotas_controller_spec.rbindex ec7691d9f90..1f5b80b1355 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_llm_quotas_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_llm_quotas_controller_spec.rb@@ -34,6 +34,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmQuotasController dollm_model_id: llm_model.id,max_tokens: 1000,max_usages: 100,+ max_cost: "1.25",duration_seconds: 1.day.to_i,},}@@ -48,6 +49,9 @@ RSpec.describe DiscourseAi::Admin::AiLlmQuotasController doquota = LlmQuota.lastexpect(quota.group_id).to eq(group.id)expect(quota.max_tokens).to eq(1000)+ expect(quota.max_cost).to eq(BigDecimal("1.25"))+ serialized_quota = response.parsed_body["llm_quota"] || response.parsed_body+ expect(serialized_quota["max_cost"]).to eq("1.25")endit "fails with invalid params" do@@ -69,11 +73,13 @@ RSpec.describe DiscourseAi::Admin::AiLlmQuotasController doparams: {quota: {max_tokens: 2000,+ max_cost: "2.50",},}expect(response.status).to eq(200)expect(quota.reload.max_tokens).to eq(2000)+ expect(quota.max_cost).to eq(BigDecimal("2.5"))endit "fails with invalid params" dodiff --git a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rbindex 5146ea3f84e..69cdfb7a020 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb@@ -154,7 +154,15 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController docontext "with quotas" dolet(:group) { Fabricate(:group) }let(:quota_params) do- [{ group_id: group.id, max_tokens: 1000, max_usages: 10, duration_seconds: 86_400 }]+ [+ {+ group_id: group.id,+ max_tokens: 1000,+ max_usages: 10,+ max_cost: "1.25",+ duration_seconds: 86_400,+ },+ ]endit "creates model with quotas" do@@ -168,6 +176,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doexpect(created_model.llm_quotas.count).to eq(1)quota = created_model.llm_quotas.firstexpect(quota.max_tokens).to eq(1000)+ expect(quota.max_cost).to eq(BigDecimal("1.25"))expect(quota.group_id).to eq(group.id)endend@@ -483,12 +492,14 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController dogroup_id: group1.id,max_tokens: 1500,max_usages: 15,+ max_cost: "1.50",duration_seconds: 43_200,},{group_id: group3.id,max_tokens: 3000,max_usages: 30,+ max_cost: "3.00",duration_seconds: 86_400,},],@@ -503,6 +514,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doupdated_quota1 = llm_model.llm_quotas.find_by(group: group1)expect(updated_quota1.max_tokens).to eq(1500)expect(updated_quota1.max_usages).to eq(15)+ expect(updated_quota1.max_cost).to eq(BigDecimal("1.5"))expect(updated_quota1.duration_seconds).to eq(43_200)expect(llm_model.llm_quotas.find_by(group: group2)).to be_nil@@ -511,8 +523,79 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doexpect(new_quota).to be_presentexpect(new_quota.max_tokens).to eq(3000)expect(new_quota.max_usages).to eq(30)+ expect(new_quota.max_cost).to eq(BigDecimal("3.0"))expect(new_quota.duration_seconds).to eq(86_400)end++ it "returns validation errors for invalid quota limits" do+ group1 = Fabricate(:group)+ group2 = Fabricate(:group)+ quota =+ Fabricate(+ :llm_quota,+ llm_model: llm_model,+ group: group1,+ max_tokens: 1000,+ max_usages: nil,+ max_cost: "1.50",+ duration_seconds: 86_400,+ )+ other_quota = Fabricate(:llm_quota, llm_model: llm_model, group: group2)++ put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",+ params: {+ ai_llm: {+ llm_quotas: [+ {+ group_id: group1.id,+ max_tokens: 0,+ max_usages: nil,+ max_cost: "1.50",+ duration_seconds: 86_400,+ },+ ],+ },+ }++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to include("Max tokens must be greater than 0")+ expect(quota.reload.max_tokens).to eq(1000)+ expect(LlmQuota.exists?(other_quota.id)).to eq(true)+ end++ it "clears optional quota limits when blank values are submitted" do+ group = Fabricate(:group)+ quota =+ Fabricate(+ :llm_quota,+ llm_model: llm_model,+ group: group,+ max_tokens: 1000,+ max_usages: 10,+ max_cost: "1.50",+ duration_seconds: 86_400,+ )++ put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",+ params: {+ ai_llm: {+ llm_quotas: [+ {+ group_id: group.id,+ max_tokens: "",+ max_usages: "",+ max_cost: "2.50",+ duration_seconds: 86_400,+ },+ ],+ },+ }++ expect(response.status).to eq(200)+ expect(quota.reload.max_tokens).to be_nil+ expect(quota.max_usages).to be_nil+ expect(quota.max_cost).to eq(BigDecimal("2.5"))+ endendit "updates the model" dodiff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rbindex 33555c1a1a0..6242814d25f 100644--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb@@ -150,21 +150,15 @@ RSpec.describe "Managing LLM configurations" doit "prefills the quotas form" dovisit "/admin/plugins/discourse-ai/ai-llms/#{llm_model_1.id}/edit"- expect(page).to have_selector(- ".ai-llm-quotas__table .ai-llm-quotas__cell",- text: group_1.name,- )+ expect(page).to have_selector(".ai-llm-quotas__item", text: group_1.name)endit "can remove a quota" dovisit "/admin/plugins/discourse-ai/ai-llms/#{llm_model_1.id}/edit"- find(".ai-llm-quotas__delete-btn:nth-child(1)").click+ find(".ai-llm-quotas__delete-btn").click- expect(page).to have_no_selector(- ".ai-llm-quotas__table .ai-llm-quotas__cell",- text: group_1.name,- )+ expect(page).to have_no_selector(".ai-llm-quotas__item", text: group_1.name)endit "can add a quota" do@@ -177,10 +171,7 @@ RSpec.describe "Managing LLM configurations" doform.field("max_tokens").fill_in(2000)form.submit- expect(page).to have_selector(- ".ai-llm-quotas__table .ai-llm-quotas__cell",- text: Group.find(1).name,- )+ expect(page).to have_selector(".ai-llm-quotas__item", text: Group.find(1).name)endend