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
|
# File 'lib/llm_cost_tracker/doctor/pricing_snapshot_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_ids = LlmCostTracker::Call
.where.not(pricing_snapshot: nil)
.where(cost_status: %w[complete free])
.order(id: :desc)
.limit(SAMPLE_SIZE)
.pluck(:id)
return Check.new(:ok, "pricing snapshot drift", "no snapshotted calls to inspect") if sampled_ids.empty?
calls_by_id = LlmCostTracker::Call.where(id: sampled_ids).index_by(&:id)
line_items_by_call = LlmCostTracker::CallLineItem
.where(llm_cost_tracker_call_id: sampled_ids, unit: "token")
.group_by(&:llm_cost_tracker_call_id)
drifted = sampled_ids.flat_map do |id|
call = calls_by_id[id]
rates = rates_for(call.pricing_snapshot)
next [] if rates.nil? || rates.empty?
(line_items_by_call[id] || []).filter_map { |item| drift_message_for(item, rates, call_id: id) }
end
return ok_check(sampled_ids.size) if drifted.empty?
Check.new(
:warn,
"pricing snapshot drift",
"line item cost diverges from pricing_snapshot rate in #{drifted.size} cases across " \
"#{sampled_ids.size} sampled calls: #{drifted.first(5).join('; ')}#{'; ...' if drifted.size > 5}"
)
end
|