class Riffer::Providers::TokenUsage
Normalized token usage for an LLM API call. Buckets carry the same meaning for every provider.
Attributes
Subset of input_tokens read from the provider’s prompt cache, when the provider reports it.
Subset of input_tokens written to the provider’s prompt cache, when the provider reports it.
Cost of the call, set when the model is priced. For observability, not billing.
Number of tokens entering the context window, including cache reads and writes.
Number of tokens generated by the model, including reasoning/thinking tokens.
Public Class Methods
Source
# File lib/riffer/providers/token_usage.rb, line 24 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 44 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
Combines two TokenUsage objects for cumulative tracking.
Source
# File lib/riffer/providers/token_usage.rb, line 57 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
Converts the token usage to a hash; cache tokens and cost are omitted when nil.
Source
# File lib/riffer/providers/token_usage.rb, line 36 def total_tokens input_tokens + output_tokens end
Returns the total number of tokens (input + output).