class Riffer::Providers::TokenUsage
Represents token usage data from an LLM API call.
Attributes
Number of tokens read from cache, when the provider reports it.
Number of tokens written to cache, when the provider reports it.
Number of tokens in the input/prompt.
Number of tokens in the output/response.
Public Class Methods
Source
# File lib/riffer/providers/token_usage.rb, line 20 def initialize(input_tokens:, output_tokens:, cache_write_tokens: nil, cache_read_tokens: nil) @input_tokens = input_tokens @output_tokens = output_tokens @cache_write_tokens = cache_write_tokens @cache_read_tokens = cache_read_tokens end
Public Instance Methods
Source
# File lib/riffer/providers/token_usage.rb, line 39 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) ) end
Combines two TokenUsage objects for cumulative tracking.
Source
# File lib/riffer/providers/token_usage.rb, line 51 def to_h hash = {input_tokens: input_tokens, output_tokens: output_tokens} hash[:cache_write_tokens] = cache_write_tokens if cache_write_tokens hash[:cache_read_tokens] = cache_read_tokens if cache_read_tokens hash end
Converts the token usage to a hash; cache tokens are omitted when nil.
Source
# File lib/riffer/providers/token_usage.rb, line 31 def total_tokens input_tokens + output_tokens end
Returns the total number of tokens (input + output).