Module: Amlexia::Cost
- Defined in:
- lib/amlexia/cost.rb
Class Method Summary collapse
- .enrich_event(event) ⇒ Object
- .estimate_cost_usd(cost_usd:, model_name: nil, provider: nil, tokens_input: nil, tokens_output: nil, total_tokens: nil) ⇒ Object
Class Method Details
.enrich_event(event) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/amlexia/cost.rb', line 42 def enrich_event(event) cost, = estimate_cost_usd( cost_usd: event[:cost_usd], model_name: event[:model_name], provider: event[:provider] || event[:provider_name], tokens_input: event[:tokens_input], tokens_output: event[:tokens_output], total_tokens: event[:total_tokens] ) event[:cost_usd] = cost if cost event end |
.estimate_cost_usd(cost_usd:, model_name: nil, provider: nil, tokens_input: nil, tokens_output: nil, total_tokens: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/amlexia/cost.rb', line 21 def estimate_cost_usd(cost_usd:, model_name: nil, provider: nil, tokens_input: nil, tokens_output: nil, total_tokens: nil) return [cost_usd, "reported"] if cost_usd && cost_usd.positive? key = (model_name || "").downcase price = MODEL_PRICES[key] price ||= MODEL_PRICES[PROVIDER_DEFAULT_MODEL[(provider || "").downcase]] if provider return [cost_usd, nil] unless price inp = tokens_input || 0 out = tokens_output || 0 if inp.zero? && out.zero? return [cost_usd, nil] unless total_tokens&.positive? blended = (price[:input] + price[:output]) / 2.0 return [(total_tokens / 1_000_000.0) * blended, "estimated"] end est = (inp / 1_000_000.0) * price[:input] + (out / 1_000_000.0) * price[:output] [est.round(8), "estimated"] end |