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?
(also: #cache_read)
Returns the cache read cost, or nil when no cache tokens were used, aliased as
cache_read. -
#cache_write_costs ⇒ Float?
(also: #cache_write)
Returns the cache write cost, or nil when no cache creation tokens were used, aliased as
cache_write. -
#input_audio_costs ⇒ Float?
(also: #input_audio)
Returns the input audio cost, or nil when no input audio tokens were used, aliased as
input_audio. -
#input_costs ⇒ Float
(also: #input)
Returns the input cost, aliased as
input. -
#input_image_costs ⇒ Float?
(also: #input_image)
Returns the input image cost, or nil when no input image tokens were used, aliased as
input_image. -
#output_audio_costs ⇒ Float?
(also: #output_audio)
Returns the output audio cost, or nil when no output audio tokens were used, aliased as
output_audio. -
#output_costs ⇒ Float
(also: #output)
Returns the output cost, aliased as
output. -
#reasoning_costs ⇒ Float?
(also: #reasoning)
Returns the reasoning cost, or nil when no reasoning tokens were used, aliased as
reasoning.
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? Also known as: cache_read
Returns the cache read cost, or nil when no cache tokens
were used, aliased as cache_read
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def cache_read_costs @cache_read_costs end |
#cache_write_costs ⇒ Float? Also known as: cache_write
Returns the cache write cost, or nil when no cache creation
tokens were used, aliased as cache_write
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def cache_write_costs @cache_write_costs end |
#input_audio_costs ⇒ Float? Also known as: input_audio
Returns the input audio cost, or nil when no input audio tokens
were used, aliased as input_audio
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_audio_costs @input_audio_costs end |
#input_costs ⇒ Float Also known as: input
Returns the input cost, aliased as input
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_costs @input_costs end |
#input_image_costs ⇒ Float? Also known as: input_image
Returns the input image cost, or nil when no input image tokens
were used, aliased as input_image
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def input_image_costs @input_image_costs end |
#output_audio_costs ⇒ Float? Also known as: output_audio
Returns the output audio cost, or nil when no output audio tokens
were used, aliased as output_audio
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def output_audio_costs @output_audio_costs end |
#output_costs ⇒ Float Also known as: output
Returns the output cost, aliased as output
31 32 33 |
# File 'lib/llm/cost.rb', line 31 def output_costs @output_costs end |
#reasoning_costs ⇒ Float? Also known as: reasoning
Returns the reasoning cost, or nil when no reasoning tokens
were used, aliased as reasoning
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 |
# File 'lib/llm/cost.rb', line 83 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
95 96 97 |
# File 'lib/llm/cost.rb', line 95 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 |