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

#costObject (readonly)

Returns the value of attribute cost

Returns:

  • (Object)

    the current value of cost



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

def cost
  @cost
end

#cost_statusObject (readonly)

Returns the value of attribute cost_status

Returns:

  • (Object)

    the current value of cost_status



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

def cost_status
  @cost_status
end

#event_idObject (readonly)

Returns the value of attribute event_id

Returns:

  • (Object)

    the current value of event_id



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

def event_id
  @event_id
end

#latency_msObject (readonly)

Returns the value of attribute latency_ms

Returns:

  • (Object)

    the current value of latency_ms



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

def latency_ms
  @latency_ms
end

#line_itemsObject (readonly)

Returns the value of attribute line_items

Returns:

  • (Object)

    the current value of line_items



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

def line_items
  @line_items
end

#modelObject (readonly)

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



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

def model
  @model
end

#pricing_modeObject (readonly)

Returns the value of attribute pricing_mode

Returns:

  • (Object)

    the current value of pricing_mode



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

def pricing_mode
  @pricing_mode
end

#pricing_snapshotObject (readonly)

Returns the value of attribute pricing_snapshot

Returns:

  • (Object)

    the current value of pricing_snapshot



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

def pricing_snapshot
  @pricing_snapshot
end

#providerObject (readonly)

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



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

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



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

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



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

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



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

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



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

def provider_workspace_id
  @provider_workspace_id
end

#streamObject (readonly)

Returns the value of attribute stream

Returns:

  • (Object)

    the current value of stream



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

def stream
  @stream
end

#tagsObject (readonly)

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



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

def tags
  @tags
end

#token_usageObject (readonly)

Returns the value of attribute token_usage

Returns:

  • (Object)

    the current value of token_usage



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

def token_usage
  @token_usage
end

#tracked_atObject (readonly)

Returns the value of attribute tracked_at

Returns:

  • (Object)

    the current value of tracked_at



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

def tracked_at
  @tracked_at
end

#usage_sourceObject (readonly)

Returns the value of attribute usage_source

Returns:

  • (Object)

    the current value of usage_source



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

def usage_source
  @usage_source
end

Class Method Details

.build(**attributes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/llm_cost_tracker/event.rb', line 26

def self.build(**attributes)
  token_usage = attributes.fetch(:token_usage)
  line_items = attributes[:line_items] || resolve_line_items(attributes[:service_line_items])

  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: attributes[:pricing_mode],
    cost: attributes[:cost],
    tags: attributes[:tags],
    latency_ms: attributes[:latency_ms],
    stream: attributes[:stream] || false,
    usage_source: attributes[:usage_source]&.to_s,
    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,
    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) ⇒ Object



56
57
58
59
60
# File 'lib/llm_cost_tracker/event.rb', line 56

def self.resolve_line_items(service_items)
  Array(service_items).map do |item|
    item.is_a?(Charges::LineItem) ? item : Charges::LineItem.build(item)
  end
end

Instance Method Details

#batch?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/llm_cost_tracker/event.rb', line 52

def batch?
  Pricing::Mode.tokenize(pricing_mode.to_s).include?("batch")
end

#to_hObject



66
67
68
69
70
71
72
73
# File 'lib/llm_cost_tracker/event.rb', line 66

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

#total_costObject



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

def total_cost
  cost&.total
end