class RubyLLM::Cost
Represents the cost of token usage for a model response.
Constants
- COMPONENTS
- PER_MILLION
Attributes
Public Class Methods
Source
# File lib/ruby_llm/cost.rb, line 11 def self.aggregate(costs) costs = costs.compact.select(&:tokens?) return new(amounts: {}, has_tokens: false) if costs.empty? missing = COMPONENTS.select do |component| costs.any? { |cost| cost.missing?(component) } end amounts = COMPONENTS.to_h do |component| [component, missing.include?(component) ? nil : aggregate_component(costs, component)] end new(amounts:, missing:, has_tokens: true) end
# File lib/ruby_llm/cost.rb, line 27 def initialize(tokens: nil, model: nil, amounts: nil, missing: [], has_tokens: nil, category: :text_tokens, input_details: nil) @tokens = tokens @model = normalize_model(model) @amounts = amounts @missing = missing @has_tokens = has_tokens @category = category.to_sym @input_details = input_details end
rubocop:disable Metrics/ParameterLists
Public Instance Methods
Source
# File lib/ruby_llm/cost.rb, line 47 def cache_read amount_for(:cache_read) end
Also aliased as: cached_input
Source
# File lib/ruby_llm/cost.rb, line 51 def cache_write amount_for(:cache_write) end
Also aliased as: cache_creation
Source
# File lib/ruby_llm/cost.rb, line 39 def input amount_for(:input) end
rubocop:enable Metrics/ParameterLists
Source
# File lib/ruby_llm/cost.rb, line 91 def missing?(component) return @missing.include?(component) if aggregate? return image_input_missing? if component == :input && detailed_image_input? return false if component == :thinking && !thinking_priced_separately? tokens = tokens_for(component) tokens.to_i.positive? && price_for(component).nil? end
Source
# File lib/ruby_llm/cost.rb, line 55 def thinking amount_for(:thinking) end
Also aliased as: reasoning
Source
# File lib/ruby_llm/cost.rb, line 74 def to_h { input: input, output: output, cache_read: cache_read, cache_write: cache_write, thinking: thinking, total: total }.compact end
Source
# File lib/ruby_llm/cost.rb, line 85 def tokens? return @has_tokens unless @has_tokens.nil? COMPONENTS.any? { |component| !tokens_for(component).nil? } end
Source
# File lib/ruby_llm/cost.rb, line 64 def total return nil unless tokens? return nil if COMPONENTS.any? { |component| missing?(component) } costs = COMPONENTS.filter_map { |component| public_send(component) } return nil if costs.empty? costs.sum end