ExAgent.CostGuard (ExAgent v3.0.0)

View Source

Helpers for turning token usage into an estimated cost, used together with the max_budget_cents field of ExAgent.UsageLimits.

Stability — Stable.

ExAgent does not ship a pricing table (model prices change constantly and vary by vendor). You bring the prices that matter to you as a map and CostGuard.estimator/1 builds the pricing function you pass to ExAgent.run/3 via the :estimate_cost option.

Example

pricing = ExAgent.CostGuard.estimator(%{
  input_per_1k_cents: 250,
  output_per_1k_cents: 1000
})

ExAgent.run(agent, "go", estimate_cost: pricing)

Summary

Functions

Evaluate homogeneous or model-aware pricing. A valid quote returns {:ok, cents} or {:ok, {:partial, cents}}. An estimator can also return :unknown or an error.

Build a cost estimator from a pricing map.

Types

pricing()

@type pricing() :: %{
  optional(:input_per_1k_cents) => number() | nil,
  optional(:output_per_1k_cents) => number() | nil
}

Functions

estimate(estimator, model, usage)

Evaluate homogeneous or model-aware pricing. A valid quote returns {:ok, cents} or {:ok, {:partial, cents}}. An estimator can also return :unknown or an error.

estimator(pricing)

@spec estimator(pricing()) :: (ExAgent.Message.Usage.t() ->
                           float() | {:partial, float()} | :unknown)

Build a cost estimator from a pricing map.

The map accepts only :input_per_1k_cents and :output_per_1k_cents. Omit a key or set it to nil for an unknown rate. An unrecognized key raises ArgumentError when the estimator is built.

Prices are per 1000 tokens, in cents (250 means $2.50 per 1K, or $2500 per 1M tokens). Fractions are preserved rather than truncating every small request to zero. A number prices complete token counts in all used directions. Partial token counts or a missing rate for one used direction yield {:partial, subtotal_cents} when there is a priced portion. An explicit zero rate counts as priced. If either token count is missing or marked unavailable, or positive usage has no applicable rate, the result is :unknown. Two fully reported zero counts cost zero. This is a flat input/output approximation, without cache discounts or adding reasoning subsets twice. Supply a model-aware callback for differentiated prices.