Class: LlmCostTracker::Pricing::Calculation
- Inherits:
-
Object
- Object
- LlmCostTracker::Pricing::Calculation
- Defined in:
- lib/llm_cost_tracker/pricing/calculation.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
- #cost ⇒ Object
- #cost_status ⇒ Object
- #effective ⇒ Object
-
#initialize(provider:, model:, token_usage:, line_items:, mode:, usage_source: nil) ⇒ Calculation
constructor
A new instance of Calculation.
- #match ⇒ Object
- #priced_line_items ⇒ Object
- #snapshot ⇒ Object
- #token_cost ⇒ Object
Constructor Details
#initialize(provider:, model:, token_usage:, line_items:, mode:, usage_source: nil) ⇒ Calculation
Returns a new instance of Calculation.
24 25 26 27 28 29 30 31 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 24 def initialize(provider:, model:, token_usage:, line_items:, mode:, usage_source: nil) @provider = provider @model = model @token_usage = token_usage @line_items = line_items @mode = mode @usage_source = usage_source end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
33 34 35 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 33 def mode @mode end |
Class Method Details
.for(provider:, model:, tokens:, pricing_mode:, line_items: [], usage_source: nil) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 15 def self.for(provider:, model:, tokens:, pricing_mode:, line_items: [], usage_source: nil) new(provider: provider, model: model, token_usage: Usage::TokenUsage.build_from_tokens(tokens), line_items: line_items, mode: Mode.normalize(pricing_mode), usage_source: usage_source) end |
Instance Method Details
#cost ⇒ Object
67 68 69 70 71 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 67 def cost return @cost if defined?(@cost) @cost = combine_service_lines end |
#cost_status ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 73 def cost_status @cost_status ||= Charges::CostStatus.call( token_usage: @token_usage, usage_source: @usage_source, token_cost: token_cost, token_pricing_partial: token_pricing_partial?, service_line_items: priced_line_items.reject(&:token?), total_cost: cost&.total ) end |
#effective ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 41 def effective return @effective if defined?(@effective) @effective = match && EffectivePrices.call( usage: @token_usage, quantities: quantities, prices: match.prices, pricing_mode: @mode ) end |
#match ⇒ Object
35 36 37 38 39 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 35 def match return @match if defined?(@match) @match = Matcher.lookup(provider: @provider, model: @model) end |
#priced_line_items ⇒ Object
55 56 57 58 59 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 55 def priced_line_items @priced_line_items ||= unpriced_line_items.map do |line_item| line_item.token? ? price_token(line_item) : price_service(line_item) end end |
#snapshot ⇒ Object
61 62 63 64 65 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 61 def snapshot return @snapshot if defined?(@snapshot) @snapshot = priceable? ? build_snapshot : nil end |
#token_cost ⇒ Object
49 50 51 52 53 |
# File 'lib/llm_cost_tracker/pricing/calculation.rb', line 49 def token_cost return @token_cost if defined?(@token_cost) @token_cost = priceable? ? build_token_cost : nil end |