Sycophant.Usage (sycophant v0.5.0)

Copy Markdown

Token usage statistics from an LLM response.

Reports input and output token counts, plus optional cache hit/miss information for providers that support prompt caching.

Examples

iex> %Sycophant.Usage{input_tokens: 10, output_tokens: 25}
#Sycophant.Usage<%{in: 10, out: 25}>

Summary

Functions

Calculates cost fields from token counts and a Pricing struct.

t()

Types

t()

@type t() :: %Sycophant.Usage{
  __type__: binary(),
  cache_creation_input_tokens: nil | integer(),
  cache_read_cost: nil | float(),
  cache_read_input_tokens: nil | integer(),
  cache_write_cost: nil | float(),
  input_cost: nil | float(),
  input_tokens: nil | integer(),
  output_cost: nil | float(),
  output_tokens: nil | integer(),
  pricing:
    nil
    | %Sycophant.Pricing{
        __type__: binary(),
        components: [
          %Sycophant.Pricing.Component{
            __type__: binary(),
            id: nil | binary(),
            kind: nil | binary(),
            meter: nil | binary(),
            notes: nil | binary(),
            per: nil | integer(),
            rate: nil | number(),
            size_class: nil | binary(),
            tool: nil | binary(),
            unit: nil | binary()
          }
        ],
        currency: nil | binary()
      },
  reasoning_cost: nil | float(),
  reasoning_tokens: nil | integer(),
  total_cost: nil | float()
}

Functions

calculate_cost(usage, pricing)

@spec calculate_cost(t() | nil, Sycophant.Pricing.t() | nil) :: t() | nil

Calculates cost fields from token counts and a Pricing struct.

Looks up each token component by ID (e.g. "token.input", "token.output") and computes cost as tokens * rate / per. Missing components yield nil costs.

t()