ALLM.Usage (allm v0.4.0)

Copy Markdown View Source

Token and cost usage for a single response — Layer A serializable data.

Every numeric field is optional and nil-able because not every provider returns every counter. Costs are populated either by an adapter that knows its own pricing or by an optional model-catalog integration.

Fields

FieldTypeNotes
:input_tokensnon_neg_integer | nil
:output_tokensnon_neg_integer | nil
:cached_input_tokensnon_neg_integer | nilProvider-cached prompt tokens.
:reasoning_tokensnon_neg_integer | nilReasoning-model thinking tokens.
:total_tokensnon_neg_integer | nil
:input_costfloat | nilUSD; populated when the adapter knows pricing.
:output_costfloat | nil
:total_costfloat | nil
:tool_usagemapPer-tool tally (caller-derived).
:extramapProvider-specific spillover.

Summary

Functions

Build a %Usage{} from keyword opts.

Return the :total_tokens field when set, otherwise the sum of :input_tokens + :output_tokens when both are integers, otherwise nil.

Types

cost()

@type cost() :: float()

t()

@type t() :: %ALLM.Usage{
  cached_input_tokens: non_neg_integer() | nil,
  extra: map(),
  input_cost: cost() | nil,
  input_tokens: non_neg_integer() | nil,
  output_cost: cost() | nil,
  output_tokens: non_neg_integer() | nil,
  reasoning_tokens: non_neg_integer() | nil,
  tool_usage: map(),
  total_cost: cost() | nil,
  total_tokens: non_neg_integer() | nil
}

Functions

new(opts)

@spec new(keyword()) :: t()

Build a %Usage{} from keyword opts.

Every field is optional. Unknown keys raise ArgumentError via struct!/2.

Examples

iex> u = ALLM.Usage.new(input_tokens: 10, output_tokens: 20)
iex> u.input_tokens
10
iex> u.tool_usage
%{}

total_tokens(usage)

@spec total_tokens(t()) :: non_neg_integer() | nil

Return the :total_tokens field when set, otherwise the sum of :input_tokens + :output_tokens when both are integers, otherwise nil.

Examples

iex> ALLM.Usage.total_tokens(ALLM.Usage.new(total_tokens: 42))
42

iex> ALLM.Usage.total_tokens(ALLM.Usage.new(input_tokens: 10, output_tokens: 20))
30

iex> ALLM.Usage.total_tokens(ALLM.Usage.new([]))
nil