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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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(
      table: current.fetch(:pricing_overrides),
      source: :pricing_overrides,
      provider_model: provider_model,
      model_name: model_name,
      normalized_model: normalized_model
    ) ||
    explain_table(
      table: current.fetch(:file_prices),
      source: :prices_file,
      provider_model: provider_model,
      model_name: model_name,
      normalized_model: normalized_model
    ) ||
    explain_table(
      table: Registry.builtin_prices,
      source: :bundled,
      provider_model: provider_model,
      model_name: model_name,
      normalized_model: normalized_model
    )
  cache_lookup(cache_key, match)
  match
end

.reset!Object



50
51
52
53
54
55
56
# File 'lib/llm_cost_tracker/pricing/lookup.rb', line 50

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