Candil.Cost (Candil v3.0.0)

Copy Markdown View Source

Cost estimation for LLM API usage.

Prices are stored as USD per 1M tokens for (input, output). Only models with well-known pricing are listed; unknown models return :unknown.

Example

iex> Candil.Cost.estimate("gpt-4o", 1000, 500)
{:ok, 0.00775}

iex> Candil.Cost.estimate("unknown-model", 1000, 500)
:unknown

Summary

Functions

Estimates the USD cost for a model and token counts.

Estimate cost for a (provider, model, input_tokens, output_tokens) tuple. Returns {:ok, cost_usd} on success, :unknown if the model is not in the pricing table, or {:error, reason} if the inputs are invalid.

Formats a USD price as a human-readable string.

Returns the model names with known pricing.

Functions

estimate(model, input_tokens, output_tokens)

@spec estimate(String.t(), non_neg_integer(), non_neg_integer()) ::
  {:ok, float()} | :unknown

Estimates the USD cost for a model and token counts.

estimate(provider, model, input_tokens, output_tokens)

@spec estimate(atom() | String.t(), String.t(), non_neg_integer(), non_neg_integer()) ::
  {:ok, float()} | :unknown | {:error, term()}

Estimate cost for a (provider, model, input_tokens, output_tokens) tuple. Returns {:ok, cost_usd} on success, :unknown if the model is not in the pricing table, or {:error, reason} if the inputs are invalid.

Emits [:candil, :cost, :estimate] telemetry on every call (even for unknown models, with cost_usd: 0.0).

format_price(price)

@spec format_price(float()) :: String.t()

Formats a USD price as a human-readable string.

Examples:

  • 0.00775 → "$0.0078"
  • 1.50 → "$1.50"
  • 1500.0 → "$1.5K"
  • 1_500_000 → "$1.5M"

Negative prices are formatted with a leading minus sign.

Examples

iex> Candil.Cost.format_price(0.0075)
"$0.0075"

iex> Candil.Cost.format_price(1500.0)
"$1.5K"

known_models()

@spec known_models() :: [String.t()]

Returns the model names with known pricing.