Class: LlmCostTracker::UsageCapture

Inherits:
Data
  • Object
show all
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

Class Method Summary collapse

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch

Returns:

  • (Object)

    the current value of batch



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def batch
  @batch
end

#line_itemsObject (readonly)

Returns the value of attribute line_items

Returns:

  • (Object)

    the current value of line_items



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def line_items
  @line_items
end

#modelObject (readonly)

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def model
  @model
end

#pricing_modeObject (readonly)

Returns the value of attribute pricing_mode

Returns:

  • (Object)

    the current value of pricing_mode



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def pricing_mode
  @pricing_mode
end

#providerObject (readonly)

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

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



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_idObject (readonly)

Returns the value of attribute provider_project_id

Returns:

  • (Object)

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

Returns the value of attribute provider_response_id

Returns:

  • (Object)

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

Returns the value of attribute provider_workspace_id

Returns:

  • (Object)

    the current value of provider_workspace_id



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def provider_workspace_id
  @provider_workspace_id
end

#streamObject (readonly)

Returns the value of attribute stream

Returns:

  • (Object)

    the current value of stream



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def stream
  @stream
end

#token_usageObject (readonly)

Returns the value of attribute token_usage

Returns:

  • (Object)

    the current value of token_usage



9
10
11
# File 'lib/llm_cost_tracker/usage_capture.rb', line 9

def token_usage
  @token_usage
end

#usage_sourceObject (readonly)

Returns the value of attribute usage_source

Returns:

  • (Object)

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

Returns:

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