Class: ActiveHarness::Pricing::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/active_harness/pricing/source.rb

Overview

Reads a standardized pricing data file and looks up models by canonical key. Data is loaded lazily on first access and reloaded automatically after CACHE_TTL.

Data file format (JSON hash):

{
  "mistral-nemo": {
    "name":                "Mistral Nemo",
    "input_per_1m":        0.02,
    "output_per_1m":       0.03,
    "context_window":      131072,
    "tokens_per_second":   56.87,   # optional
    "time_to_first_token": 0.99     # optional
  }
}

Usage:

src = Source.new("data/pricepertoken.json", :pricepertoken)
src.find("mistral-nemo")           # exact
src.find("mistral-nemo-instruct")  # prefix fallback

Defined Under Namespace

Classes: PricingData

Constant Summary collapse

CACHE_TTL =

24 hours

86_400

Instance Method Summary collapse

Constructor Details

#initialize(data_file, source_name) ⇒ Source

Returns a new instance of Source.



44
45
46
47
# File 'lib/active_harness/pricing/source.rb', line 44

def initialize(data_file, source_name)
  @data_file   = data_file
  @source_name = source_name.to_sym
end

Instance Method Details

#allObject



66
67
68
# File 'lib/active_harness/pricing/source.rb', line 66

def all
  data.map { |key, raw| build(key, raw) }
end

#find(canonical_key) ⇒ Object

Finds a model by canonical key. Falls back to prefix match when exact key is not found (e.g. “mistral-nemo-instruct-2407” → finds “mistral-nemo”).



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_harness/pricing/source.rb', line 52

def find(canonical_key)
  key = canonical_key.to_s

  raw = data[key]
  return build(key, raw) if raw

  # prefix fallback: find the longest stored key that is a prefix of the lookup key
  match_key, match_raw = data
    .select { |k, _| key.start_with?(k) && k.length >= 5 }
    .max_by { |k, _| k.length }

  build(match_key, match_raw) if match_raw
end

#reload!Object



70
71
72
73
# File 'lib/active_harness/pricing/source.rb', line 70

def reload!
  @data      = nil
  @loaded_at = nil
end