Class: LlmCostTracker::Doctor::CostDriftCheck

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

Constant Summary collapse

SAMPLE_SIZE =
200
EPSILON =
BigDecimal("0.00000001")

Instance Method Summary collapse

Instance Method Details

#callObject



14
15
16
17
18
19
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
50
51
# File 'lib/llm_cost_tracker/doctor/cost_drift_check.rb', line 14

def call
  return unless Probe.table_exists?("llm_cost_tracker_calls")
  return unless Probe.table_exists?("llm_cost_tracker_call_line_items")

  sampled = LlmCostTracker::Call
            .where.not(total_cost: nil)
            .where(cost_status: %w[complete free partial])
            .order(id: :desc)
            .limit(SAMPLE_SIZE)
            .pluck(:id, :total_cost, :cost_status)
  return Check.new(:ok, "cost drift", "no priced calls to inspect") if sampled.empty?

  line_item_totals = LlmCostTracker::CallLineItem
                     .where(llm_cost_tracker_call_id: sampled.map(&:first))
                     .group(:llm_cost_tracker_call_id)
                     .sum(:cost)

  drifted = sampled.filter_map do |id, total_cost, cost_status|
    line_total = line_item_totals[id] || BigDecimal("0")
    header = BigDecimal(total_cost.to_s)
    next if cost_status == "partial" && header >= line_total
    next if (header - line_total).abs <= EPSILON

    "##{id}: header=#{header.to_s('F')} line_items=#{line_total.to_s('F')}"
  end

  if drifted.empty?
    return Check.new(:ok, "cost drift",
                     "header total_cost matches line items in #{sampled.size} sampled calls")
  end

  Check.new(
    :warn,
    "cost drift",
    "header total_cost diverges from line items in #{drifted.size}/#{sampled.size} sampled calls: " \
    "#{drifted.first(5).join('; ')}#{'; ...' if drifted.size > 5}"
  )
end