Module: LlmCostTracker::PriceRegistry

Defined in:
lib/llm_cost_tracker/price_registry.rb

Constant Summary collapse

DEFAULT_PRICES_PATH =
File.expand_path("prices.json", __dir__)
PRICE_KEYS =
%w[input cached_input output cache_read_input cache_creation_input].freeze
NORMALIZE_PRICE_ENTRY =
lambda do |price|
  (price || {}).each_with_object({}) do |(key, value), normalized|
    key = key.to_s
    normalized[key.to_sym] = Float(value) if PRICE_KEYS.include?(key)
  end
end
NORMALIZE_PRICE_TABLE =
lambda do |table|
  (table || {}).each_with_object({}) do |(model, price), normalized|
    normalized[model.to_s] = NORMALIZE_PRICE_ENTRY.call(price)
  end
end
RAW_REGISTRY =
JSON.parse(File.read(DEFAULT_PRICES_PATH)).freeze
PRICE_METADATA =
RAW_REGISTRY.fetch("metadata", {}).freeze
BUILTIN_PRICES =
NORMALIZE_PRICE_TABLE.call(RAW_REGISTRY.fetch("models", {})).freeze

Class Method Summary collapse

Class Method Details

.builtin_pricesObject



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

def builtin_prices
  BUILTIN_PRICES
end

.file_prices(path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llm_cost_tracker/price_registry.rb', line 38

def file_prices(path)
  return {} unless path

  path = path.to_s
  cache_key = [path, File.mtime(path).to_f]
  return @file_prices if @file_prices_cache_key == cache_key

  @file_prices_cache_key = cache_key
  @file_prices = normalize_price_table(price_file_models(load_price_file(path)))
rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, ArgumentError, TypeError => e
  raise Error, "Unable to load prices_file #{path.inspect}: #{e.message}"
end

.metadataObject



30
31
32
# File 'lib/llm_cost_tracker/price_registry.rb', line 30

def 
  PRICE_METADATA
end

.normalize_price_table(table) ⇒ Object



34
35
36
# File 'lib/llm_cost_tracker/price_registry.rb', line 34

def normalize_price_table(table)
  NORMALIZE_PRICE_TABLE.call(table)
end