Module: ActiveAgent::Delegation::Pricing
- Defined in:
- lib/active_agent/delegation/pricing.rb
Overview
Token price registry used to turn usage into dollars for cost budgets.
ActiveAgent deliberately ships no built-in price list: vendor pricing changes far faster than a gem release, and a stale table silently under-reports spend. Register the rates your app actually pays — once, in an initializer — and every cost budget in the app uses them.
Rates are expressed in USD per one million tokens, matching how every major provider publishes them.
Defined Under Namespace
Classes: Rate
Class Method Summary collapse
-
.cost_for(usage:, model: nil, rates: nil) ⇒ Float?
Computes the dollar cost of a single generation.
-
.rates ⇒ Array<Rate>
Registered rates, most recently registered first.
-
.rates_for(model) ⇒ Hash?
{ input:, output: }in USD per 1M tokens. -
.register(pattern, input:, output:) ⇒ Rate
Registers a rate card.
-
.reset! ⇒ void
Clears the registry.
Class Method Details
.cost_for(usage:, model: nil, rates: nil) ⇒ Float?
Computes the dollar cost of a single generation.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_agent/delegation/pricing.rb', line 79 def cost_for(usage:, model: nil, rates: nil) return nil if usage.nil? resolved = normalize(rates) || rates_for(model) return nil if resolved.nil? input = (usage.input_tokens || 0) * resolved[:input] output = (usage.output_tokens || 0) * resolved[:output] (input + output) / 1_000_000.0 end |
.rates ⇒ Array<Rate>
Returns registered rates, most recently registered first.
37 38 39 |
# File 'lib/active_agent/delegation/pricing.rb', line 37 def rates @rates ||= [] end |
.rates_for(model) ⇒ Hash?
Returns { input:, output: } in USD per 1M tokens.
66 67 68 69 70 71 |
# File 'lib/active_agent/delegation/pricing.rb', line 66 def rates_for(model) return nil if model.blank? rate = rates.find { |candidate| candidate.matches?(model) } { input: rate.input, output: rate.output } if rate end |
.register(pattern, input:, output:) ⇒ Rate
Registers a rate card.
String patterns match by prefix (so "gpt-4o-mini" covers
"gpt-4o-mini-2024-07-18"); Regexp patterns match as written. Later
registrations win over earlier ones.
51 52 53 54 55 |
# File 'lib/active_agent/delegation/pricing.rb', line 51 def register(pattern, input:, output:) Rate.new(pattern: pattern, input: input.to_f, output: output.to_f).tap do |rate| rates.unshift(rate) end end |
.reset! ⇒ void
This method returns an undefined value.
Clears the registry. Mostly useful in tests.
60 61 62 |
# File 'lib/active_agent/delegation/pricing.rb', line 60 def reset! @rates = [] end |