Class: Agents::RunContext::Usage

Inherits:
Object
  • Object
show all
Defined in:
lib/agents/run_context.rb

Overview

Usage tracks token consumption across all LLM calls within a single run. This is very rudimentary usage reporting. We can use this further for billing purposes, but is not a replacement for tracing.

Examples:

Accumulating usage from multiple LLM calls

usage = Agents::RunContext::Usage.new

# Add usage from first call
usage.add(OpenStruct.new(input_tokens: 100, output_tokens: 50, total_tokens: 150))

# Add usage from second call
usage.add(OpenStruct.new(input_tokens: 200, output_tokens: 100, total_tokens: 300))

puts usage.total_tokens  # => 450

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUsage

Initialize a new Usage tracker with all counters at zero



90
91
92
93
94
# File 'lib/agents/run_context.rb', line 90

def initialize
  @input_tokens = 0
  @output_tokens = 0
  @total_tokens = 0
end

Instance Attribute Details

#input_tokensObject

Returns the value of attribute input_tokens.



87
88
89
# File 'lib/agents/run_context.rb', line 87

def input_tokens
  @input_tokens
end

#output_tokensObject

Returns the value of attribute output_tokens.



87
88
89
# File 'lib/agents/run_context.rb', line 87

def output_tokens
  @output_tokens
end

#total_tokensObject

Returns the value of attribute total_tokens.



87
88
89
# File 'lib/agents/run_context.rb', line 87

def total_tokens
  @total_tokens
end

Instance Method Details

#add(response) ⇒ Object

Add usage metrics from an LLM response to the running totals. Only tracks usage for responses that have token data (e.g., RubyLLM::Message). Safely skips responses without token methods (e.g., RubyLLM::Tool::Halt).

Examples:

Adding usage from an LLM response

usage.add(llm_response)

Parameters:

  • response (RubyLLM::Message)

    A RubyLLM::Message object with token usage data



103
104
105
106
107
108
109
110
111
112
# File 'lib/agents/run_context.rb', line 103

def add(response)
  return unless response.respond_to?(:input_tokens)

  input = response.input_tokens || 0
  output = response.output_tokens || 0

  @input_tokens += input
  @output_tokens += output
  @total_tokens += input + output
end