Class: LlmCostTracker::Charges::LineItem

Inherits:
Data
  • Object
show all
Defined in:
lib/llm_cost_tracker/charges/line_item.rb,
lib/llm_cost_tracker/charges/line_item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_stateObject (readonly)

Returns the value of attribute cache_state

Returns:

  • (Object)

    the current value of cache_state



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def cache_state
  @cache_state
end

#costObject (readonly)

Returns the value of attribute cost

Returns:

  • (Object)

    the current value of cost



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def cost
  @cost
end

#cost_statusObject (readonly)

Returns the value of attribute cost_status

Returns:

  • (Object)

    the current value of cost_status



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def cost_status
  @cost_status
end

#currencyObject (readonly)

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def currency
  @currency
end

#detailsObject (readonly)

Returns the value of attribute details

Returns:

  • (Object)

    the current value of details



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def details
  @details
end

#directionObject (readonly)

Returns the value of attribute direction

Returns:

  • (Object)

    the current value of direction



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def direction
  @direction
end

#kindObject (readonly)

Returns the value of attribute kind

Returns:

  • (Object)

    the current value of kind



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def kind
  @kind
end

#modalityObject (readonly)

Returns the value of attribute modality

Returns:

  • (Object)

    the current value of modality



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def modality
  @modality
end

#price_keyObject (readonly)

Returns the value of attribute price_key

Returns:

  • (Object)

    the current value of price_key



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def price_key
  @price_key
end

#price_sourceObject (readonly)

Returns the value of attribute price_source

Returns:

  • (Object)

    the current value of price_source



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def price_source
  @price_source
end

#price_source_versionObject (readonly)

Returns the value of attribute price_source_version

Returns:

  • (Object)

    the current value of 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_basisObject (readonly)

Returns the value of attribute pricing_basis

Returns:

  • (Object)

    the current value of pricing_basis



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def pricing_basis
  @pricing_basis
end

#provider_fieldObject (readonly)

Returns the value of attribute provider_field

Returns:

  • (Object)

    the current value of provider_field



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def provider_field
  @provider_field
end

#provider_item_idObject (readonly)

Returns the value of attribute provider_item_id

Returns:

  • (Object)

    the current value of 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

#quantityObject (readonly)

Returns the value of attribute quantity

Returns:

  • (Object)

    the current value of quantity



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def quantity
  @quantity
end

#rate_amountObject (readonly)

Returns the value of attribute rate_amount

Returns:

  • (Object)

    the current value of rate_amount



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def rate_amount
  @rate_amount
end

#rate_quantityObject (readonly)

Returns the value of attribute rate_quantity

Returns:

  • (Object)

    the current value of rate_quantity



11
12
13
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 11

def rate_quantity
  @rate_quantity
end

#unitObject (readonly)

Returns the value of attribute unit

Returns:

  • (Object)

    the current value of 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

Returns:

  • (Boolean)


105
106
107
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 105

def billable?
  quantity.positive?
end

#cost_valueObject



126
127
128
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 126

def cost_value
  cost || BigDecimal("0")
end

#dimensionObject



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

Returns:

  • (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_hObject



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

Returns:

  • (Boolean)


117
118
119
# File 'lib/llm_cost_tracker/charges/line_item.rb', line 117

def token?
  unit == "token"
end

#unpriced?Boolean

Returns:

  • (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