Alloy.Usage (alloy v0.10.1)

Copy Markdown View Source

Token usage tracking across turns.

Accumulates input/output token counts from provider responses so callers can track costs and enforce limits.

Summary

Functions

Estimates cost in cents given per-million token prices. Replaces (does not accumulate) the existing estimated_cost_cents.

Merges usage from a provider response into accumulated usage.

Returns total tokens consumed (input + output).

Types

t()

@type t() :: %Alloy.Usage{
  cache_creation_input_tokens: non_neg_integer(),
  cache_read_input_tokens: non_neg_integer(),
  estimated_cost_cents: number(),
  input_tokens: non_neg_integer(),
  output_tokens: non_neg_integer()
}

Functions

estimate_cost(usage, input_price_per_m, output_price_per_m)

@spec estimate_cost(t(), number(), number()) :: t()

Estimates cost in cents given per-million token prices. Replaces (does not accumulate) the existing estimated_cost_cents.

merge(acc, response_usage)

@spec merge(t(), map()) :: t()

Merges usage from a provider response into accumulated usage.

total(usage)

@spec total(t()) :: non_neg_integer()

Returns total tokens consumed (input + output).