Class: LlmCostTracker::UsageCapture
- Inherits:
-
Data
- Object
- Data
- LlmCostTracker::UsageCapture
- Defined in:
- lib/llm_cost_tracker/usage_capture.rb,
lib/llm_cost_tracker/usage_capture.rb
Constant Summary collapse
- UNKNOWN_MODEL =
"unknown"
Instance Attribute Summary collapse
-
#batch ⇒ Object
readonly
Returns the value of attribute batch.
-
#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.
-
#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.
-
#token_usage ⇒ Object
readonly
Returns the value of attribute token_usage.
-
#usage_source ⇒ Object
readonly
Returns the value of attribute usage_source.
Class Method Summary collapse
Instance Attribute Details
#batch ⇒ Object (readonly)
Returns the value of attribute batch
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def batch @batch end |
#line_items ⇒ Object (readonly)
Returns the value of attribute line_items
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def line_items @line_items end |
#model ⇒ Object (readonly)
Returns the value of attribute model
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def model @model end |
#pricing_mode ⇒ Object (readonly)
Returns the value of attribute pricing_mode
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def pricing_mode @pricing_mode end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def provider @provider end |
#provider_api_key_id ⇒ Object (readonly)
Returns the value of attribute provider_api_key_id
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def provider_api_key_id @provider_api_key_id end |
#provider_project_id ⇒ Object (readonly)
Returns the value of attribute provider_project_id
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def provider_project_id @provider_project_id end |
#provider_response_id ⇒ Object (readonly)
Returns the value of attribute provider_response_id
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def provider_response_id @provider_response_id end |
#provider_workspace_id ⇒ Object (readonly)
Returns the value of attribute provider_workspace_id
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def provider_workspace_id @provider_workspace_id end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def stream @stream end |
#token_usage ⇒ Object (readonly)
Returns the value of attribute token_usage
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def token_usage @token_usage end |
#usage_source ⇒ Object (readonly)
Returns the value of attribute usage_source
9 10 11 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9 def usage_source @usage_source end |
Class Method Details
.batch_from_pricing_mode?(pricing_mode) ⇒ Boolean
27 28 29 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 27 def self.batch_from_pricing_mode?(pricing_mode) pricing_mode.to_s.split("_").include?("batch") end |
.build(**attributes) ⇒ Object
31 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 |
# File 'lib/llm_cost_tracker/usage_capture.rb', line 31 def self.build(**attributes) pricing_mode = Pricing.normalize_mode(attributes[:pricing_mode]) batch = attributes[:batch] batch = batch_from_pricing_mode?(pricing_mode) if batch.nil? token_usage = attributes.fetch(:token_usage) service_line_items = Array(attributes[:service_line_items]).map do |item| item.is_a?(Billing::LineItem) ? item : Billing::LineItem.build(item) end line_items = attributes[:line_items] || (Billing::LineItem.from_token_usage(token_usage) + service_line_items) new( provider: attributes.fetch(:provider).to_s, model: attributes.fetch(:model).to_s.strip.presence || UNKNOWN_MODEL, token_usage: token_usage, 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, pricing_mode: pricing_mode, line_items: line_items ) end |