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)


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

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)


124
125
126
# File 'lib/riffer/config.rb', line 124

def cache_read
  @cache_read
end

#cache_writeFloat? (readonly)

Cache-write rate per million tokens.

Returns:

  • (Float, nil)


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

def cache_write
  @cache_write
end

#inputFloat (readonly)

Input rate per million tokens.

Returns:

  • (Float)


118
119
120
# File 'lib/riffer/config.rb', line 118

def input
  @input
end

#outputFloat (readonly)

Output rate per million tokens.

Returns:

  • (Float)


121
122
123
# File 'lib/riffer/config.rb', line 121

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)


141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/riffer/config.rb', line 141

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