Class: LLM::Cost

Inherits:
Struct
  • 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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_read_costsFloat?

Returns the cache read cost, or nil when no cache tokens were used

Returns:

  • (Float, nil)

    the current value of cache_read_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def cache_read_costs
  @cache_read_costs
end

#cache_write_costsFloat?

Returns the cache write cost, or nil when no cache creation tokens were used

Returns:

  • (Float, nil)

    the current value of cache_write_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def cache_write_costs
  @cache_write_costs
end

#input_audio_costsFloat?

Returns the input audio cost, or nil when no input audio tokens were used

Returns:

  • (Float, nil)

    the current value of input_audio_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def input_audio_costs
  @input_audio_costs
end

#input_costsFloat

Returns the input cost

Returns:

  • (Float)

    the current value of input_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def input_costs
  @input_costs
end

#input_image_costsFloat?

Returns the input image cost, or nil when no input image tokens were used

Returns:

  • (Float, nil)

    the current value of input_image_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def input_image_costs
  @input_image_costs
end

#output_audio_costsFloat?

Returns the output audio cost, or nil when no output audio tokens were used

Returns:

  • (Float, nil)

    the current value of output_audio_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def output_audio_costs
  @output_audio_costs
end

#output_costsFloat

Returns the output cost

Returns:

  • (Float)

    the current value of output_costs



31
32
33
# File 'lib/llm/cost.rb', line 31

def output_costs
  @output_costs
end

#reasoning_costsFloat?

Returns the reasoning cost, or nil when no reasoning tokens were used

Returns:

  • (Float, nil)

    the current value of reasoning_costs



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.

Parameters:

  • Context (LLM::Context)

    used to resolve provider, model, and token usage

Returns:



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_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



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_sString

Returns the total cost in a human friendly format

Returns:

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

#totalFloat

Returns the total cost

Returns:

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