Class: LLM::Usage

Inherits:
Struct
  • Object
show all
Defined in:
lib/llm/usage.rb

Overview

The LLM::Usage class represents token usage for a given conversation or completion. As a conversation grows, so does the number of tokens used. This class helps track the number of input, output, reasoning, cache, and overall token count. It can also help track usage of the context window (which may vary by model).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_read_tokensObject

Returns the value of attribute cache_read_tokens

Returns:

  • (Object)

    the current value of cache_read_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def cache_read_tokens
  @cache_read_tokens
end

#cache_write_tokensObject

Returns the value of attribute cache_write_tokens

Returns:

  • (Object)

    the current value of cache_write_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def cache_write_tokens
  @cache_write_tokens
end

#input_audio_tokensObject

Returns the value of attribute input_audio_tokens

Returns:

  • (Object)

    the current value of input_audio_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def input_audio_tokens
  @input_audio_tokens
end

#input_image_tokensObject

Returns the value of attribute input_image_tokens

Returns:

  • (Object)

    the current value of input_image_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def input_image_tokens
  @input_image_tokens
end

#input_tokensObject

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def input_tokens
  @input_tokens
end

#output_audio_tokensObject

Returns the value of attribute output_audio_tokens

Returns:

  • (Object)

    the current value of output_audio_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def output_audio_tokens
  @output_audio_tokens
end

#output_tokensObject

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def output_tokens
  @output_tokens
end

#reasoning_tokensObject

Returns the value of attribute reasoning_tokens

Returns:

  • (Object)

    the current value of reasoning_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def reasoning_tokens
  @reasoning_tokens
end

#total_tokensObject

Returns the value of attribute total_tokens

Returns:

  • (Object)

    the current value of total_tokens



10
11
12
# File 'lib/llm/usage.rb', line 10

def total_tokens
  @total_tokens
end

Instance Method Details

#to_jsonString

Returns:

  • (String)


17
18
19
20
21
22
23
24
# File 'lib/llm/usage.rb', line 17

def to_json(...)
  LLM.json.dump({
    input_tokens:, output_tokens:,
    reasoning_tokens:,
    input_audio_tokens:, output_audio_tokens:, input_image_tokens:,
    cache_read_tokens:, cache_write_tokens:, total_tokens:
  })
end