Class: Riffer::Providers::TokenUsage
- Inherits:
-
Object
- Object
- Riffer::Providers::TokenUsage
- Defined in:
- lib/riffer/providers/token_usage.rb
Overview
Represents token usage data from an LLM API call.
Instance Attribute Summary collapse
-
#cache_read_tokens ⇒ Object
readonly
Number of tokens read from cache, when the provider reports it.
-
#cache_write_tokens ⇒ Object
readonly
Number of tokens written to cache, when the provider reports it.
-
#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_write_tokens: nil, cache_read_tokens: nil) ⇒ TokenUsage
constructor
– : (input_tokens: Integer, output_tokens: Integer, ?cache_write_tokens: Integer?, ?cache_read_tokens: Integer?) -> void.
-
#to_h ⇒ Object
Converts the token usage to a hash; cache tokens are omitted when nil.
-
#total_tokens ⇒ Object
Returns the total number of tokens (input + output).
Constructor Details
#initialize(input_tokens:, output_tokens:, cache_write_tokens: nil, cache_read_tokens: nil) ⇒ TokenUsage
– : (input_tokens: Integer, output_tokens: Integer, ?cache_write_tokens: Integer?, ?cache_read_tokens: Integer?) -> void
20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#cache_read_tokens ⇒ Object (readonly)
Number of tokens read from cache, when the provider reports it.
16 17 18 |
# File 'lib/riffer/providers/token_usage.rb', line 16 def cache_read_tokens @cache_read_tokens end |
#cache_write_tokens ⇒ Object (readonly)
Number of tokens written to cache, when the provider reports it.
13 14 15 |
# File 'lib/riffer/providers/token_usage.rb', line 13 def cache_write_tokens @cache_write_tokens end |
#input_tokens ⇒ Object (readonly)
Number of tokens in the input/prompt.
7 8 9 |
# File 'lib/riffer/providers/token_usage.rb', line 7 def input_tokens @input_tokens end |
#output_tokens ⇒ Object (readonly)
Number of tokens in the output/response.
10 11 12 |
# File 'lib/riffer/providers/token_usage.rb', line 10 def output_tokens @output_tokens end |
Instance Method Details
#+(other) ⇒ Object
Combines two TokenUsage objects for cumulative tracking.
– : (Riffer::Providers::TokenUsage) -> Riffer::Providers::TokenUsage
39 40 41 42 43 44 45 46 |
# 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 |
#to_h ⇒ Object
Converts the token usage to a hash; cache tokens are omitted when nil. – : () -> Hash[Symbol, Integer]
51 52 53 54 55 56 |
# 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 |
#total_tokens ⇒ Object
Returns the total number of tokens (input + output).
– : () -> Integer
31 32 33 |
# File 'lib/riffer/providers/token_usage.rb', line 31 def total_tokens input_tokens + output_tokens end |