Module: Brute::UsageDetection::OpenRouter

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

Overview

OpenRouter reports usage as the provider's raw hash on the response, in OpenAI's wire shape. Reasoning tokens, when the model reports them, arrive nested under completion_tokens_details.

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
29
30
31
32
33
34
# File 'lib/brute/usage_detection/open_router.rb', line 13

def self.detect(response)
  if response.respond_to?(:usage)
    usage = response.usage
    if usage.nil?
      nil
    else
      usage = usage.to_h
      details = (usage["completion_tokens_details"] || usage[:completion_tokens_details] || {}).to_h
      Usage.new(
        input:      field(usage, "prompt_tokens"),
        output:     field(usage, "completion_tokens"),
        total:      field(usage, "total_tokens"),
        reasoning:  field(details, "reasoning_tokens"),
        cache_read: field(usage, "cached_tokens"),
        cost:       field(usage, "cost"),
        raw:        usage,
      )
    end
  else
    nil
  end
end

.field(hash, key) ⇒ Object

OpenRouter hands back string keys; a hand-built response may not.



37
# File 'lib/brute/usage_detection/open_router.rb', line 37

def self.field(hash, key) = hash[key] || hash[key.to_sym]