Module: LlmCostTracker::PriceRegistry

Defined in:
lib/llm_cost_tracker/price_registry.rb

Constant Summary collapse

DEFAULT_PRICES_PATH =
File.expand_path("prices.json", __dir__)
EMPTY_PRICES =
{}.freeze
PRICE_KEYS =
%w[input cached_input output cache_read_input cache_creation_input].freeze
METADATA_KEYS =
%w[_source _updated _notes].freeze

Class Method Summary collapse

Class Method Details

.builtin_pricesObject



16
17
18
# File 'lib/llm_cost_tracker/price_registry.rb', line 16

def builtin_prices
  @builtin_prices ||= normalize_price_table(raw_registry.fetch("models", {})).freeze
end

.file_prices(path) ⇒ Object



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

def file_prices(path)
  return EMPTY_PRICES unless path

  path = path.to_s
  cache_key = [path, File.mtime(path).to_f]
  cached = @file_prices_cache
  return cached[:value] if cached && cached[:key] == cache_key

  value = normalize_file_prices(price_file_models(load_price_file(path)), path: path).freeze
  @file_prices_cache = { key: cache_key, value: value }.freeze
  value
rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, ArgumentError, TypeError, NoMethodError => e
  raise Error, "Unable to load prices_file #{path.inspect}: #{e.message}"
end

.metadataObject



20
21
22
# File 'lib/llm_cost_tracker/price_registry.rb', line 20

def 
  @metadata ||= raw_registry.fetch("metadata", {}).freeze
end

.normalize_price_table(table) ⇒ Object



24
25
26
27
28
# File 'lib/llm_cost_tracker/price_registry.rb', line 24

def normalize_price_table(table)
  (table || {}).each_with_object({}) do |(model, price), normalized|
    normalized[model.to_s] = normalize_price_entry(price)
  end
end