Class: Agents::RunContext::Usage
- Inherits:
-
Object
- Object
- Agents::RunContext::Usage
- 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.
Instance Attribute Summary collapse
-
#input_tokens ⇒ Object
Returns the value of attribute input_tokens.
-
#output_tokens ⇒ Object
Returns the value of attribute output_tokens.
-
#total_tokens ⇒ Object
Returns the value of attribute total_tokens.
Instance Method Summary collapse
-
#add(response) ⇒ Object
Add usage metrics from an LLM response to the running totals.
-
#initialize ⇒ Usage
constructor
Initialize a new Usage tracker with all counters at zero.
Constructor Details
#initialize ⇒ Usage
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_tokens ⇒ Object
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_tokens ⇒ Object
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_tokens ⇒ Object
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).
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 |