LLM.Usage (LLM v0.1.2)

Copy Markdown View Source

Token usage information from an LLM request.

Reports the number of tokens consumed by the input (prompt) and output (completion). The total_tokens field is computed automatically if not provided by the provider.

Fields

  • :input_tokens — tokens in the prompt/messages sent to the model
  • :output_tokens — tokens in the model's response
  • :total_tokens — sum of input and output tokens

Examples

usage = LLM.Usage.new(input_tokens: 100, output_tokens: 50)
usage.total_tokens
#=> 150

# total_tokens can be provided directly
usage = LLM.Usage.new(input_tokens: 100, output_tokens: 50, total_tokens: 200)
usage.total_tokens
#=> 200

Summary

Functions

Create a usage struct, computing total_tokens if not provided.

Types

t()

@type t() :: %LLM.Usage{
  input_tokens: non_neg_integer() | nil,
  output_tokens: non_neg_integer() | nil,
  total_tokens: non_neg_integer() | nil
}

Functions

new(attrs)

Create a usage struct, computing total_tokens if not provided.

Accepts a map or keyword list with :input_tokens, :output_tokens, and optionally :total_tokens.

LLM.Usage.new(input_tokens: 10, output_tokens: 20)
#=> %LLM.Usage{input_tokens: 10, output_tokens: 20, total_tokens: 30}