class RubyLLM::Cost::Aggregate
An Aggregate is the combined cost of several messages, summing each component across them. Chat#cost and Cost.aggregate return Aggregate objects, which respond to the same component readers as Cost.
chat.cost.total
A component returns nil when pricing was missing for one of the messages, or when no message has a cost for that component.
Public Instance Methods
Source
# File lib/ruby_llm/cost.rb, line 170
Returns the summed cost of cache-read input tokens in US dollars, or nil when pricing was missing for a message or no message reports cache-read tokens.
Source
# File lib/ruby_llm/cost.rb, line 176
Returns the summed cost of cache-write input tokens in US dollars, or nil when pricing was missing for a message or no message reports cache-write tokens.
Source
# File lib/ruby_llm/cost.rb, line 158
Returns the summed cost of input tokens in US dollars, or nil when pricing was missing for a message or no message reports input tokens.
Source
# File lib/ruby_llm/cost.rb, line 164
Returns the summed cost of billable output tokens in US dollars, or nil when pricing was missing for a message or no message reports output tokens.
Source
# File lib/ruby_llm/cost.rb, line 186 COMPONENTS.each do |component| define_method(component) { @amounts[component] } end
Returns the summed cost of separately priced thinking tokens in US dollars, or nil when no message has a thinking cost.
Source
# File lib/ruby_llm/cost.rb, line 205 def to_h { input: input, output: output, cache_read: cache_read, cache_write: cache_write, thinking: thinking, total: total }.compact end
Returns a hash of component costs in US dollars, plus :total, omitting nil values.
Source
# File lib/ruby_llm/cost.rb, line 193 def total return nil unless tokens? return nil if @missing.any? costs = COMPONENTS.filter_map { |component| public_send(component) } return nil if costs.empty? costs.sum end
Returns the sum of all components in US dollars. Returns nil when there is no token usage, or when pricing was missing for any component.