Class: LlmCostTracker::Storage::ActiveRecordStore

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/storage/active_record_store.rb

Class Method Summary collapse

Class Method Details

.model_classObject



36
37
38
# File 'lib/llm_cost_tracker/storage/active_record_store.rb', line 36

def model_class
  LlmCostTracker::LlmApiCall
end

.monthly_total(time: Time.now.utc) ⇒ Object



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

def monthly_total(time: Time.now.utc)
  beginning_of_month = Time.new(time.year, time.month, 1, 0, 0, 0, "+00:00")

  model_class
    .where(tracked_at: beginning_of_month..time)
    .sum(:total_cost)
    .to_f
end

.save(event) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/llm_cost_tracker/storage/active_record_store.rb', line 7

def save(event)
  tags = stringify_tags(event.tags || {})

  attributes = {
    provider:      event.provider,
    model:         event.model,
    input_tokens:  event.input_tokens,
    output_tokens: event.output_tokens,
    total_tokens:  event.total_tokens,
    input_cost:    event.cost&.input_cost,
    output_cost:   event.cost&.output_cost,
    total_cost:    event.cost&.total_cost,
    tags:          tags_for_storage(tags),
    tracked_at:    event.tracked_at
  }
  attributes[:latency_ms] = event.latency_ms if model_class.latency_column?

  model_class.create!(attributes)
end