Class: Riffer::Config::Pricing::Rates

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/config.rb

Overview

Per-million-token rates for one model’s four token buckets. cache_read and cache_write fall back to the input rate when unset.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, output:, cache_read: nil, cache_write: nil) ⇒ Rates

– : (input: Float, output: Float, ?cache_read: Float?, ?cache_write: Float?) -> void



173
174
175
176
177
178
# File 'lib/riffer/config.rb', line 173

def initialize(input:, output:, cache_read: nil, cache_write: nil)
  @input = input
  @output = output
  @cache_read = cache_read
  @cache_write = cache_write
end

Instance Attribute Details

#cache_readObject (readonly)

Cache-read rate per million tokens.



166
167
168
# File 'lib/riffer/config.rb', line 166

def cache_read
  @cache_read
end

#cache_writeObject (readonly)

Cache-write rate per million tokens.



169
170
171
# File 'lib/riffer/config.rb', line 169

def cache_write
  @cache_write
end

#inputObject (readonly)

Input rate per million tokens.



160
161
162
# File 'lib/riffer/config.rb', line 160

def input
  @input
end

#outputObject (readonly)

Output rate per million tokens.



163
164
165
# File 'lib/riffer/config.rb', line 163

def output
  @output
end

Instance Method Details

#cost_for(input_tokens:, output_tokens:, cache_read_tokens: nil, cache_write_tokens: nil) ⇒ Object

Returns the cost for the given token counts. – : (input_tokens: Integer, output_tokens: Integer, ?cache_read_tokens: Integer?, ?cache_write_tokens: Integer?) -> Float



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/riffer/config.rb', line 183

def cost_for(input_tokens:, output_tokens:, cache_read_tokens: nil, cache_write_tokens: nil)
  read = cache_read_tokens || 0
  write = cache_write_tokens || 0
  uncached = input_tokens - read - write
  uncached = 0 if uncached.negative?

  per_million = uncached * input +
    read * (cache_read || input) +
    write * (cache_write || input) +
    output_tokens * output
  per_million / 1_000_000.0
end