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
# File 'lib/brute/usage_detection/ruby_llm.rb', line 13

def self.detect(message)
  return nil unless message.respond_to?(:tokens)

  tokens = message.tokens
  return nil if tokens.nil?

  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

.read(tokens, name) ⇒ Object



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

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