Module: LlmCostTracker::Pricing::Lookup

Defined in:
lib/llm_cost_tracker/pricing/lookup.rb

Defined Under Namespace

Classes: Match

Constant Summary collapse

MUTEX =
Mutex.new
CACHE_MISS =
Object.new.freeze
NO_MATCH =
Object.new.freeze
MAX_LOOKUP_CACHE_ENTRIES =
512

Class Method Summary collapse

Class Method Details

.call(provider:, model:) ⇒ Object



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

def call(provider:, model:)
  provider_name = provider.to_s.presence
  model_name = model.to_s
  cache_key = [provider_name, model_name]
  cached = cached_lookup(cache_key)
  return cached unless cached.equal?(CACHE_MISS)

  provider_model = provider_name ? "#{provider_name}/#{model_name}" : model_name
  normalized_model = normalize_model_name(model_name)
  current = current_price_tables

  match =
    explain_table(current.fetch(:pricing_overrides), :pricing_overrides, provider_model, model_name,
                  normalized_model) ||
    explain_table(current.fetch(:file_prices), :prices_file, provider_model, model_name, normalized_model) ||
    explain_table(Registry.builtin_prices, :bundled, provider_model, model_name, normalized_model)
  cache_lookup(cache_key, match)
  match
end

.reset!Object



33
34
35
36
37
38
39
# File 'lib/llm_cost_tracker/pricing/lookup.rb', line 33

def reset!
  MUTEX.synchronize do
    @prices_cache = nil
    @lookup_cache = nil
    @sorted_price_keys_cache = nil
  end
end