Class: LlmCostTracker::Call
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LlmCostTracker::Call
- Defined in:
- app/models/llm_cost_tracker/call.rb
Class Method Summary collapse
- .already_recorded?(provider:, provider_response_id:) ⇒ Boolean
- .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
- .qualified(column) ⇒ Object
- .total_cost ⇒ Object
- .total_tokens ⇒ Object
Instance Method Summary collapse
Class Method Details
.already_recorded?(provider:, provider_response_id:) ⇒ Boolean
45 46 47 48 49 |
# File 'app/models/llm_cost_tracker/call.rb', line 45 def already_recorded?(provider:, provider_response_id:) return false if provider_response_id.to_s.empty? where(provider: provider, provider_response_id: provider_response_id).exists? end |
.average_latency_ms ⇒ Object
83 |
# File 'app/models/llm_cost_tracker/call.rb', line 83 def average_latency_ms = average(:latency_ms)&.to_f |
.by_tag(key, value) ⇒ Object
51 |
# File 'app/models/llm_cost_tracker/call.rb', line 51 def by_tag(key, value) = (key => value) |
.by_tags(tags) ⇒ Object
53 |
# File 'app/models/llm_cost_tracker/call.rb', line 53 def () = Ledger::Tags::Query.apply() |
.cost_by_model(limit: nil) ⇒ Object
59 |
# File 'app/models/llm_cost_tracker/call.rb', line 59 def cost_by_model(limit: nil) = cost_by_column(:model, limit: limit) |
.cost_by_provider(limit: nil) ⇒ Object
61 |
# File 'app/models/llm_cost_tracker/call.rb', line 61 def cost_by_provider(limit: nil) = cost_by_column(:provider, limit: limit) |
.cost_by_tag(key, limit: nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/llm_cost_tracker/call.rb', line 67 def cost_by_tag(key, limit: nil) cost = qualified(:total_cost) label = Ledger::Tags::Breakdown.label_sql(connection) raw_value = Ledger::Tags::Breakdown.raw_value_sql(connection) relation = Ledger::Tags::Breakdown.join_relation(self, key) .select("#{label} AS name", "COALESCE(SUM(#{cost}), 0) AS total_cost") .group(Arel.sql(label)) .order( Arel.sql("COALESCE(SUM(#{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
93 94 95 96 97 |
# File 'app/models/llm_cost_tracker/call.rb', line 93 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
89 90 91 |
# File 'app/models/llm_cost_tracker/call.rb', line 89 def group_by_period(period, column: :tracked_at) group(Arel.sql(period_group_expression(period, column: column))) end |
.group_by_tag(key) ⇒ Object
63 64 65 |
# File 'app/models/llm_cost_tracker/call.rb', line 63 def group_by_tag(key) Ledger::Tags::Breakdown.join_relation(self, key).group(Ledger::Tags::Breakdown.value_arel) end |
.latency_by_model ⇒ Object
85 |
# File 'app/models/llm_cost_tracker/call.rb', line 85 def latency_by_model = group(:model).average(:latency_ms).transform_values(&:to_f) |
.latency_by_provider ⇒ Object
87 |
# File 'app/models/llm_cost_tracker/call.rb', line 87 def latency_by_provider = group(:provider).average(:latency_ms).transform_values(&:to_f) |
.qualified(column) ⇒ Object
99 100 101 |
# File 'app/models/llm_cost_tracker/call.rb', line 99 def qualified(column) "#{quoted_table_name}.#{connection.quote_column_name(column)}" end |
.total_cost ⇒ Object
55 |
# File 'app/models/llm_cost_tracker/call.rb', line 55 def total_cost = sum(:total_cost).to_f |
.total_tokens ⇒ Object
57 |
# File 'app/models/llm_cost_tracker/call.rb', line 57 def total_tokens = sum(:total_tokens).to_i |
Instance Method Details
#tag_pairs ⇒ Object
125 126 127 128 129 |
# File 'app/models/llm_cost_tracker/call.rb', line 125 def tag_pairs tag_records.to_h do |record| [record.key, record.value] end end |