Skip to content

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

DEV: AI credits should reset at start of month (#35587)

Keegan George5ef7fcc21d2b
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
index 779e3557747..2d1d1fa887e 100644
--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb
+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
@@ -65,7 +65,7 @@ class LlmCreditAllocation < ActiveRecord::Base
def next_reset_at
return nil if last_reset_at.nil?
- last_reset_at + 1.month
+ (last_reset_at + 1.month).beginning_of_month
end
def reset_if_needed!
@@ -73,7 +73,7 @@ class LlmCreditAllocation < ActiveRecord::Base
reload
return unless should_reset?
- now = Time.current
+ now = Time.current.beginning_of_month
update!(monthly_used: 0, last_reset_at: now)
end
end
@@ -145,7 +145,7 @@ class LlmCreditAllocation < ActiveRecord::Base
private
def set_last_reset_at
- self.last_reset_at ||= Time.current
+ self.last_reset_at ||= Time.current.beginning_of_month
end
def format_reset_time
diff --git a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb
index 8efb17fbe01..e73b6e814e4 100644
--- a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb
+++ b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb
@@ -4,6 +4,6 @@ Fabricator(:llm_credit_allocation) do
llm_model
monthly_credits 1_000_000
monthly_used 0
- last_reset_at { Time.current }
+ last_reset_at { Time.current.beginning_of_month }
soft_limit_percentage 80
end
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 2ba3901138b..c92eb320154 100644
--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
@@ -42,6 +42,12 @@ RSpec.describe LlmCreditAllocation do
allocation.save!
expect(allocation.last_reset_at).to be_present
end
+
+ it "sets last_reset_at to beginning of month on create" do
+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))
+ allocation = Fabricate(:llm_credit_allocation, llm_model: llm_model)
+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))
+ end
end
describe "#credits_remaining" do
@@ -219,10 +225,24 @@ RSpec.describe LlmCreditAllocation do
end
describe "#next_reset_at" do
- it "returns one month after last_reset_at" do
+ it "returns beginning of next month after last_reset_at" do
freeze_time
allocation = Fabricate(:llm_credit_allocation, last_reset_at: Time.current)
- expect(allocation.next_reset_at).to eq_time(1.month.from_now)
+ expect(allocation.next_reset_at).to eq_time((1.month.from_now).beginning_of_month)
+ end
+
+ it "returns first of next month when created mid-month" do
+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))
+ allocation = Fabricate(:llm_credit_allocation)
+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))
+ expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-11-01 00:00:00"))
+ end
+
+ it "returns first of next month when created on first of month" do
+ freeze_time(Time.zone.parse("2025-11-01 00:00:00"))
+ allocation = Fabricate(:llm_credit_allocation)
+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-11-01 00:00:00"))
+ expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-12-01 00:00:00"))
end
end
@@ -253,7 +273,7 @@ RSpec.describe LlmCreditAllocation do
allocation.reload
expect(allocation.monthly_used).to eq(0)
- expect(allocation.last_reset_at).to be_within(1.second).of(Time.current)
+ expect(allocation.last_reset_at).to eq_time(Time.current.beginning_of_month)
end
end
@@ -273,6 +293,23 @@ RSpec.describe LlmCreditAllocation do
expect(allocation.monthly_used).to eq(800)
expect(allocation.last_reset_at).to be_within(1.second).of(original_time)
end
+
+ it "resets to beginning of current month when triggered mid-month" do
+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))
+ allocation =
+ Fabricate(
+ :llm_credit_allocation,
+ monthly_credits: 1000,
+ monthly_used: 800,
+ last_reset_at: Time.zone.parse("2025-09-01 00:00:00"),
+ )
+
+ allocation.reset_if_needed!
+
+ allocation.reload
+ expect(allocation.monthly_used).to eq(0)
+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))
+ end
end
describe "#deduct_credits!" do