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"- OPTIONAL_ATTRIBUTES =
%i[ pricing_basis price_key price_source price_source_version provider_field provider_item_id ].freeze
- SYMBOL_ATTRIBUTES =
%i[ kind direction modality cache_state unit pricing_basis price_source ].freeze
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
- #apply_rate(rate) ⇒ Object
- #billable? ⇒ Boolean
- #cost_value ⇒ Object
- #priced? ⇒ Boolean
- #to_h ⇒ Object
- #token? ⇒ Boolean
- #unpriced? ⇒ Boolean
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 51 def self.build(attributes) attributes = attributes.to_h component = component_for(attributes) normalized = { 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), details: attributes[:details] || {} }.merge(optional_attributes_for(attributes)) new(**normalized) end |
.from_token_usage(token_usage) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 72 def self.from_token_usage(token_usage) return [] unless token_usage Components::TOKEN_PRICED.filter_map do |component| quantity = token_usage.public_send(component.token_key) next unless quantity.positive? new( kind: component.kind, direction: component.direction, modality: component.modality, cache_state: component.cache_state, quantity: BigDecimal(quantity.to_s), unit: component.unit, rate_amount: nil, rate_quantity: BigDecimal("1"), cost: nil, currency: USD, cost_status: CostStatus::UNKNOWN, pricing_basis: nil, price_key: nil, price_source: nil, price_source_version: nil, provider_field: nil, provider_item_id: nil, details: {} ) end end |
Instance Method Details
#apply_rate(rate) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 166 def apply_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 |
#billable? ⇒ Boolean
146 147 148 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 146 def billable? quantity.positive? end |
#cost_value ⇒ Object
162 163 164 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 162 def cost_value cost || BigDecimal("0") end |
#priced? ⇒ Boolean
150 151 152 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 150 def priced? [CostStatus::COMPLETE, CostStatus::FREE].include?(cost_status) end |
#to_h ⇒ Object
182 183 184 185 186 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 182 def to_h super.transform_values do |value| value.is_a?(BigDecimal) ? value.to_s("F") : value end end |
#token? ⇒ Boolean
158 159 160 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 158 def token? unit == :token end |
#unpriced? ⇒ Boolean
154 155 156 |
# File 'lib/llm_cost_tracker/billing/line_item.rb', line 154 def unpriced? cost_status == CostStatus::UNKNOWN end |