Module: LlmCostTracker::LlmApiCallMetrics

Included in:
LlmApiCall
Defined in:
lib/llm_cost_tracker/llm_api_call_metrics.rb

Instance Method Summary collapse

Instance Method Details

#average_latency_msObject



37
38
39
40
41
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 37

def average_latency_ms
  return nil unless latency_column?

  average(:latency_ms)&.to_f
end

#cost_by_modelObject



15
16
17
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 15

def cost_by_model
  group(:model).sum(:total_cost)
end

#cost_by_providerObject



19
20
21
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 19

def cost_by_provider
  group(:provider).sum(:total_cost)
end

#cost_by_tag(key, limit: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 27

def cost_by_tag(key, limit: nil)
  relation = group_by_tag(key).order(Arel.sql("COALESCE(SUM(total_cost), 0) DESC"))
  relation = relation.limit(limit) if limit

  costs = relation.sum(:total_cost).each_with_object(Hash.new(0.0)) do |(tag_value, cost), grouped|
    grouped[tag_value_label(tag_value)] += cost.to_f
  end
  costs.sort_by { |_label, cost| -cost }.to_h
end

#group_by_tag(key) ⇒ Object



23
24
25
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 23

def group_by_tag(key)
  group(Arel.sql(tag_value_expression(key)))
end

#latency_by_modelObject



43
44
45
46
47
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 43

def latency_by_model
  return {} unless latency_column?

  group(:model).average(:latency_ms).transform_values(&:to_f)
end

#latency_by_providerObject



49
50
51
52
53
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 49

def latency_by_provider
  return {} unless latency_column?

  group(:provider).average(:latency_ms).transform_values(&:to_f)
end

#tag_value_expression(key, table_name: quoted_table_name) ⇒ Object



59
60
61
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 59

def tag_value_expression(key, table_name: quoted_table_name)
  TagSql.value_expression(self, key, table_name: table_name)
end

#tag_value_label(value) ⇒ Object



55
56
57
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 55

def tag_value_label(value)
  TagSql.value_label(value)
end

#total_costObject



7
8
9
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 7

def total_cost
  sum(:total_cost).to_f
end

#total_tokensObject



11
12
13
# File 'lib/llm_cost_tracker/llm_api_call_metrics.rb', line 11

def total_tokens
  sum(:total_tokens).to_i
end