Class: LlmCostTracker::Event
- Inherits:
-
Data
- Object
- Data
- LlmCostTracker::Event
- Defined in:
- lib/llm_cost_tracker/event.rb,
lib/llm_cost_tracker/event.rb
Constant Summary collapse
- UNKNOWN_MODEL =
"unknown"
Instance Attribute Summary collapse
-
#batch ⇒ Object
readonly
Returns the value of attribute batch.
-
#cost ⇒ Object
readonly
Returns the value of attribute cost.
-
#cost_status ⇒ Object
readonly
Returns the value of attribute cost_status.
-
#event_id ⇒ Object
readonly
Returns the value of attribute event_id.
-
#latency_ms ⇒ Object
readonly
Returns the value of attribute latency_ms.
-
#line_items ⇒ Object
readonly
Returns the value of attribute line_items.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#pricing_mode ⇒ Object
readonly
Returns the value of attribute pricing_mode.
-
#pricing_snapshot ⇒ Object
readonly
Returns the value of attribute pricing_snapshot.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#provider_api_key_id ⇒ Object
readonly
Returns the value of attribute provider_api_key_id.
-
#provider_project_id ⇒ Object
readonly
Returns the value of attribute provider_project_id.
-
#provider_response_id ⇒ Object
readonly
Returns the value of attribute provider_response_id.
-
#provider_workspace_id ⇒ Object
readonly
Returns the value of attribute provider_workspace_id.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#token_usage ⇒ Object
readonly
Returns the value of attribute token_usage.
-
#tracked_at ⇒ Object
readonly
Returns the value of attribute tracked_at.
-
#usage_source ⇒ Object
readonly
Returns the value of attribute usage_source.
Class Method Summary collapse
- .batch_from_pricing_mode?(pricing_mode) ⇒ Boolean
- .build(**attributes) ⇒ Object
- .resolve_line_items(service_items, token_usage) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#batch ⇒ Object (readonly)
Returns the value of attribute batch
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def batch @batch end |
#cost ⇒ Object (readonly)
Returns the value of attribute cost
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def cost @cost end |
#cost_status ⇒ Object (readonly)
Returns the value of attribute cost_status
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def cost_status @cost_status end |
#event_id ⇒ Object (readonly)
Returns the value of attribute event_id
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def event_id @event_id end |
#latency_ms ⇒ Object (readonly)
Returns the value of attribute latency_ms
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def latency_ms @latency_ms end |
#line_items ⇒ Object (readonly)
Returns the value of attribute line_items
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def line_items @line_items end |
#model ⇒ Object (readonly)
Returns the value of attribute model
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def model @model end |
#pricing_mode ⇒ Object (readonly)
Returns the value of attribute pricing_mode
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def pricing_mode @pricing_mode end |
#pricing_snapshot ⇒ Object (readonly)
Returns the value of attribute pricing_snapshot
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def pricing_snapshot @pricing_snapshot end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def provider @provider end |
#provider_api_key_id ⇒ Object (readonly)
Returns the value of attribute 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_id ⇒ Object (readonly)
Returns the value of attribute 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_id ⇒ Object (readonly)
Returns the value of attribute 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_id ⇒ Object (readonly)
Returns the value of attribute provider_workspace_id
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def provider_workspace_id @provider_workspace_id end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def stream @stream end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def @tags end |
#token_usage ⇒ Object (readonly)
Returns the value of attribute token_usage
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def token_usage @token_usage end |
#tracked_at ⇒ Object (readonly)
Returns the value of attribute tracked_at
7 8 9 |
# File 'lib/llm_cost_tracker/event.rb', line 7 def tracked_at @tracked_at end |
#usage_source ⇒ Object (readonly)
Returns the value of attribute 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
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.normalize_mode(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_h ⇒ Object
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: ? .to_h : {}, line_items: (line_items || []).map(&:to_h) ) end |
#total_cost ⇒ Object
68 69 70 |
# File 'lib/llm_cost_tracker/event.rb', line 68 def total_cost cost&.fetch(:total_cost, nil) end |