Module: Brute::UsageDetection::RubyLLM

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

Overview

ruby_llm hangs usage off the message, not the response: a Tokens object with input/output/cache_read/cache_write/thinking, plus the provider's own reported_cost when it gives one.

Class Method Summary collapse

Class Method Details

.detect(message) ⇒ Object



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

def self.detect(message)
  if message.respond_to?(:tokens)
    tokens = message.tokens
    if tokens.nil?
      nil
    else
      Usage.new(
        input:       read(tokens, :input),
        output:      read(tokens, :output),
        reasoning:   read(tokens, :thinking),
        cache_read:  read(tokens, :cache_read),
        cache_write: read(tokens, :cache_write),
        cost:        read(tokens, :reported_cost),
        raw:         tokens,
      )
    end
  else
    nil
  end
end

.read(tokens, name) ⇒ Object



34
# File 'lib/brute/usage_detection/ruby_llm.rb', line 34

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