Class: RubyCode::TokenCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycode/token_counter.rb

Overview

Tracks token usage across LLM requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: 0, output: 0, cached: 0, cache_creation: 0, thinking: 0) ⇒ TokenCounter

Returns a new instance of TokenCounter.



8
9
10
11
12
13
14
# File 'lib/rubycode/token_counter.rb', line 8

def initialize(input: 0, output: 0, cached: 0, cache_creation: 0, thinking: 0)
  @input = input || 0
  @output = output || 0
  @cached = cached || 0
  @cache_creation = cache_creation || 0
  @thinking = thinking || 0
end

Instance Attribute Details

#cache_creation ⇒ Object (readonly)

Returns the value of attribute cache_creation.



6
7
8
# File 'lib/rubycode/token_counter.rb', line 6

def cache_creation
  @cache_creation
end

#cached ⇒ Object (readonly)

Returns the value of attribute cached.



6
7
8
# File 'lib/rubycode/token_counter.rb', line 6

def cached
  @cached
end

#input ⇒ Object (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/rubycode/token_counter.rb', line 6

def input
  @input
end

#output ⇒ Object (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/rubycode/token_counter.rb', line 6

def output
  @output
end

#thinking ⇒ Object (readonly)

Returns the value of attribute thinking.



6
7
8
# File 'lib/rubycode/token_counter.rb', line 6

def thinking
  @thinking
end

Instance Method Details

#+(other) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rubycode/token_counter.rb', line 31

def +(other)
  TokenCounter.new(
    input: input + other.input,
    output: output + other.output,
    cached: cached + other.cached,
    cache_creation: cache_creation + other.cache_creation,
    thinking: thinking + other.thinking
  )
end

#to_h ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubycode/token_counter.rb', line 20

def to_h
  {
    input_tokens: input,
    output_tokens: output,
    cached_tokens: cached,
    cache_creation_tokens: cache_creation,
    thinking_tokens: thinking,
    total_tokens: total
  }.compact
end

#total ⇒ Object



16
17
18
# File 'lib/rubycode/token_counter.rb', line 16

def total
  input + output + thinking
end