Module: LlmCostTracker::Pricing

Defined in:
lib/llm_cost_tracker/pricing.rb

Constant Summary collapse

PRICES =
PriceRegistry.builtin_prices
MUTEX =
Monitor.new

Class Method Summary collapse

Class Method Details

.cost_for(provider:, model:, input_tokens:, output_tokens:, cache_read_input_tokens: 0, cache_write_input_tokens: 0, pricing_mode: nil) ⇒ Object



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

def cost_for(provider:, model:, input_tokens:, output_tokens:, cache_read_input_tokens: 0,
             cache_write_input_tokens: 0, pricing_mode: nil)
  prices = lookup(provider: provider, model: model)
  return nil unless prices

  usage = UsageBreakdown.build(
    input_tokens: input_tokens,
    output_tokens: output_tokens,
    cache_read_input_tokens: cache_read_input_tokens,
    cache_write_input_tokens: cache_write_input_tokens
  )
  costs = calculate_costs(usage, prices, pricing_mode: pricing_mode)

  Cost.new(
    input_cost: costs[:input].round(8),
    cache_read_input_cost: costs[:cache_read_input].round(8),
    cache_write_input_cost: costs[:cache_write_input].round(8),
    output_cost: costs[:output].round(8),
    total_cost: costs.values.sum.round(8),
    currency: "USD"
  )
end

.lookup(provider:, model:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/llm_cost_tracker/pricing.rb', line 34

def lookup(provider:, model:)
  provider_name = provider.to_s
  model_name = model.to_s
  provider_model = provider_name.empty? ? model_name : "#{provider_name}/#{model_name}"
  normalized_model = normalize_model_name(model_name)
  current = current_price_tables

  lookup_in_table(current.fetch(:pricing_overrides), provider_model, model_name, normalized_model) ||
    lookup_in_table(current.fetch(:file_prices), provider_model, model_name, normalized_model) ||
    lookup_in_table(PRICES, provider_model, model_name, normalized_model)
end