class Riffer::TokenUsage
Represents token usage data from an LLM API call.
Tracks input tokens, output tokens, and optional cache statistics.
token_usage = Riffer::TokenUsage.new(input_tokens: 100, output_tokens: 50) token_usage.total_tokens # => 150
combined = token_usage1 + token_usage2 # Combine multiple token usage objects
Attributes
Number of tokens written to cache (Anthropic-specific).
Number of tokens read from cache (Anthropic-specific).
Number of tokens in the input/prompt.
Number of tokens in the output/response.
Public Class Methods
Source
# File lib/riffer/token_usage.rb, line 27 def initialize(input_tokens:, output_tokens:, cache_creation_tokens: nil, cache_read_tokens: nil) @input_tokens = input_tokens @output_tokens = output_tokens @cache_creation_tokens = cache_creation_tokens @cache_read_tokens = cache_read_tokens end
: (input_tokens: Integer, output_tokens: Integer, ?cache_creation_tokens: Integer?, ?cache_read_tokens: Integer?) -> void
Public Instance Methods
Source
# File lib/riffer/token_usage.rb, line 44 def +(other) Riffer::TokenUsage.new( input_tokens: input_tokens + other.input_tokens, output_tokens: output_tokens + other.output_tokens, cache_creation_tokens: add_nullable(cache_creation_tokens, other.cache_creation_tokens), cache_read_tokens: add_nullable(cache_read_tokens, other.cache_read_tokens) ) end
Combines two TokenUsage objects for cumulative tracking.
Source
# File lib/riffer/token_usage.rb, line 58 def to_h hash = {input_tokens: input_tokens, output_tokens: output_tokens} hash[:cache_creation_tokens] = cache_creation_tokens if cache_creation_tokens hash[:cache_read_tokens] = cache_read_tokens if cache_read_tokens hash end
Converts the token usage to a hash representation.
Cache tokens are omitted if nil.
: () -> Hash[Symbol, Integer]
Source
# File lib/riffer/token_usage.rb, line 37 def total_tokens input_tokens + output_tokens end
Returns the total number of tokens (input + output).
: () -> Integer