Class: LlmCostTracker::Call
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LlmCostTracker::Call
- Defined in:
- app/models/llm_cost_tracker/call.rb
Class Method Summary collapse
- .average_latency_ms ⇒ Object
- .by_tag(key, value) ⇒ Object
- .by_tags(tags) ⇒ Object
- .cost_by_model(limit: nil) ⇒ Object
- .cost_by_provider(limit: nil) ⇒ Object
- .cost_by_tag(key, limit: nil) ⇒ Object
- .daily_costs(days: 30) ⇒ Object
- .group_by_period(period, column: :tracked_at) ⇒ Object
- .group_by_tag(key) ⇒ Object
- .latency_by_model ⇒ Object
- .latency_by_provider ⇒ Object
- .total_cost ⇒ Object
- .total_tokens ⇒ Object
Instance Method Summary collapse
Class Method Details
.average_latency_ms ⇒ Object
95 |
# File 'app/models/llm_cost_tracker/call.rb', line 95 def average_latency_ms = average(:latency_ms)&.to_f |
.by_tag(key, value) ⇒ Object
64 |
# File 'app/models/llm_cost_tracker/call.rb', line 64 def by_tag(key, value) = (key => value) |
.by_tags(tags) ⇒ Object
66 |
# File 'app/models/llm_cost_tracker/call.rb', line 66 def () = Ledger::Tags::Query.apply() |
.cost_by_model(limit: nil) ⇒ Object
72 |
# File 'app/models/llm_cost_tracker/call.rb', line 72 def cost_by_model(limit: nil) = cost_by_column(:model, limit: limit) |
.cost_by_provider(limit: nil) ⇒ Object
74 |
# File 'app/models/llm_cost_tracker/call.rb', line 74 def cost_by_provider(limit: nil) = cost_by_column(:provider, limit: limit) |
.cost_by_tag(key, limit: nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/llm_cost_tracker/call.rb', line 80 def cost_by_tag(key, limit: nil) label = Ledger::Tags::Sql.label_sql(connection) raw_value = Ledger::Tags::Sql.raw_value_sql(connection) relation = Ledger::Tags::Sql.join_relation(self, key) .select("#{label} AS name", "COALESCE(SUM(total_cost), 0) AS total_cost") .group(Arel.sql(label)) .order( Arel.sql("COALESCE(SUM(total_cost), 0) DESC"), Arel.sql("MAX(CASE WHEN #{raw_value} IS NULL THEN 1 ELSE 0 END) ASC"), Arel.sql("#{label} DESC") ) relation = relation.limit(limit) if limit relation end |
.daily_costs(days: 30) ⇒ Object
105 106 107 108 109 |
# File 'app/models/llm_cost_tracker/call.rb', line 105 def daily_costs(days: 30) where(tracked_at: days.days.ago..) .group_by_period(:day) .sum(:total_cost) end |
.group_by_period(period, column: :tracked_at) ⇒ Object
101 102 103 |
# File 'app/models/llm_cost_tracker/call.rb', line 101 def group_by_period(period, column: :tracked_at) group(Arel.sql(period_group_expression(period, column: column))) end |
.group_by_tag(key) ⇒ Object
76 77 78 |
# File 'app/models/llm_cost_tracker/call.rb', line 76 def group_by_tag(key) Ledger::Tags::Sql.join_relation(self, key).group(Ledger::Tags::Sql.value_arel) end |
.latency_by_model ⇒ Object
97 |
# File 'app/models/llm_cost_tracker/call.rb', line 97 def latency_by_model = group(:model).average(:latency_ms).transform_values(&:to_f) |
.latency_by_provider ⇒ Object
99 |
# File 'app/models/llm_cost_tracker/call.rb', line 99 def latency_by_provider = group(:provider).average(:latency_ms).transform_values(&:to_f) |
.total_cost ⇒ Object
68 |
# File 'app/models/llm_cost_tracker/call.rb', line 68 def total_cost = sum(:total_cost).to_f |
.total_tokens ⇒ Object
70 |
# File 'app/models/llm_cost_tracker/call.rb', line 70 def total_tokens = sum(:total_tokens).to_i |
Instance Method Details
#parsed_tags ⇒ Object
157 158 159 160 161 |
# File 'app/models/llm_cost_tracker/call.rb', line 157 def tag_records.to_h do |record| [record.key, record.value] end end |