Class: LlmCostTracker::Budget

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/budget.rb

Class Method Summary collapse

Class Method Details

.check!(event) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/llm_cost_tracker/budget.rb', line 25

def check!(event)
  config = LlmCostTracker.configuration
  return unless event.cost

  check_per_call_budget(event, config)
  budgets = check_period_budgets(config)
  totals = totals_for_check(event, config, budgets)

  budgets.each do |period, budget|
    total = totals.fetch(period)

    handle_exceeded(budget_type: period, total: total, budget: budget, last_event: event) if total >= budget
  end
end

.enforce!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/llm_cost_tracker/budget.rb', line 8

def enforce!
  config = LlmCostTracker.configuration
  return unless config.budget_exceeded_behavior == :block_requests
  return unless config.active_record?

  budgets = enforce_period_budgets(config)
  return if budgets.empty?

  totals = active_record_totals(budgets.keys, time: Time.now.utc)

  budgets.each do |period, budget|
    total = totals.fetch(period)

    handle_exceeded(budget_type: period, total: total, budget: budget) if total >= budget
  end
end