class Riffer::Providers::TokenUsage
rbs_inline: enabled
Attributes
Subset of input_tokens.
Subset of input_tokens.
For observability, not billing.
Normalized across providers: includes cache reads and writes.
Includes reasoning/thinking tokens.
Public Class Methods
Source
# File lib/riffer/providers/token_usage.rb, line 7 def self.from_hash(usage) return usage if usage.is_a?(Riffer::Providers::TokenUsage) new( input_tokens: usage[:input_tokens], output_tokens: usage[:output_tokens], cache_write_tokens: usage[:cache_write_tokens], cache_read_tokens: usage[:cache_read_tokens], cost: usage[:cost], ) end
Source
# File lib/riffer/providers/token_usage.rb, line 36 def initialize(input_tokens:, output_tokens:, cache_write_tokens: nil, cache_read_tokens: nil, cost: nil) @input_tokens = input_tokens @output_tokens = output_tokens @cache_write_tokens = cache_write_tokens @cache_read_tokens = cache_read_tokens @cost = cost end
Public Instance Methods
Source
# File lib/riffer/providers/token_usage.rb, line 52 def +(other) Riffer::Providers::TokenUsage.new( input_tokens: input_tokens + other.input_tokens, output_tokens: output_tokens + other.output_tokens, cache_write_tokens: add_nullable(cache_write_tokens, other.cache_write_tokens), cache_read_tokens: add_nullable(cache_read_tokens, other.cache_read_tokens), cost: add_cost(cost, other.cost), ) end
Source
# File lib/riffer/providers/token_usage.rb, line 64 def to_h hash = { input_tokens: input_tokens, output_tokens: output_tokens } #: Hash[Symbol, (Integer | Float)] hash[:cache_write_tokens] = cache_write_tokens if cache_write_tokens hash[:cache_read_tokens] = cache_read_tokens if cache_read_tokens hash[:cost] = cost if cost hash end
Source
# File lib/riffer/providers/token_usage.rb, line 46 def total_tokens input_tokens + output_tokens end