Module: LlmCostTracker::PriceRegistry
- Defined in:
- lib/llm_cost_tracker/price_registry.rb
Constant Summary collapse
- DEFAULT_PRICES_PATH =
File.("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
- MUTEX =
Monitor.new
Class Method Summary collapse
- .builtin_prices ⇒ Object
- .file_prices(path) ⇒ Object
- .metadata ⇒ Object
- .normalize_price_table(table) ⇒ Object
Class Method Details
.builtin_prices ⇒ Object
18 19 20 21 22 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 18 def builtin_prices @builtin_prices ||= MUTEX.synchronize do @builtin_prices || normalize_price_table(raw_registry.fetch("models", {})).freeze end end |
.file_prices(path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 34 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 MUTEX.synchronize do 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 end rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, ArgumentError, TypeError, NoMethodError => e raise Error, "Unable to load prices_file #{path.inspect}: #{e.}" end |
.metadata ⇒ Object
24 25 26 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 24 def @metadata ||= MUTEX.synchronize { @metadata || raw_registry.fetch("metadata", {}).freeze } end |
.normalize_price_table(table) ⇒ Object
28 29 30 31 32 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 28 def normalize_price_table(table) (table || {}).each_with_object({}) do |(model, price), normalized| normalized[model.to_s] = normalize_price_entry(price) end end |