ExAgent. RateLimiter
(ExAgent v3.0.0)
View Source
Supervised provider quota admission shared by agent definitions.
Stability — Stable.
{ExAgent.RateLimiter,
name: MyApp.ModelQuota,
limits: %{"google" => [rpm: 60, tpm: 100_000, concurrency: 4]}}Set :rate_limiter on the agent to the same local name or its PID. Use an
atom or a {:via, Registry, {registry, tenant}} name to follow replacements.
Keys are provider strings or {provider, model} string pairs. A provider
uses one granularity. Unconfigured keys pass through. Each configured limit is a
positive integer. A TPM estimate uses the request projection and output cap, or the
configured :default_output_tokens estimate (4096) when no cap is set.
The default limiter coordinates a FIFO queue on this node. The one-minute
window rounds expiry up to the next second. The queue holds at most
:max_queue waiters per key (default 1024).
Fallback's :fallback_on_quota_denial policy requests immediate admission.
It leaves queued callers in order and returns a refusal if capacity is busy.
A zero-argument :clock can supply monotonic milliseconds for quota time.
Reservation, authorization and settlement replies share the run deadline
on the VM monotonic clock. An unused permit expires at that deadline.
The limiter still applies a dispatched attempt's settlement if it receives
the report late. Missing terminal usage leaves the estimate charged.
To share quota across replicas, configure shared: [adapter: {module, handle}]. Each
replica keeps a local registered name and uses the same backend scope and
limits. See ExAgent.RateLimiter.Adapter and ExAgent.RateLimiter.Postgres.
Shared admission returns quota refusals immediately. :lease_ms (120,000)
caps each attempt and :timeout (5,000) caps backend calls. Both are nested
under :shared. Use :clock and :max_queue in local mode.
Summary
Functions
Builds a supervisor child specification and validates its configuration.
Clears a configured key's provider cooldown and wakes its queue. RPM, TPM and concurrency reservations remain in effect. Use after the provider has confirmed that traffic can resume.
Starts and registers the limiter on the current node.
Types
Functions
@spec child_spec(keyword()) :: Supervisor.child_spec()
Builds a supervisor child specification and validates its configuration.
@spec reset_cooldown(server(), key(), keyword()) :: :ok | {:error, :unknown_key | :rate_limiter_unavailable | :result_unconfirmed | ExAgent.RateLimiter.Adapter.migration_required()}
Clears a configured key's provider cooldown and wakes its queue. RPM, TPM and concurrency reservations remain in effect. Use after the provider has confirmed that traffic can resume.
:timeout bounds the caller's wait (5,000 ms by default). Pass
:infinity or an integer in 0..4_294_967_295 milliseconds. An absent limiter
returns {:error, :rate_limiter_unavailable}. Without a reply after dispatch,
returns {:error, :result_unconfirmed}. The limiter may still apply the reset.
A confirmed missing PostgreSQL quota schema returns
{:error, {:rate_limiter_migration_required, 1}}.
With a shared adapter, the reset applies across replicas and also uses the
configured backend timeout.
@spec start_link(keyword()) :: GenServer.on_start()
Starts and registers the limiter on the current node.