Class: Legion::LLM::Usage

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/llm/metering/usage.rb

Overview

Immutable value object representing token usage for a provider response.

input_tokens - Integer tokens consumed by the prompt output_tokens - Integer tokens in the generated response cache_read_tokens - Integer tokens served from prompt cache (e.g. Anthropic cache_read) cache_write_tokens - Integer tokens written to prompt cache total_tokens - Integer total; auto-calculated as input + output when not provided output_tokens_details - Optional breakdown (e.g. { reasoning_tokens: 48 }) from Responses API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0, total_tokens: nil, output_tokens_details: {}) ⇒ Usage

Returns a new instance of Usage.



21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/llm/metering/usage.rb', line 21

def initialize(input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0, total_tokens: nil, output_tokens_details: {})
  super(
    input_tokens:,
    output_tokens:,
    cache_read_tokens:,
    cache_write_tokens:,
    total_tokens:          total_tokens || (input_tokens + output_tokens),
    output_tokens_details: output_tokens_details
  )
end

Instance Attribute Details

#cache_read_tokensObject (readonly)

Returns the value of attribute cache_read_tokens

Returns:

  • (Object)

    the current value of cache_read_tokens



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def cache_read_tokens
  @cache_read_tokens
end

#cache_write_tokensObject (readonly)

Returns the value of attribute cache_write_tokens

Returns:

  • (Object)

    the current value of cache_write_tokens



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def cache_write_tokens
  @cache_write_tokens
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def output_tokens
  @output_tokens
end

#output_tokens_detailsObject (readonly)

Returns the value of attribute output_tokens_details

Returns:

  • (Object)

    the current value of output_tokens_details



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def output_tokens_details
  @output_tokens_details
end

#total_tokensObject (readonly)

Returns the value of attribute total_tokens

Returns:

  • (Object)

    the current value of total_tokens



13
14
15
# File 'lib/legion/llm/metering/usage.rb', line 13

def total_tokens
  @total_tokens
end