ExAgent. UsageLimits
(ExAgent v3.0.0)
View Source
Execution-tree safety net: each run's limits also constrain its framework descendants. The runtime serializes admission across the tree and uses these functions to check limits. Token and cost thresholds are checked again just before dispatch. A request that stops at this check still counts as admitted. The thresholds are retrospective, so already dispatched requests can exceed them.
Stability — Stable.
Set limits on the agent or pass usage_limits: to a direct run, stream,
resume or Server turn. The lowest threshold applies for each dimension.
If any of the applicable policies uses :strict accounting, the run uses
:strict. A new %UsageLimits{} defaults to :strict, even if it sets only
a request or tool threshold. Pass accounting: :estimated explicitly when
tightening an estimated policy. Resuming retains the saved limits too.
Any nil threshold is unchecked. When a limit is exceeded the run terminates with
a RunError with reason {:usage_limit_exceeded, dimension, value}. Dimensions
are :requests, :total_tokens, :input_tokens, :output_tokens,
:tool_calls and :budget_cents.
request_limit/total_tokens_limit/input_tokens_limit/output_tokens_limit— checked before each model request.tool_calls_limit— checked before a batch of tool calls is executed (pydanticAI semantics: if the model returned parallel calls that would exceed the limit, none run).max_budget_cents— checked before each model request against an estimated cost (seeExAgent.CostGuard). An explicit:estimate_costcallback takes precedence over a qualified public upstream estimate; unknown/error never silently falls back. Candidate shared:spend_budgetadmission uses the same cents and limit reason, with{:retry_after_ms, wait}as its evidence.
accounting: :strict (default) requires sufficient Model-reported metrics for
enabled thresholds. A declared normalized-only contract rejects before IO;
unknown contracts are checked against actual reports before later effects.
Ordinary runs check the reserved batch against accumulated usage again after
resolving permissions for each tool. Strict accounting can block a waiting
tool while another request's usage is unavailable. Work already dispatched
can still finish. A successful fallback after a dispatched failure without
usage may leave a strict token or cost limit without the coverage it requires.
accounting: :estimated opts into available subtotals and may continue without
metrics/pricing, but metric limits require a finite request_limit in this scope
or an ancestor. The recorded subtotal also applies before preparing successor
steps in Composition and Flow. Host-only limits require no accounting. A child's
policy cannot weaken its ancestors. In-flight operations can exceed
retrospective thresholds.
Example
Here model is an explicitly configured ExAgent.Models.ReqLLM instance;
normalized metrics require opt-in estimated thresholds and a finite request bound.
alias ExAgent.{CostGuard, UsageLimits}
agent =
ExAgent.new(
model: model,
usage_limits: %UsageLimits{
accounting: :estimated,
request_limit: 5,
total_tokens_limit: 2000,
tool_calls_limit: 10,
max_budget_cents: 25
}
)
pricing = CostGuard.estimator(%{input_per_1k_cents: 250, output_per_1k_cents: 1000})
ExAgent.run(agent, "research this", estimate_cost: pricing)
Summary
Functions
Check accumulated usage and cost before admitting another model request.
request_count counts requests already admitted (zero before the first).
Check the tool_calls_limit before executing a batch of incoming tool
calls. Returns :ok or {:error, {:usage_limit_exceeded, :tool_calls, n}}.
Validate a complete limits struct or nil. Counters must be nonnegative
integers, and a monetary threshold must be a nonnegative number. A struct
with missing or extra fields returns {:error, :invalid_usage_limits}.
Types
@type t() :: %ExAgent.UsageLimits{ accounting: :strict | :estimated, input_tokens_limit: non_neg_integer() | nil, max_budget_cents: number() | nil, output_tokens_limit: non_neg_integer() | nil, request_limit: non_neg_integer() | nil, tool_calls_limit: non_neg_integer() | nil, total_tokens_limit: non_neg_integer() | nil }
Functions
@spec check_before_request( t(), ExAgent.Message.Usage.t(), non_neg_integer(), number() | {:partial, number()} | :unknown | nil ) :: :ok | {:error, term()}
Check accumulated usage and cost before admitting another model request.
request_count counts requests already admitted (zero before the first).
Pass the estimated cost so far in nonnegative cents, {:partial, cents}
for a known subtotal, or :unknown or nil when no price is available.
A numeric quote represents the full accumulated price. If only some of
the usage was measured, pass the price of that portion as a partial quote.
A strict monetary limit requires a full price. An estimated limit compares
the subtotal to its threshold.
Omitting the quote is equivalent to passing nil.
Returns :ok or {:error, reason}. An exceeded monetary limit reports
{:usage_limit_exceeded, :budget_cents, cents} with the numeric amount.
An invalid quote returns {:error, :invalid_cost_estimate}.
@spec check_tool_calls(t(), non_neg_integer(), non_neg_integer()) :: :ok | {:error, {:usage_limit_exceeded, :tool_calls, non_neg_integer()}}
Check the tool_calls_limit before executing a batch of incoming tool
calls. Returns :ok or {:error, {:usage_limit_exceeded, :tool_calls, n}}.
executed is the historical parameter name for admitted/reserved tool calls,
not dispatched calls, successes or external effects. Following
pydanticAI, if executed + incoming would exceed the limit, none of the
incoming calls are executed.
Validate a complete limits struct or nil. Counters must be nonnegative
integers, and a monetary threshold must be a nonnegative number. A struct
with missing or extra fields returns {:error, :invalid_usage_limits}.