Class: LlmCostTracker::Event

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

Constant Summary collapse

UNKNOWN_MODEL =
"unknown"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch

Returns:

  • (Object)

    the current value of batch



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def batch
  @batch
end

#costObject (readonly)

Returns the value of attribute cost

Returns:

  • (Object)

    the current value of cost



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def cost
  @cost
end

#cost_statusObject (readonly)

Returns the value of attribute cost_status

Returns:

  • (Object)

    the current value of cost_status



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def cost_status
  @cost_status
end

#event_idObject (readonly)

Returns the value of attribute event_id

Returns:

  • (Object)

    the current value of event_id



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def event_id
  @event_id
end

#latency_msObject (readonly)

Returns the value of attribute latency_ms

Returns:

  • (Object)

    the current value of latency_ms



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def latency_ms
  @latency_ms
end

#line_itemsObject (readonly)

Returns the value of attribute line_items

Returns:

  • (Object)

    the current value of line_items



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def line_items
  @line_items
end

#modelObject (readonly)

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def model
  @model
end

#pricing_modeObject (readonly)

Returns the value of attribute pricing_mode

Returns:

  • (Object)

    the current value of pricing_mode



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def pricing_mode
  @pricing_mode
end

#pricing_snapshotObject (readonly)

Returns the value of attribute pricing_snapshot

Returns:

  • (Object)

    the current value of pricing_snapshot



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def pricing_snapshot
  @pricing_snapshot
end

#providerObject (readonly)

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def provider
  @provider
end

#provider_api_key_idObject (readonly)

Returns the value of attribute provider_api_key_id

Returns:

  • (Object)

    the current value of provider_api_key_id



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def provider_api_key_id
  @provider_api_key_id
end

#provider_project_idObject (readonly)

Returns the value of attribute provider_project_id

Returns:

  • (Object)

    the current value of provider_project_id



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def provider_project_id
  @provider_project_id
end

#provider_response_idObject (readonly)

Returns the value of attribute provider_response_id

Returns:

  • (Object)

    the current value of provider_response_id



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def provider_response_id
  @provider_response_id
end

#provider_workspace_idObject (readonly)

Returns the value of attribute provider_workspace_id

Returns:

  • (Object)

    the current value of provider_workspace_id



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def provider_workspace_id
  @provider_workspace_id
end

#streamObject (readonly)

Returns the value of attribute stream

Returns:

  • (Object)

    the current value of stream



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def stream
  @stream
end

#tagsObject (readonly)

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def tags
  @tags
end

#token_usageObject (readonly)

Returns the value of attribute token_usage

Returns:

  • (Object)

    the current value of token_usage



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def token_usage
  @token_usage
end

#tracked_atObject (readonly)

Returns the value of attribute tracked_at

Returns:

  • (Object)

    the current value of tracked_at



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def tracked_at
  @tracked_at
end

#usage_sourceObject (readonly)

Returns the value of attribute usage_source

Returns:

  • (Object)

    the current value of usage_source



7
8
9
# File 'lib/llm_cost_tracker/event.rb', line 7

def usage_source
  @usage_source
end

Class Method Details

.batch_from_pricing_mode?(pricing_mode) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/llm_cost_tracker/event.rb', line 28

def self.batch_from_pricing_mode?(pricing_mode)
  pricing_mode.to_s.split("_").include?("batch")
end

.build(**attributes) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/llm_cost_tracker/event.rb', line 32

def self.build(**attributes)
  pricing_mode = Pricing::Mode.normalize(attributes[:pricing_mode])
  token_usage = attributes.fetch(:token_usage)
  batch = attributes[:batch].nil? ? batch_from_pricing_mode?(pricing_mode) : attributes[:batch]
  line_items = attributes[:line_items] || resolve_line_items(attributes[:service_line_items], token_usage)

  new(
    event_id: attributes[:event_id],
    provider: attributes.fetch(:provider).to_s,
    model: attributes.fetch(:model).to_s.strip.presence || Event::UNKNOWN_MODEL,
    token_usage: token_usage,
    pricing_mode: pricing_mode,
    cost: attributes[:cost],
    tags: attributes[:tags],
    latency_ms: attributes[:latency_ms],
    stream: attributes[:stream] || false,
    usage_source: attributes[:usage_source],
    provider_response_id: attributes[:provider_response_id].to_s.strip.presence,
    provider_project_id: attributes[:provider_project_id].to_s.strip.presence,
    provider_api_key_id: attributes[:provider_api_key_id].to_s.strip.presence,
    provider_workspace_id: attributes[:provider_workspace_id].to_s.strip.presence,
    batch: batch,
    tracked_at: attributes[:tracked_at],
    cost_status: attributes[:cost_status],
    pricing_snapshot: attributes[:pricing_snapshot],
    line_items: line_items
  )
end

.resolve_line_items(service_items, token_usage) ⇒ Object



61
62
63
64
65
66
# File 'lib/llm_cost_tracker/event.rb', line 61

def self.resolve_line_items(service_items, token_usage)
  service_line_items = Array(service_items).map do |item|
    item.is_a?(Billing::LineItem) ? item : Billing::LineItem.build(item)
  end
  Billing::LineItem.from_token_usage(token_usage) + service_line_items
end

Instance Method Details

#to_hObject



72
73
74
75
76
77
78
79
# File 'lib/llm_cost_tracker/event.rb', line 72

def to_h
  super.merge(
    token_usage: token_usage.to_h,
    cost: cost && cost.to_h.transform_values { |v| v.is_a?(BigDecimal) ? v.to_f : v },
    tags: tags ? tags.to_h : {},
    line_items: (line_items || []).map(&:to_h)
  )
end

#total_costObject



68
69
70
# File 'lib/llm_cost_tracker/event.rb', line 68

def total_cost
  cost&.fetch(:total_cost, nil)
end