Module: LlmCostTracker::Pricing
- Defined in:
- lib/llm_cost_tracker/pricing.rb,
lib/llm_cost_tracker/pricing/sync.rb,
lib/llm_cost_tracker/pricing/lookup.rb,
lib/llm_cost_tracker/pricing/unknown.rb,
lib/llm_cost_tracker/pricing/registry.rb,
lib/llm_cost_tracker/pricing/explainer.rb,
lib/llm_cost_tracker/pricing/components.rb,
lib/llm_cost_tracker/pricing/sync/fetcher.rb,
lib/llm_cost_tracker/pricing/effective_prices.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
lib/llm_cost_tracker/pricing/sync/registry_loader.rb,
lib/llm_cost_tracker/pricing/sync/registry_writer.rb
Defined Under Namespace
Modules: EffectivePrices, Explainer, Lookup, Registry, Sync
Classes: Component, Explanation, Unknown
Constant Summary
collapse
- COMPONENTS =
[
Component.new(
price_key: :input,
token_key: :input_tokens,
cost_key: :input_cost
),
Component.new(
price_key: :cache_read_input,
token_key: :cache_read_input_tokens,
cost_key: :cache_read_input_cost
),
Component.new(
price_key: :cache_write_input,
token_key: :cache_write_input_tokens,
cost_key: :cache_write_input_cost
),
Component.new(
price_key: :cache_write_1h_input,
token_key: :cache_write_1h_input_tokens,
cost_key: :cache_write_1h_input_cost
),
Component.new(
price_key: :output,
token_key: :output_tokens,
cost_key: :output_cost
)
].freeze
- COST_KEYS =
(COMPONENTS.map(&:cost_key) + %i[total_cost]).freeze
Class Method Summary
collapse
Class Method Details
.cost_for(provider:, model:, token_usage:, pricing_mode: nil) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/llm_cost_tracker/pricing.rb', line 26
def cost_for(provider:, model:, token_usage:, pricing_mode: nil)
prices = lookup(provider: provider, model: model)
return nil unless prices
costs = calculate_costs(token_usage, prices, pricing_mode: pricing_mode)
return nil unless costs
values = COMPONENTS.to_h do |component|
[component.cost_key, costs.fetch(component.price_key).round(8)]
end
values.merge(total_cost: costs.values.sum.round(8))
end
|
.explain(provider:, model:, token_usage:, pricing_mode: nil) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/llm_cost_tracker/pricing.rb', line 44
def explain(provider:, model:, token_usage:, pricing_mode: nil)
Explainer.call(
provider: provider,
model: model,
token_usage: token_usage,
pricing_mode: pricing_mode
)
end
|
.lookup(provider:, model:) ⇒ Object
40
41
42
|
# File 'lib/llm_cost_tracker/pricing.rb', line 40
def lookup(provider:, model:)
Lookup.call(provider: provider, model: model)&.prices
end
|
.normalize_mode(value) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/llm_cost_tracker/pricing.rb', line 18
def normalize_mode(value)
mode = value.to_s.strip.presence
return nil unless mode
mode = mode.tr("-", "_")
STANDARD_MODE_VALUES.include?(mode) ? nil : mode
end
|
.stored_cost_attributes(attributes) ⇒ Object
53
54
55
|
# File 'lib/llm_cost_tracker/pricing.rb', line 53
def stored_cost_attributes(attributes)
attributes.to_h.symbolize_keys.slice(*COST_KEYS).compact
end
|