class RubyLLM::Cost
A Cost prices token usage in US dollars using pricing from the model registry. Usage entries own accounting events; Message and Chat aggregate calls, and Model#cost_for prices any token usage against a specific model.
response = chat.ask "Summarize Ruby's object model." response.cost.total cost = model.cost_for(response.tokens) cost.input cost.output
The components are RubyLLM’s normalized token buckets: input, output, cache_read, cache_write, and thinking. When the registry lacks pricing for tokens that were used, the affected component and total return nil instead of a false zero.
When the provider reports the exact cost of a call, total returns the reported amount instead of a registry-price estimate, even when registry pricing is missing.
Costs are computed when the object is built. Readers do not recalculate them when registry prices change. ::aggregate and ::from_h return the same class, so a single call, a whole chat, and a stored breakdown all read the same way.
Public Class Methods
# File lib/ruby_llm/cost.rb, line 44 def aggregate(costs, complete: true) costs = costs.compact.select(&:tokens?) missing = COMPONENTS.select do |component| costs.any? { |cost| cost.missing?(component) } end amounts = COMPONENTS.to_h do |component| values = costs.filter_map { |cost| cost.public_send(component) } [component, missing.include?(component) || values.empty? ? nil : values.sum] end new(amounts:, missing:, reported: costs.any?, complete:, total: aggregate_total(costs)) end
Combines several costs into one Cost that sums each component. Ignores nil entries. A component returns nil when pricing was missing for one of the calls, or when no call has a cost for that component. Pass complete: false when some requests are still running or their costs are unknown; total then remains nil.
cost = RubyLLM::Cost.aggregate(messages.map(&:cost)) cost.total
Source
# File lib/ruby_llm/cost.rb, line 66 def from_h(hash, tokens: nil) amounts = COMPONENTS.to_h { |component| [component, hash[component] || hash[component.to_s]] } total_recorded = hash.key?(:total) || hash.key?('total') total = hash[:total] || hash['total'] if total_recorded missing = missing_recorded_components(amounts, tokens, total_recorded) new(amounts:, missing:, reported: recorded_tokens?(amounts, tokens, total_recorded), total:) end
Public Instance Methods
Source
# File lib/ruby_llm/cost.rb, line 128 def cache_read @amounts[:cache_read] end
Returns the cost of cache-read input tokens in US dollars, or nil when the token count or its pricing is unavailable.
Source
# File lib/ruby_llm/cost.rb, line 134 def cache_write @amounts[:cache_write] end
Returns the cost of cache-write input tokens in US dollars, or nil when the token count or its pricing is unavailable.
Source
# File lib/ruby_llm/cost.rb, line 116 def input @amounts[:input] end
Returns the cost of input tokens in US dollars, or nil when the token count or its pricing is unavailable.
Source
# File lib/ruby_llm/cost.rb, line 122 def output @amounts[:output] end
Returns the cost of billable output tokens in US dollars, or nil when the token count or its pricing is unavailable.
Source
# File lib/ruby_llm/cost.rb, line 142 def thinking @amounts[:thinking] end
Returns the cost of thinking tokens in US dollars, or nil when the model does not price reasoning output separately from regular output or the token count is unavailable. When not priced separately, thinking tokens are part of output.
Source
# File lib/ruby_llm/cost.rb, line 164 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 150 def total return nil unless @complete return nil unless tokens? return @total unless @total.nil? return nil if @missing.any? amounts = @amounts.values.compact return nil if amounts.empty? amounts.sum end
Returns the sum of all components in US dollars, or the exact amount the provider reported when it reported one. Returns nil when there is no token usage, or when pricing is missing for tokens that were used and the provider reported no cost.