Module: Smith::Pricing

Defined in:
lib/smith/pricing.rb

Class Method Summary collapse

Class Method Details

.compute_cost(model:, input_tokens:, output_tokens:) ⇒ Object

Compute provider cost for a single agent call. Two pricing shapes are supported:

Flat (existing): the catalog entry is a Hash with
  `:input_cost_per_token` / `:output_cost_per_token` keys. Used
  for models with a single rate across all input sizes
  (Gemini 2.5 Flash, Claude Opus 4.6/4.7).

Tiered (new): the catalog entry has a `:tiers` array of bracket
  hashes, each with `:max_input_tokens` (nil = unbounded ceiling),
  `:input_cost_per_token`, `:output_cost_per_token`. Tiers are
  walked in order; the first whose `max_input_tokens` covers the
  call's input_tokens picks the rate. Used for models with
  long-context premium pricing (Gemini 2.5 Pro: $1.25/$10 below
  200K input tokens, $2.50/$15 above).


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/smith/pricing.rb', line 20

def self.compute_cost(model:, input_tokens:, output_tokens:)
  catalog = Smith.config.pricing
  return nil unless catalog

  entry = catalog[model.to_s]
  return nil unless entry

  rates = resolve_rates(entry, input_tokens)
  return nil unless rates

  input_rate, output_rate = rates
  (input_tokens * input_rate) + (output_tokens * output_rate)
end