Class: LlmCostTracker::Charges::LineItem
- Inherits:
-
Data
- Object
- Data
- LlmCostTracker::Charges::LineItem
- Defined in:
- lib/llm_cost_tracker/charges/line_item.rb,
lib/llm_cost_tracker/charges/line_item.rb
Instance Attribute Summary collapse
-
#cache_state ⇒ Object
readonly
Returns the value of attribute cache_state.
-
#cost ⇒ Object
readonly
Returns the value of attribute cost.
-
#cost_status ⇒ Object
readonly
Returns the value of attribute cost_status.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#modality ⇒ Object
readonly
Returns the value of attribute modality.
-
#price_key ⇒ Object
readonly
Returns the value of attribute price_key.
-
#price_source ⇒ Object
readonly
Returns the value of attribute price_source.
-
#price_source_version ⇒ Object
readonly
Returns the value of attribute price_source_version.
-
#pricing_basis ⇒ Object
readonly
Returns the value of attribute pricing_basis.
-
#provider_field ⇒ Object
readonly
Returns the value of attribute provider_field.
-
#provider_item_id ⇒ Object
readonly
Returns the value of attribute provider_item_id.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#rate_amount ⇒ Object
readonly
Returns the value of attribute rate_amount.
-
#rate_quantity ⇒ Object
readonly
Returns the value of attribute rate_quantity.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #billable? ⇒ Boolean
- #cost_value ⇒ Object
- #dimension ⇒ Object
- #priced? ⇒ Boolean
- #to_h ⇒ Object
- #token? ⇒ Boolean
- #unpriced? ⇒ Boolean
- #with_rate(rate) ⇒ Object
Instance Attribute Details
#cache_state ⇒ Object (readonly)
Returns the value of attribute cache_state
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def cache_state @cache_state end |
#cost ⇒ Object (readonly)
Returns the value of attribute cost
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def cost @cost end |
#cost_status ⇒ Object (readonly)
Returns the value of attribute cost_status
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def cost_status @cost_status end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def currency @currency end |
#details ⇒ Object (readonly)
Returns the value of attribute details
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def details @details end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def direction @direction end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def kind @kind end |
#modality ⇒ Object (readonly)
Returns the value of attribute modality
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def modality @modality end |
#price_key ⇒ Object (readonly)
Returns the value of attribute price_key
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def price_key @price_key end |
#price_source ⇒ Object (readonly)
Returns the value of attribute price_source
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def price_source @price_source end |
#price_source_version ⇒ Object (readonly)
Returns the value of attribute price_source_version
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def price_source_version @price_source_version end |
#pricing_basis ⇒ Object (readonly)
Returns the value of attribute pricing_basis
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def pricing_basis @pricing_basis end |
#provider_field ⇒ Object (readonly)
Returns the value of attribute provider_field
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def provider_field @provider_field end |
#provider_item_id ⇒ Object (readonly)
Returns the value of attribute provider_item_id
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def provider_item_id @provider_item_id end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def quantity @quantity end |
#rate_amount ⇒ Object (readonly)
Returns the value of attribute rate_amount
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def rate_amount @rate_amount end |
#rate_quantity ⇒ Object (readonly)
Returns the value of attribute rate_quantity
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def rate_quantity @rate_quantity end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit
11 12 13 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11 def unit @unit end |
Class Method Details
.build(attributes) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 33 def self.build(attributes) attributes = attributes.to_h dimension = dimension_for(attributes) new( kind: attributes[:kind]&.to_s || dimension&.kind, direction: attributes[:direction]&.to_s || dimension&.direction, modality: attributes[:modality]&.to_s || dimension&.modality, cache_state: attributes[:cache_state]&.to_s || dimension&.cache_state || "none", quantity: decimal_or_nil(attributes[:quantity]) || BigDecimal("0"), unit: attributes[:unit]&.to_s || dimension&.unit, rate_amount: decimal_or_nil(attributes[:rate_amount]), rate_quantity: decimal_or_nil(attributes[:rate_quantity]) || BigDecimal("1"), cost: decimal_or_nil(attributes[:cost]), currency: canonical_currency(attributes[:currency]), cost_status: cost_status_for(attributes), pricing_basis: attributes[:pricing_basis]&.to_s, price_key: attributes[:price_key]&.to_s, price_source: attributes[:price_source]&.to_s, price_source_version: attributes[:price_source_version], provider_field: attributes[:provider_field], provider_item_id: attributes[:provider_item_id], details: attributes[:details] || {} ) end |
.from_token_usage(token_usage) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 58 def self.from_token_usage(token_usage) return [] unless token_usage token_usage.priced_quantities.filter_map do |key, quantity| next unless quantity.positive? dimension = Usage::Catalog.fetch(key) build( kind: dimension.kind, direction: dimension.direction, modality: dimension.modality, cache_state: dimension.cache_state, quantity: quantity, unit: dimension.unit ) end end |
Instance Method Details
#billable? ⇒ Boolean
105 106 107 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 105 def billable? quantity.positive? end |
#cost_value ⇒ Object
126 127 128 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 126 def cost_value cost || BigDecimal("0") end |
#dimension ⇒ Object
121 122 123 124 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 121 def dimension Usage::Catalog[price_key] || Usage::Catalog.token_priced_for(kind: kind, direction: direction, cache_state: cache_state) end |
#priced? ⇒ Boolean
109 110 111 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 109 def priced? [CostStatus::COMPLETE, CostStatus::FREE].include?(cost_status) end |
#to_h ⇒ Object
144 145 146 147 148 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 144 def to_h super.transform_values do |value| value.is_a?(BigDecimal) ? value.to_s("F") : value end end |
#token? ⇒ Boolean
117 118 119 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 117 def token? unit == "token" end |
#unpriced? ⇒ Boolean
113 114 115 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 113 def unpriced? cost_status == CostStatus::UNKNOWN end |
#with_rate(rate) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 130 def with_rate(rate) applied_cost = (quantity / rate.quantity) * rate.amount with( rate_amount: rate.amount, rate_quantity: rate.quantity, cost: applied_cost, currency: rate.currency.upcase, cost_status: applied_cost.zero? ? CostStatus::FREE : CostStatus::COMPLETE, price_key: rate.source_key, price_source: rate.source, price_source_version: rate.source_version ) end |