Class: RubyLLM::ActiveRecord::Usage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ruby_llm/active_record/usage.rb

Overview

RubyLLM's private normalized persistence for one provider attempt.

Instance Method Summary collapse

Instance Method Details

#costObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_llm/active_record/usage.rb', line 28

def cost
  recorded = {
    input: input_cost,
    output: output_cost,
    cache_read: cache_read_cost,
    cache_write: cache_write_cost,
    thinking: thinking_cost,
    total: total_cost
  }.compact
  RubyLLM::Cost.from_h(recorded, tokens: tokens)
end

#cost_available?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ruby_llm/active_record/usage.rb', line 44

def cost_available?
  !total_cost.nil?
end

#to_entryObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/active_record/usage.rb', line 48

def to_entry
  ::RubyLLM::Accounting::Usage::Entry.new(
    operation: operation,
    provider: provider,
    model: model,
    status: status,
    tokens: tokens,
    cost: cost,
    message: message
  )
end

#tokensObject



18
19
20
21
22
23
24
25
26
# File 'lib/ruby_llm/active_record/usage.rb', line 18

def tokens
  RubyLLM::Tokens.new(
    input: input_tokens,
    output: output_tokens,
    cache_read: cache_read_tokens,
    cache_write: cache_write_tokens,
    thinking: thinking_tokens
  )
end

#usage_available?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ruby_llm/active_record/usage.rb', line 40

def usage_available?
  tokens.to_h.any?
end