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

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/config.rb,
sig/generated/riffer/config.rbs

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

Parameters:

  • input: (Float)
  • output: (Float)
  • cache_read: (Float, nil) (defaults to: nil)
  • cache_write: (Float, nil) (defaults to: nil)


140
141
142
143
144
145
# File 'lib/riffer/config.rb', line 140

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_readFloat? (readonly)

Cache-read rate per million tokens.

Returns:

  • (Float, nil)


133
134
135
# File 'lib/riffer/config.rb', line 133

def cache_read
  @cache_read
end

#cache_writeFloat? (readonly)

Cache-write rate per million tokens.

Returns:

  • (Float, nil)


136
137
138
# File 'lib/riffer/config.rb', line 136

def cache_write
  @cache_write
end

#inputFloat (readonly)

Input rate per million tokens.

Returns:

  • (Float)


127
128
129
# File 'lib/riffer/config.rb', line 127

def input
  @input
end

#outputFloat (readonly)

Output rate per million tokens.

Returns:

  • (Float)


130
131
132
# File 'lib/riffer/config.rb', line 130

def output
  @output
end

Instance Method Details

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

Returns the cost for the given token counts.

: (input_tokens: Integer, output_tokens: Integer, ?cache_read_tokens: Integer?, ?cache_write_tokens: Integer?) -> Float

Parameters:

  • input_tokens: (Integer)
  • output_tokens: (Integer)
  • cache_read_tokens: (Integer, nil) (defaults to: nil)
  • cache_write_tokens: (Integer, nil) (defaults to: nil)

Returns:

  • (Float)


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/riffer/config.rb', line 150

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