Module: Admin::AiUsageLogsHelper

Defined in:
app/helpers/admin/ai_usage_logs_helper.rb

Overview

View helpers for the AI Usage & Cost dashboard.

Instance Method Summary collapse

Instance Method Details

#ai_usage_cache_cell(row) ⇒ ActiveSupport::SafeBuffer

Prompt-cache hit rate cell for a rollup row. Renders "—" when the row
billed no cache tokens at all, so features on providers without prompt
caching (or models we never send cache_control to) read as "not
applicable" rather than as a 0% hit rate to go fix.

Parameters:

  • row (Hash)

    a @by_feature / @by_model rollup row

Returns:

  • (ActiveSupport::SafeBuffer)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/admin/ai_usage_logs_helper.rb', line 27

def ai_usage_cache_cell(row)
  cached = row[:cached_tokens].to_i
  written = row[:cache_write_tokens].to_i
  return tag.span('', class: 'text-body-secondary') if cached.zero? && written.zero?

  tag.span(
    number_to_percentage(row[:cache_hit_pct], precision: 1),
    class: row[:cache_hit_pct] >= 50 ? 'text-success' : 'text-warning',
    title: "#{number_with_delimiter(cached)} cached read / " \
           "#{number_with_delimiter(written)} cache write / " \
           "#{number_with_delimiter(row[:input_tokens].to_i)} fresh input"
  )
end

#ai_usage_unpriced_tooltip(row) ⇒ String

Human explanation of why some/all calls in a rollup row are unpriced,
split into the two distinct causes so the dashboard stops conflating
"we never captured token usage" with "we have no price for this model".

Parameters:

  • row (Hash)

    a @by_feature / @by_model rollup row

Returns:

  • (String)

    tooltip text



12
13
14
15
16
17
18
# File 'app/helpers/admin/ai_usage_logs_helper.rb', line 12

def ai_usage_unpriced_tooltip(row)
  reasons = []
  reasons << "#{number_with_delimiter(row[:no_token_count])} missing captured usage (streaming/tool turns)" if row[:no_token_count].to_i.positive?
  reasons << "#{number_with_delimiter(row[:no_price_count])} missing a model price" if row[:no_price_count].to_i.positive?

  "#{number_with_delimiter(row[:null_cost_count])} of #{number_with_delimiter(row[:calls])} calls unpriced — #{reasons.join('; ')}"
end