Skip to content

A platform for community discussion. Free, open, simple.

FIX: Feature credit cost not always working (#36111)

Keegan Georgeac35c2815359
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
index 3809be0b79f..2e74eb17def 100644
--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb
+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
@@ -113,9 +113,15 @@ class LlmCreditAllocation < ActiveRecord::Base
allocation.credits_available?
end
- def self.check_credits!(llm_model)
+ def self.check_credits!(llm_model, feature_name = nil)
return unless llm_model&.credit_system_enabled?
+ # If feature has 0 credit cost, skip the check entirely
+ if feature_name.present?
+ cost_per_token = LlmFeatureCreditCost.credit_cost_for(llm_model, feature_name)
+ return if cost_per_token.zero?
+ end
+
allocation = llm_model.llm_credit_allocation
allocation.check_credits!
end
diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rb
index ef6eed66227..6b6511925b1 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb
@@ -78,7 +78,7 @@ module DiscourseAi
&blk
)
LlmQuota.check_quotas!(@llm_model, user)
- LlmCreditAllocation.check_credits!(@llm_model)
+ LlmCreditAllocation.check_credits!(@llm_model, feature_name)
start_time = Time.now
diff --git a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
index 13191e08297..c739214ff9d 100644
--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
@@ -390,4 +390,84 @@ RSpec.describe LlmCreditAllocation do
expect(allocation.relative_reset_time).to eq("")
end
end
+
+ describe ".check_credits!" do
+ fab!(:llm_model) { Fabricate(:llm_model, id: -1) }
+
+ it "raises error when hard limit reached and no feature_name provided" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)
+
+ expect { LlmCreditAllocation.check_credits!(llm_model) }.to raise_error(
+ LlmCreditAllocation::CreditLimitExceeded,
+ )
+ end
+
+ it "does not raise error when credits available" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 500)
+
+ expect { LlmCreditAllocation.check_credits!(llm_model) }.not_to raise_error
+ end
+
+ it "bypasses credit check for features with 0 credit cost" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)
+ Fabricate(
+ :llm_feature_credit_cost,
+ llm_model: llm_model,
+ feature_name: "spam_detection",
+ credits_per_token: 0.0,
+ )
+
+ expect { LlmCreditAllocation.check_credits!(llm_model, "spam_detection") }.not_to raise_error
+ end
+
+ it "raises error for features with non-zero credit cost when limit reached" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)
+ Fabricate(
+ :llm_feature_credit_cost,
+ llm_model: llm_model,
+ feature_name: "assistant",
+ credits_per_token: 1.0,
+ )
+
+ expect { LlmCreditAllocation.check_credits!(llm_model, "assistant") }.to raise_error(
+ LlmCreditAllocation::CreditLimitExceeded,
+ )
+ end
+
+ it "uses default credit cost when feature not found" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)
+ Fabricate(
+ :llm_feature_credit_cost,
+ llm_model: llm_model,
+ feature_name: "default",
+ credits_per_token: 1.0,
+ )
+
+ expect { LlmCreditAllocation.check_credits!(llm_model, "unknown_feature") }.to raise_error(
+ LlmCreditAllocation::CreditLimitExceeded,
+ )
+ end
+
+ it "bypasses check when default credit cost is 0" do
+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)
+ Fabricate(
+ :llm_feature_credit_cost,
+ llm_model: llm_model,
+ feature_name: "default",
+ credits_per_token: 0.0,
+ )
+
+ expect { LlmCreditAllocation.check_credits!(llm_model, "unknown_feature") }.not_to raise_error
+ end
+
+ it "returns early when model has no credit system" do
+ regular_model = Fabricate(:llm_model)
+
+ expect { LlmCreditAllocation.check_credits!(regular_model) }.not_to raise_error
+ end
+
+ it "returns early when model is nil" do
+ expect { LlmCreditAllocation.check_credits!(nil) }.not_to raise_error
+ end
+ end
end