Class: RubyLLM::Tokens

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/tokens.rb

Overview

Represents token usage for a response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: nil, output: nil, cached: nil, cache_creation: nil, thinking: nil, reasoning: nil) ⇒ Tokens

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
15
# File 'lib/ruby_llm/tokens.rb', line 9

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

Instance Attribute Details

#cache_creationObject (readonly)

Returns the value of attribute cache_creation.



6
7
8
# File 'lib/ruby_llm/tokens.rb', line 6

def cache_creation
  @cache_creation
end

#cachedObject (readonly)

Returns the value of attribute cached.



6
7
8
# File 'lib/ruby_llm/tokens.rb', line 6

def cached
  @cached
end

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/ruby_llm/tokens.rb', line 6

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/ruby_llm/tokens.rb', line 6

def output
  @output
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



6
7
8
# File 'lib/ruby_llm/tokens.rb', line 6

def thinking
  @thinking
end

Class Method Details

.build(input: nil, output: nil, cached: nil, cache_creation: nil, thinking: nil, reasoning: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/tokens.rb', line 19

def self.build(input: nil, output: nil, cached: nil, cache_creation: nil, thinking: nil, reasoning: nil)
  return nil if [input, output, cached, cache_creation, thinking, reasoning].all?(&:nil?)

  new(
    input: input,
    output: output,
    cached: cached,
    cache_creation: cache_creation,
    thinking: thinking,
    reasoning: reasoning
  )
end

Instance Method Details

#reasoningObject



43
44
45
# File 'lib/ruby_llm/tokens.rb', line 43

def reasoning
  thinking
end

#to_hObject

rubocop:enable Metrics/ParameterLists



33
34
35
36
37
38
39
40
41
# File 'lib/ruby_llm/tokens.rb', line 33

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