Module: LlmCostTracker::Pricing::Lookup

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

Defined Under Namespace

Classes: Match

Constant Summary collapse

DEFAULT_CURRENCY =
"USD"
MUTEX =
Mutex.new
CACHE_MISS =
Object.new.freeze
NO_MATCH =
Object.new.freeze
LOOKUP_CACHE_LIMIT =
2_048

Class Method Summary collapse

Class Method Details

.call(provider:, model:) ⇒ Object



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

def call(provider:, model:)
  provider_name = provider.to_s.presence
  model_name = model.to_s
  return nil if model_name.empty?

  invalidate_cache_if_prices_file_changed!

  cache_key = [provider_name, model_name]
  cached = cached_lookup(cache_key)
  return cached unless cached.equal?(CACHE_MISS)

  match = lookup_match(provider_name: provider_name, model_name: model_name)
  cache_lookup(cache_key, match)
  match
end

.prices_file_mtime_isoObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm_cost_tracker/pricing/lookup.rb', line 39

def prices_file_mtime_iso
  invalidate_cache_if_prices_file_changed!
  signature = @prices_file_signature
  return nil unless signature

  cached = @prices_file_iso_cache
  return cached[:value] if cached && cached[:mtime] == signature

  MUTEX.synchronize do
    cached = @prices_file_iso_cache
    return cached[:value] if cached && cached[:mtime] == signature

    iso = signature.utc.iso8601
    @prices_file_iso_cache = { mtime: signature, value: iso }.freeze
    iso
  end
end

.reset!Object



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

def reset!
  MUTEX.synchronize do
    reset_prices_caches!(signature: nil)
    @prices_file_last_check_at = nil
  end
end