Class: Riffer::TokenUsage
- Inherits:
-
Object
- Object
- Riffer::TokenUsage
- Defined in:
- lib/riffer/token_usage.rb
Overview
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
Instance Attribute Summary collapse
-
#cache_creation_tokens ⇒ Object
readonly
Number of tokens written to cache (Anthropic-specific).
-
#cache_read_tokens ⇒ Object
readonly
Number of tokens read from cache (Anthropic-specific).
-
#input_tokens ⇒ Object
readonly
Number of tokens in the input/prompt.
-
#output_tokens ⇒ Object
readonly
Number of tokens in the output/response.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Combines two TokenUsage objects for cumulative tracking.
-
#initialize(input_tokens:, output_tokens:, cache_creation_tokens: nil, cache_read_tokens: nil) ⇒ TokenUsage
constructor
Creates a new TokenUsage instance.
-
#to_h ⇒ Object
Converts the token usage to a hash representation.
-
#total_tokens ⇒ Object
Returns the total number of tokens (input + output).
Constructor Details
#initialize(input_tokens:, output_tokens:, cache_creation_tokens: nil, cache_read_tokens: nil) ⇒ TokenUsage
Creates a new TokenUsage instance.
- input_tokens
-
Integer - number of input tokens
- output_tokens
-
Integer - number of output tokens
- cache_creation_tokens
-
Integer or nil - tokens written to cache
- cache_read_tokens
-
Integer or nil - tokens read from cache
39 40 41 42 43 44 |
# File 'lib/riffer/token_usage.rb', line 39 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 |
Instance Attribute Details
#cache_creation_tokens ⇒ Object (readonly)
Number of tokens written to cache (Anthropic-specific).
Returns Integer or nil.
26 27 28 |
# File 'lib/riffer/token_usage.rb', line 26 def cache_creation_tokens @cache_creation_tokens end |
#cache_read_tokens ⇒ Object (readonly)
Number of tokens read from cache (Anthropic-specific).
Returns Integer or nil.
31 32 33 |
# File 'lib/riffer/token_usage.rb', line 31 def cache_read_tokens @cache_read_tokens end |
#input_tokens ⇒ Object (readonly)
Number of tokens in the input/prompt.
Returns Integer.
16 17 18 |
# File 'lib/riffer/token_usage.rb', line 16 def input_tokens @input_tokens end |
#output_tokens ⇒ Object (readonly)
Number of tokens in the output/response.
Returns Integer.
21 22 23 |
# File 'lib/riffer/token_usage.rb', line 21 def output_tokens @output_tokens end |
Instance Method Details
#+(other) ⇒ Object
Combines two TokenUsage objects for cumulative tracking.
- other
-
Riffer::TokenUsage - another token usage object to combine with
Returns Riffer::TokenUsage - a new TokenUsage with summed values.
58 59 60 61 62 63 64 65 |
# File 'lib/riffer/token_usage.rb', line 58 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 |
#to_h ⇒ Object
Converts the token usage to a hash representation.
Cache tokens are omitted if nil.
Returns Hash.
72 73 74 75 76 77 |
# File 'lib/riffer/token_usage.rb', line 72 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 |
#total_tokens ⇒ Object
Returns the total number of tokens (input + output).
Returns Integer.
49 50 51 |
# File 'lib/riffer/token_usage.rb', line 49 def total_tokens input_tokens + output_tokens end |