Class: LLM::Cost
- Inherits:
-
Object
- Object
- LLM::Cost
- Defined in:
- lib/llm/cost.rb
Overview
The LLM::Cost class represents an approximate cost breakdown for a provider request. It stores input, output, input audio, output audio, input image, cache read, cache write, and reasoning costs separately and can return the total.
Class Method Summary collapse
-
.from(ctx) ⇒ LLM::Cost
Build a cost breakdown from token usage and model pricing.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true when two costs hold the same breakdown.
-
#cache_read ⇒ Float
(also: #cache_read_costs)
Returns the cache read cost.
-
#cache_write ⇒ Float
(also: #cache_write_costs)
Returns the cache write cost.
- #initialize(input_costs: nil, output_costs: nil, input_audio_costs: nil, output_audio_costs: nil, input_image_costs: nil, cache_read_costs: nil, cache_write_costs: nil, reasoning_costs: nil) ⇒ LLM::Cost constructor
-
#input ⇒ Float
(also: #input_costs)
Returns the input cost.
-
#input_audio ⇒ Float
(also: #input_audio_costs)
Returns the input audio cost.
-
#input_image ⇒ Float
(also: #input_image_costs)
Returns the input image cost.
-
#output ⇒ Float
(also: #output_costs)
Returns the output cost.
-
#output_audio ⇒ Float
(also: #output_audio_costs)
Returns the output audio cost.
-
#reasoning ⇒ Float
(also: #reasoning_costs)
Returns the reasoning cost.
-
#to_h ⇒ Hash
Returns a hash with the non-nil cost components and the total.
-
#to_s ⇒ String
Returns the total cost in a human friendly format.
-
#total ⇒ Float
Returns the total cost.
Constructor Details
#initialize(input_costs: nil, output_costs: nil, input_audio_costs: nil, output_audio_costs: nil, input_image_costs: nil, cache_read_costs: nil, cache_write_costs: nil, reasoning_costs: nil) ⇒ LLM::Cost
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/llm/cost.rb', line 52 def initialize(input_costs: nil, output_costs: nil, input_audio_costs: nil, output_audio_costs: nil, input_image_costs: nil, cache_read_costs: nil, cache_write_costs: nil, reasoning_costs: nil) @input_costs = input_costs @output_costs = output_costs @input_audio_costs = input_audio_costs @output_audio_costs = output_audio_costs @input_image_costs = input_image_costs @cache_read_costs = cache_read_costs @cache_write_costs = cache_write_costs @reasoning_costs = reasoning_costs end |
Class Method Details
.from(ctx) ⇒ LLM::Cost
Build a cost breakdown from token usage and model pricing.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/llm/cost.rb', line 14 def self.from(ctx) pricing = LLM.registry_for(ctx.llm).cost(model: ctx.model) usage = ctx.usage output = usage.output_tokens - usage.reasoning_tokens input = usage.input_tokens - usage.cache_read_tokens new( input_costs: price(pricing.input, input), output_costs: price(pricing.output, output), input_audio_costs: price(pricing.input_audio, usage.input_audio_tokens), output_audio_costs: price(pricing.output_audio, usage.output_audio_tokens), input_image_costs: price(pricing.input, usage.input_image_tokens), cache_read_costs: price(pricing.cache_read, usage.cache_read_tokens), cache_write_costs: price(pricing.cache_write, usage.cache_write_tokens), reasoning_costs: price(pricing.reasoning || pricing.output, usage.reasoning_tokens) ) rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError new end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true when two costs hold the same breakdown
69 70 71 |
# File 'lib/llm/cost.rb', line 69 def ==(other) other.is_a?(LLM::Cost) and other.to_h == to_h end |
#cache_read ⇒ Float Also known as: cache_read_costs
Returns the cache read cost
117 118 119 |
# File 'lib/llm/cost.rb', line 117 def cache_read @cache_read_costs || 0.to_f end |
#cache_write ⇒ Float Also known as: cache_write_costs
Returns the cache write cost
125 126 127 |
# File 'lib/llm/cost.rb', line 125 def cache_write @cache_write_costs || 0.to_f end |
#input ⇒ Float Also known as: input_costs
Returns the input cost
77 78 79 |
# File 'lib/llm/cost.rb', line 77 def input @input_costs || 0.to_f end |
#input_audio ⇒ Float Also known as: input_audio_costs
Returns the input audio cost
93 94 95 |
# File 'lib/llm/cost.rb', line 93 def input_audio @input_audio_costs || 0.to_f end |
#input_image ⇒ Float Also known as: input_image_costs
Returns the input image cost
109 110 111 |
# File 'lib/llm/cost.rb', line 109 def input_image @input_image_costs || 0.to_f end |
#output ⇒ Float Also known as: output_costs
Returns the output cost
85 86 87 |
# File 'lib/llm/cost.rb', line 85 def output @output_costs || 0.to_f end |
#output_audio ⇒ Float Also known as: output_audio_costs
Returns the output audio cost
101 102 103 |
# File 'lib/llm/cost.rb', line 101 def output_audio @output_audio_costs || 0.to_f end |
#reasoning ⇒ Float Also known as: reasoning_costs
Returns the reasoning cost
133 134 135 |
# File 'lib/llm/cost.rb', line 133 def reasoning @reasoning_costs || 0.to_f end |
#to_h ⇒ Hash
Returns a hash with the non-nil cost components and the total
153 154 155 156 157 158 159 160 |
# File 'lib/llm/cost.rb', line 153 def to_h { input:, output:, cache_read:, cache_write:, input_audio:, output_audio:, input_image:, reasoning:, total: }.compact end |
#to_s ⇒ String
Returns the total cost in a human friendly format
165 166 167 |
# File 'lib/llm/cost.rb', line 165 def to_s format("%.2f", total) end |
#total ⇒ Float
Returns the total cost
141 142 143 144 145 146 147 148 |
# File 'lib/llm/cost.rb', line 141 def total [ input_costs, output_costs, input_audio_costs, output_audio_costs, cache_read_costs, cache_write_costs, input_image_costs, reasoning_costs ].compact.sum end |