Skip to contents

AgentUsage() creates a normalized usage record. AgentResult$usage and run usage/stop events are scoped to that run, while Agent$usage() describes the complete in-memory conversation at the time it is called.

This is a read-only S7 value. Its total_tokens property is calculated from input plus output tokens at construction. Read properties with $ or S7::prop(); S7::props() returns a plain named-list snapshot for reporting or serialization. Construct a new value for different usage.

Usage

AgentUsage(
  requests = 0L,
  tool_calls = 0L,
  input_tokens = 0,
  output_tokens = 0,
  cached_tokens = 0,
  cost_usd = 0
)

Arguments

requests

Number of model requests attributed to the run.

tool_calls

Number of requested tool calls, including calls rejected before execution.

input_tokens

Provider-reported input tokens.

output_tokens

Provider-reported output tokens.

cached_tokens

Provider-reported cached input tokens. These are reported separately and are not added again to total_tokens.

cost_usd

Provider-reported estimated cost in US dollars, or NA_real_ when the provider did not report complete cost information.

Value

A read-only AgentUsage S7 object.

Additional properties

@total_tokens

Input plus output tokens, without adding cached input again. Read-only.

Examples

AgentUsage(
  requests = 2,
  tool_calls = 1,
  input_tokens = 120,
  output_tokens = 30,
  cost_usd = 0.002
)
#> <AgentUsage>
#>   requests: 2
#>   tool_calls: 1
#>   tokens: 150
#>   cached_tokens: 0
#>   cost_usd: $0.0020
usage <- AgentUsage(input_tokens = 120, output_tokens = 30, cost_usd = NA_real_)
usage$total_tokens
#> [1] 150
S7::props(usage)
#> $requests
#> [1] 0
#> 
#> $tool_calls
#> [1] 0
#> 
#> $input_tokens
#> [1] 120
#> 
#> $output_tokens
#> [1] 30
#> 
#> $cached_tokens
#> [1] 0
#> 
#> $total_tokens
#> [1] 150
#> 
#> $cost_usd
#> [1] NA
#>