Module: Brute::UsageDetection::LLMrb

Defined in:
lib/brute/usage_detection/llmrb.rb

Overview

llm.rb models usage as its own LLM::Usage value object. Note it answers 0, not nil, for anything the provider left out — that is llm.rb's reading of the response and it is reported as given.

Class Method Summary collapse

Class Method Details

.detect(response) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brute/usage_detection/llmrb.rb', line 13

def self.detect(response)
  return nil unless response.respond_to?(:usage)

  usage = response.usage
  return nil if usage.nil?

  Usage.new(
    input:       read(usage, :input_tokens),
    output:      read(usage, :output_tokens),
    total:       read(usage, :total_tokens),
    reasoning:   read(usage, :reasoning_tokens),
    cache_read:  read(usage, :cache_read_tokens),
    cache_write: read(usage, :cache_write_tokens),
    raw:         usage,
  )
end

.read(usage, name) ⇒ Object



30
# File 'lib/brute/usage_detection/llmrb.rb', line 30

def self.read(usage, name) = usage.respond_to?(name) ? usage.public_send(name) : nil