Class: LLM::Cost

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • input_costs (Float, nil) (defaults to: nil)
  • output_costs (Float, nil) (defaults to: nil)
  • input_audio_costs (Float, nil) (defaults to: nil)
  • output_audio_costs (Float, nil) (defaults to: nil)
  • input_image_costs (Float, nil) (defaults to: nil)
  • cache_read_costs (Float, nil) (defaults to: nil)
  • cache_write_costs (Float, nil) (defaults to: nil)
  • reasoning_costs (Float, nil) (defaults to: nil)


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.

Parameters:

  • ctx (LLM::Context)

    Context used to resolve provider, model, and token usage

Returns:



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

Returns:

  • (Boolean)

    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_readFloat Also known as: cache_read_costs

Returns the cache read cost

Returns:

  • (Float)

    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_writeFloat Also known as: cache_write_costs

Returns the cache write cost

Returns:

  • (Float)

    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

#inputFloat Also known as: input_costs

Returns the input cost

Returns:

  • (Float)

    Returns the input cost



77
78
79
# File 'lib/llm/cost.rb', line 77

def input
  @input_costs || 0.to_f
end

#input_audioFloat Also known as: input_audio_costs

Returns the input audio cost

Returns:

  • (Float)

    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_imageFloat Also known as: input_image_costs

Returns the input image cost

Returns:

  • (Float)

    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

#outputFloat Also known as: output_costs

Returns the output cost

Returns:

  • (Float)

    Returns the output cost



85
86
87
# File 'lib/llm/cost.rb', line 85

def output
  @output_costs || 0.to_f
end

#output_audioFloat Also known as: output_audio_costs

Returns the output audio cost

Returns:

  • (Float)

    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

#reasoningFloat Also known as: reasoning_costs

Returns the reasoning cost

Returns:

  • (Float)

    Returns the reasoning cost



133
134
135
# File 'lib/llm/cost.rb', line 133

def reasoning
  @reasoning_costs || 0.to_f
end

#to_hHash

Returns a hash with the non-nil cost components and the total

Returns:

  • (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_sString

Returns the total cost in a human friendly format

Returns:

  • (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

#totalFloat

Returns the total cost

Returns:

  • (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