Module: LlmCostTracker::Pricing::ServiceRates

Defined in:
lib/llm_cost_tracker/pricing/service_rates.rb

Class Method Summary collapse

Class Method Details

.charge_rate(provider:, dimension:, pricing_mode:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_cost_tracker/pricing/service_rates.rb', line 14

def charge_rate(provider:, dimension:, pricing_mode:)
  pricing_mode = Mode.normalize(pricing_mode)
  provider_name = provider.to_s.presence
  return nil unless provider_name

  dimension_key = charge_dimension_key(dimension)
  Registry.sources.each do |source|
    provider_rates = source.rates.fetch(provider_name, {})
    rate = rate_for(provider_rates, dimension_key: dimension_key, pricing_mode: pricing_mode)
    next unless rate

    return Pricing::Rate.new(
      amount: rate.fetch(:amount),
      quantity: rate.fetch(:quantity),
      currency: rate.fetch(:currency),
      source: source.name,
      source_key: "service_charges.#{provider_name}.#{rate.fetch(:source_key)}",
      source_version: source.version
    )
  end
  nil
end