Module: LlmCostTracker::TokenUsageHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/llm_cost_tracker/token_usage_helper.rb

Constant Summary collapse

COMPONENT_LABELS =
{
  input_tokens: "Input",
  cache_read_input_tokens: "Cache read",
  cache_write_input_tokens: "Cache write",
  cache_write_extended_input_tokens: "Extended cache write",
  audio_input_tokens: "Audio input",
  output_tokens: "Output",
  audio_output_tokens: "Audio output",
  hidden_output_tokens: "Hidden output"
}.freeze
QUALITY_LABELS =
COMPONENT_LABELS.merge(
  input_tokens: "Regular input",
  cache_read_input_tokens: "Cache read input",
  cache_write_input_tokens: "Cache write input",
  cache_write_extended_input_tokens: "Extended cache write input"
).freeze
STACK_CLASSES =
{
  input_tokens: "lct-stack-fill-input",
  cache_read_input_tokens: "lct-stack-fill-cache-read",
  cache_write_input_tokens: "lct-stack-fill-cache-write",
  cache_write_extended_input_tokens: "lct-stack-fill-cache-write-extended",
  audio_input_tokens: "lct-stack-fill-audio-input",
  output_tokens: "lct-stack-fill-output",
  audio_output_tokens: "lct-stack-fill-audio-output"
}.freeze

Instance Method Summary collapse

Instance Method Details

#call_line_item_costs_by_component(call) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/llm_cost_tracker/token_usage_helper.rb', line 37

def call_line_item_costs_by_component(call)
  call.line_items.each_with_object({}) do |line_item, accumulator|
    component = LlmCostTracker::Billing::Components::TOKEN_PRICED.find do |item|
      item.kind.to_s == line_item.kind.to_s &&
        item.direction.to_s == line_item.direction.to_s &&
        item.cache_state.to_s == line_item.cache_state.to_s
    end
    accumulator[component.key] = line_item.cost if component && line_item.cost
  end
end

#token_usage_stack_componentsObject



31
32
33
34
35
# File 'app/helpers/llm_cost_tracker/token_usage_helper.rb', line 31

def token_usage_stack_components
  token_usage_display_components(labels: COMPONENT_LABELS).select do |component|
    component.fetch(:cost_key)
  end
end