Class: Roast::Cogs::Agent::Usage

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/cogs/agent/usage.rb

Overview

Token usage and cost information for agent execution

Tracks the number of input and output tokens consumed during agent execution, along with the associated cost in USD. This information is used to monitor resource consumption and estimate execution costs.

Usage data can represent either aggregate usage across all models or usage for a specific model when tracked in the per-model breakdown.

#### See Also

  • ‘Agent::Stats` - contains aggregate and per-model usage information

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cost_usdObject

The total cost in United States Dollars (USD)

Represents the monetary cost of the agent execution based on the provider’s pricing model. This typically accounts for both input and output token usage, though pricing models may vary by provider and model.

#### See Also

  • ‘input_tokens`

  • ‘output_tokens`

: Float?



56
57
58
# File 'lib/roast/cogs/agent/usage.rb', line 56

def cost_usd
  @cost_usd
end

#input_tokensObject

The number of input tokens consumed

Input tokens represent the text sent to the agent, including the user prompt, system instructions, conversation history, and any other context provided to the model. This metric is used to calculate the cost of providing input to the agent.

#### See Also

  • ‘output_tokens`

  • ‘cost_usd`

: Integer?



30
31
32
# File 'lib/roast/cogs/agent/usage.rb', line 30

def input_tokens
  @input_tokens
end

#output_tokensObject

The number of output tokens generated

Output tokens represent the text generated by the agent in response to the input. This includes the agent’s responses, reasoning, and any other text produced during execution. This metric is used to calculate the cost of the agent’s output.

#### See Also

  • ‘input_tokens`

  • ‘cost_usd`

: Integer?



43
44
45
# File 'lib/roast/cogs/agent/usage.rb', line 43

def output_tokens
  @output_tokens
end

Instance Method Details

#+(other) ⇒ Object

Add two Usage objects together, summing their token counts and costs

Nil values are treated as zero when the other operand is non-nil.

: (Usage) -> Usage



63
64
65
66
67
68
69
# File 'lib/roast/cogs/agent/usage.rb', line 63

def +(other)
  result = Usage.new
  result.input_tokens = sum_nils(input_tokens, other.input_tokens)&.to_int
  result.output_tokens = sum_nils(output_tokens, other.output_tokens)&.to_int
  result.cost_usd = sum_nils(cost_usd, other.cost_usd)&.to_f
  result
end