Class: LLM::Cost
- Inherits:
-
Struct
- Object
- Struct
- 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.
Instance Attribute Summary collapse
-
#cache_read_costs ⇒ Float?
Returns the cache read cost, or nil when no cache tokens were used.
-
#cache_write_costs ⇒ Float?
Returns the cache write cost, or nil when no cache creation tokens were used.
-
#input_audio_costs ⇒ Float?
Returns the input audio cost, or nil when no input audio tokens were used.
-
#input_costs ⇒ Float
Returns the input cost.
-
#input_image_costs ⇒ Float?
Returns the input image cost, or nil when no input image tokens were used.
-
#output_audio_costs ⇒ Float?
Returns the output audio cost, or nil when no output audio tokens were used.
-
#output_costs ⇒ Float
Returns the output cost.
-
#reasoning_costs ⇒ Float?
Returns the reasoning cost, or nil when no reasoning tokens were used.
Class Method Summary collapse
-
.from(ctx) ⇒ LLM::Cost
Build a cost breakdown from token usage and model pricing.
Instance Method Summary collapse
-
#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.
Instance Attribute Details
#cache_read_costs ⇒ Float?
Returns the cache read cost, or nil when no cache tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def cache_read_costs @cache_read_costs end |
#cache_write_costs ⇒ Float?
Returns the cache write cost, or nil when no cache creation tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def cache_write_costs @cache_write_costs end |
#input_audio_costs ⇒ Float?
Returns the input audio cost, or nil when no input audio tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_audio_costs @input_audio_costs end |
#input_costs ⇒ Float
Returns the input cost
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_costs @input_costs end |
#input_image_costs ⇒ Float?
Returns the input image cost, or nil when no input image tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_image_costs @input_image_costs end |
#output_audio_costs ⇒ Float?
Returns the output audio cost, or nil when no output audio tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def output_audio_costs @output_audio_costs end |
#output_costs ⇒ Float
Returns the output cost
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def output_costs @output_costs end |
#reasoning_costs ⇒ Float?
Returns the reasoning cost, or nil when no reasoning tokens were used
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def reasoning_costs @reasoning_costs end |
Class Method Details
.from(ctx) ⇒ LLM::Cost
Build a cost breakdown from token usage and model pricing.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/llm/cost.rb', line 43 def self.from(ctx) pricing = LLM.registry_for(ctx.llm).cost(model: ctx.model) new( input_costs: price(pricing.input, ctx.usage.input_tokens), output_costs: price(pricing.output, ctx.usage.output_tokens), input_audio_costs: price(pricing.input_audio, ctx.usage.input_audio_tokens), output_audio_costs: price(pricing.output_audio, ctx.usage.output_audio_tokens), input_image_costs: price(pricing.input, ctx.usage.input_image_tokens), cache_read_costs: price(pricing.cache_read, ctx.usage.cache_read_tokens), cache_write_costs: price(pricing.cache_write, ctx.usage.cache_write_tokens), reasoning_costs: price(pricing.output, ctx.usage.reasoning_tokens) ) rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError new end |
Instance Method Details
#to_h ⇒ Hash
Returns a hash with the non-nil cost components and the total
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/llm/cost.rb', line 83 def to_h { input: input_costs, output: output_costs, input_audio: input_audio_costs, output_audio: output_audio_costs, input_image: input_image_costs, cache_read: cache_read_costs, cache_write: cache_write_costs, reasoning: reasoning_costs, total: total }.compact end |
#to_s ⇒ String
Returns the total cost in a human friendly format
100 101 102 |
# File 'lib/llm/cost.rb', line 100 def to_s format("%.12f", total).sub(/\.?0+$/, "") end |
#total ⇒ Float
Returns the total cost
71 72 73 74 75 76 77 78 |
# File 'lib/llm/cost.rb', line 71 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.round(12) end |