Module: LlmCostTracker::Pricing

Extended by:
ServiceCharges
Defined in:
lib/llm_cost_tracker/pricing.rb,
lib/llm_cost_tracker/pricing/sync.rb,
lib/llm_cost_tracker/pricing/lookup.rb,
lib/llm_cost_tracker/pricing/unknown.rb,
lib/llm_cost_tracker/pricing/registry.rb,
lib/llm_cost_tracker/pricing/explainer.rb,
lib/llm_cost_tracker/pricing/sync/fetcher.rb,
lib/llm_cost_tracker/pricing/service_charges.rb,
lib/llm_cost_tracker/pricing/effective_prices.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
lib/llm_cost_tracker/pricing/sync_change_printer.rb,
lib/llm_cost_tracker/pricing/sync/registry_writer.rb

Defined Under Namespace

Modules: EffectivePrices, Explainer, Lookup, Registry, ServiceCharges, Sync, SyncChangePrinter Classes: Explanation, Unknown

Constant Summary

Constants included from ServiceCharges

ServiceCharges::DEFAULT_CURRENCY, ServiceCharges::EMPTY_RATES, ServiceCharges::MUTEX

Class Method Summary collapse

Methods included from ServiceCharges

builtin_rates, charge_rate, file_rates, rates_from_registry, reset!

Class Method Details

.cost_and_snapshot_for(provider:, model:, tokens:, pricing_mode: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm_cost_tracker/pricing.rb', line 45

def cost_and_snapshot_for(provider:, model:, tokens:, pricing_mode: nil)
  calculation = calculation_for(
    provider: provider,
    model: model,
    tokens: tokens,
    pricing_mode: pricing_mode
  )
  return [nil, nil] unless calculation

  [cost_from(calculation), snapshot_from(calculation)]
end

.cost_for(provider:, model:, tokens:, pricing_mode: nil) ⇒ Object



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

def cost_for(provider:, model:, tokens:, pricing_mode: nil)
  calculation = calculation_for(
    provider: provider,
    model: model,
    tokens: tokens,
    pricing_mode: pricing_mode
  )
  return nil unless calculation

  cost_from(calculation)
end

.explain(provider:, model:, tokens:, pricing_mode: nil) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/llm_cost_tracker/pricing.rb', line 83

def explain(provider:, model:, tokens:, pricing_mode: nil)
  Explainer.call(
    provider: provider,
    model: model,
    tokens: tokens,
    pricing_mode: pricing_mode
  )
end

.normalize_mode(value) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/llm_cost_tracker/pricing.rb', line 24

def normalize_mode(value)
  return nil if value.nil?

  mode = value.is_a?(Symbol) ? value.downcase : normalize_string_mode(value)
  return nil unless mode

  STANDARD_MODE_VALUES.include?(mode) ? nil : mode
end

.price_line_items(provider:, model:, line_items:, pricing_mode: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/llm_cost_tracker/pricing.rb', line 57

def price_line_items(provider:, model:, line_items:, pricing_mode: nil)
  token_usage = TokenUsage.build_from_tokens(token_attributes_from(line_items))
  calculation = calculation_for(provider: provider, model: model, tokens: token_usage, pricing_mode: pricing_mode)
  snapshot = calculation && snapshot_from(calculation)

  priced = line_items.map do |line_item|
    next price_token_line_item(line_item, calculation) if line_item.unit == :token

    price_service_charge_line_item(line_item, provider: provider, pricing_mode: pricing_mode)
  end

  [priced, snapshot]
end

.snapshot_for(provider:, model:, tokens:, pricing_mode: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/llm_cost_tracker/pricing.rb', line 71

def snapshot_for(provider:, model:, tokens:, pricing_mode: nil)
  calculation = calculation_for(
    provider: provider,
    model: model,
    tokens: tokens,
    pricing_mode: pricing_mode
  )
  return nil unless calculation

  snapshot_from(calculation)
end

.stored_cost_attributes(attributes) ⇒ Object



92
93
94
95
# File 'lib/llm_cost_tracker/pricing.rb', line 92

def stored_cost_attributes(attributes)
  value = attributes.to_h[:total_cost]
  value.nil? ? {} : { total_cost: value }
end