ExAgent. UsageLimits. Budget
(ExAgent v3.0.0)
View Source
Shared monetary admission for attempts billed to a host account.
Stability — Stable.
Attach a budget to an agent with the candidate :spend_budget option. Give each tenant a trusted
adapter handle. Descendants of the execution scope inherit its budget. Each
physical model attempt, including Fallback attempts, reserves the configured
amount. Media operations and tools with a declared reservation also use
the account. An attempted operation with unknown cost keeps its hold until
reconciliation.
Public amounts are in cents, with fractions allowed. The adapter stores integer millionths of a cent. A reservation is a host quote. A qualified complete price settles the attempt at its estimated cost. Both the quote and settlement round up to the next microcent when needed. A known subtotal may raise the hold. Select a quote that covers the expected spend; the eventual price can exceed it. The budget uses the model's qualified prices independently of a run's per-execution cost estimator.
Use ExAgent.UsageLimits.Budget.ETS for a local account or
ExAgent.UsageLimits.Budget.Postgres for an account shared across replicas.
Summary
Types
A refusal before the attempt. A finite wait is in milliseconds.
A committed snapshot of the active billing window. The window begins at a Unix millisecond.
Cents, with Decimal available for amounts that need exact fractional precision.
Functions
Reads the active window's ceiling, confirmed charges and outstanding holds.
Builds a budget with a backend, a positive reservation and a timeout (5,000 ms by default).
Closes a pending reservation with authoritative billing evidence in cents. Use zero
when the evidence confirms that the provider did not charge. Retrying with
the same reservation ID and billed amount returns :ok without a second
charge. A different amount after closure returns
{:error, :budget_settlement_conflict}. The host
authorizes this operation for the budget account.
Types
@type admission_error() :: {:usage_limit_exceeded, :budget_cents, {:retry_after_ms, pos_integer() | :infinity | :unknown}} | unavailable() | :spend_budget_policy_mismatch | :deadline_exceeded
A refusal before the attempt. A finite wait is in milliseconds.
@type balance() :: %{ window_start: non_neg_integer(), limit_cents: cents(), charged_cents: cents() | nil, held_cents: cents(), partial?: boolean() }
A committed snapshot of the active billing window. The window begins at a Unix millisecond.
Cents, with Decimal available for amounts that need exact fractional precision.
@opaque t()
Functions
@spec balance(t(), keyword()) :: {:ok, balance()} | {:error, unavailable() | {:unsupported, :budget_balance}}
Reads the active window's ceiling, confirmed charges and outstanding holds.
Holds count across windows. They mark the balance partial until settled.
When there is an open hold, a positive charge is a known subtotal and a
missing subtotal is nil. A complete empty window reports zero. The read
never takes the account lock or authorizes a reservation.
Pass timeout: milliseconds to override the budget's default for this read.
An adapter without the optional read returns {:error, {:unsupported, :budget_balance}}.
The caller authorizes access to the budget account.
Builds a budget with a backend, a positive reservation and a timeout (5,000 ms by default).
@spec reconcile(t(), String.t(), cents()) :: :ok | {:error, ExAgent.UsageLimits.Budget.Adapter.settlement_error()}
Closes a pending reservation with authoritative billing evidence in cents. Use zero
when the evidence confirms that the provider did not charge. Retrying with
the same reservation ID and billed amount returns :ok without a second
charge. A different amount after closure returns
{:error, :budget_settlement_conflict}. The host
authorizes this operation for the budget account.