Module: LlmCostTracker::Pricing::Mode

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

Constant Summary collapse

STANDARD_MODE_VALUES =
%w[auto default standard standard_only unspecified].freeze
COMPOUND_MODIFIERS =
%w[data_residency].freeze
KNOWN_MODIFIERS =
%w[batch flex priority scale fast on_demand data_residency].freeze
MAX_PERMUTED_MODIFIERS =
6

Class Method Summary collapse

Class Method Details

.compose(tokens) ⇒ Object



33
34
35
36
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 33

def self.compose(tokens)
  tokens = Array(tokens).compact.uniq
  tokens.empty? ? nil : tokens.join("_")
end

.merge(provider_mode, request_mode) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 22

def self.merge(provider_mode, request_mode)
  return normalize(request_mode) if provider_mode.to_s.strip.empty?

  provider_tokens = tokenize(provider_mode) - STANDARD_MODE_VALUES
  request_host_tokens = tokenize(request_mode || "") & COMPOUND_MODIFIERS
  combined = provider_tokens | request_host_tokens
  return nil if combined.empty?

  normalize(combined.join("_"))
end

.normalize(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 11

def self.normalize(value)
  return nil if value.nil?

  mode = normalize_string(value.to_s)
  return nil unless mode
  return nil if STANDARD_MODE_VALUES.include?(mode)

  warn_unknown_tokens(mode)
  mode
end

.permutations_for(value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 59

def self.permutations_for(value)
  modifiers = tokenize(value).uniq.sort
  return [""] if modifiers.empty?
  return [modifiers.first] if modifiers.size == 1
  return [modifiers.join("_")] if modifiers.size > MAX_PERMUTED_MODIFIERS

  modifiers.permutation.map { |permutation| permutation.join("_") }.uniq
end

.tokenize(value) ⇒ Object



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

def self.tokenize(value)
  remaining = value.to_s.downcase.tr("-", "_")
  tokens = []
  loop do
    break if remaining.empty?

    compound = COMPOUND_MODIFIERS.find do |token|
      remaining == token || remaining.start_with?("#{token}_")
    end
    if compound
      tokens << compound
      remaining = remaining.delete_prefix(compound).delete_prefix("_")
    else
      first, _, rest = remaining.partition("_")
      tokens << first unless first.empty?
      remaining = rest
    end
  end
  tokens
end