Class: LlmCostTracker::Billing::LineItem
- Inherits:
-
Data
- Object
- Data
- LlmCostTracker::Billing::LineItem
- Defined in:
- lib/llm_cost_tracker/billing/line_item.rb,
lib/llm_cost_tracker/billing/line_item.rb
Constant Summary collapse
- USD =
"USD"
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
- #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
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def cache_state @cache_state end |
#cost ⇒ Object (readonly)
Returns the value of attribute cost
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def cost @cost end |
#cost_status ⇒ Object (readonly)
Returns the value of attribute cost_status
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def cost_status @cost_status end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def currency @currency end |
#details ⇒ Object (readonly)
Returns the value of attribute details
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def details @details end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def direction @direction end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def kind @kind end |
#modality ⇒ Object (readonly)
Returns the value of attribute modality
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def modality @modality end |
#price_key ⇒ Object (readonly)
Returns the value of attribute price_key
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def price_key @price_key end |
#price_source ⇒ Object (readonly)
Returns the value of attribute price_source
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def price_source @price_source end |
#price_source_version ⇒ Object (readonly)
Returns the value of attribute price_source_version
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def price_source_version @price_source_version end |
#pricing_basis ⇒ Object (readonly)
Returns the value of attribute pricing_basis
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def pricing_basis @pricing_basis end |
#provider_field ⇒ Object (readonly)
Returns the value of attribute provider_field
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def provider_field @provider_field end |
#provider_item_id ⇒ Object (readonly)
Returns the value of attribute provider_item_id
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def provider_item_id @provider_item_id end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def quantity @quantity end |
#rate_amount ⇒ Object (readonly)
Returns the value of attribute rate_amount
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def rate_amount @rate_amount end |
#rate_quantity ⇒ Object (readonly)
Returns the value of attribute rate_quantity
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def rate_quantity @rate_quantity end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit
10 11 12 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 10 def unit @unit end |
Class Method Details
.build(attributes) ⇒ Object
34 35 36 37 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/billing/line_item.rb', line 34 def self.build(attributes) attributes = attributes.to_h component = component_for(attributes) new( kind: symbol_or_nil(attributes[:kind]) || component&.kind, direction: symbol_or_nil(attributes[:direction]) || component&.direction, modality: symbol_or_nil(attributes[:modality]) || component&.modality, cache_state: symbol_or_nil(attributes[:cache_state]) || component&.cache_state, quantity: decimal_or_zero(attributes[:quantity]), unit: symbol_or_nil(attributes[:unit]) || component&.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: attributes[:currency] || USD, cost_status: cost_status_for(attributes), pricing_basis: symbol_or_nil(attributes[:pricing_basis]), price_key: attributes[:price_key], price_source: symbol_or_nil(attributes[:price_source]), 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 59 def self.from_token_usage(token_usage) return [] unless token_usage token_usage.priced_quantities.filter_map do |key, quantity| next unless quantity.positive? component = Components::BY_KEY.fetch(key) build( kind: component.kind, direction: component.direction, modality: component.modality, cache_state: component.cache_state, quantity: quantity, unit: component.unit ) end end |
Instance Method Details
#billable? ⇒ Boolean
112 113 114 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 112 def billable? quantity.positive? end |
#cost_value ⇒ Object
128 129 130 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 128 def cost_value cost || BigDecimal("0") end |
#priced? ⇒ Boolean
116 117 118 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 116 def priced? [CostStatus::COMPLETE, CostStatus::FREE].include?(cost_status) end |
#to_h ⇒ Object
148 149 150 151 152 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 148 def to_h super.transform_values do |value| value.is_a?(BigDecimal) ? value.to_s("F") : value end end |
#token? ⇒ Boolean
124 125 126 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 124 def token? unit == :token end |
#unpriced? ⇒ Boolean
120 121 122 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 120 def unpriced? cost_status == CostStatus::UNKNOWN end |
#with_rate(rate) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 132 def with_rate(rate) rate_amount = rate.fetch(:amount) rate_quantity = rate.fetch(:quantity) applied_cost = (quantity / rate_quantity) * rate_amount with( rate_amount: rate_amount, rate_quantity: rate_quantity, cost: applied_cost, currency: rate.fetch(:currency), cost_status: applied_cost.zero? ? CostStatus::FREE : CostStatus::COMPLETE, price_key: rate.fetch(:source_key), price_source: rate.fetch(:source), price_source_version: rate.fetch(:source_version) ) end |