Module: LlmCostTracker::Charges::CostStatus

Defined in:
lib/llm_cost_tracker/charges/cost_status.rb

Constant Summary collapse

COMPLETE =
"complete"
FREE =
"free"
PARTIAL =
"partial"
UNKNOWN =
"unknown"
INCOMPLETE =
[UNKNOWN, PARTIAL].freeze

Class Method Summary collapse

Class Method Details

.call(token_usage:, usage_source:, token_cost:, service_line_items:, total_cost:, token_pricing_partial: false) ⇒ Object

rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llm_cost_tracker/charges/cost_status.rb', line 20

def self.call(token_usage:,
              usage_source:,
              token_cost:,
              service_line_items:,
              total_cost:,
              token_pricing_partial: false)
  return UNKNOWN if usage_source == Usage::Source::UNKNOWN

  token_billable = token_usage.priced_quantities.any? { |_key, quantity| quantity.positive? }
  service_billable = false
  service_priced = false
  service_unpriced = false
  service_line_items.each do |line_item|
    next unless line_item.billable?

    service_billable = true
    service_priced ||= line_item.priced?
    service_unpriced ||= line_item.unpriced?
    break if service_priced && service_unpriced
  end

  priced = (token_billable && !token_cost.nil?) || service_priced || (!token_billable && !service_billable)
  unpriced = (token_billable && (token_cost.nil? || token_pricing_partial)) || service_unpriced
  return UNKNOWN if unpriced && !priced
  return PARTIAL if unpriced

  return UNKNOWN if total_cost.nil?

  total_cost.zero? ? FREE : COMPLETE
end

.unknown_pricing_sql(total_cost: "total_cost", cost_status: "cost_status") ⇒ Object



14
15
16
17
# File 'lib/llm_cost_tracker/charges/cost_status.rb', line 14

def self.unknown_pricing_sql(total_cost: "total_cost", cost_status: "cost_status")
  statuses = INCOMPLETE.map { |status| ActiveRecord::Base.connection.quote(status) }.join(", ")
  "#{total_cost} IS NULL OR #{cost_status} IN (#{statuses})"
end