ExAgent.RateLimiter.Adapter behaviour (ExAgent v3.0.0)

View Source

Shared admission and settlement for physical model attempts.

Stability — Stable.

One backend coordinates all limiters for the same provider account. Use the configured key within the scope represented by the adapter handle. Each key uses the same limits on every replica. The callbacks receive a relative IO budget in milliseconds. The backend uses its own clock for permit expiry.

acquire/3 atomically checks the limits and reserves capacity for a fresh attempt ID. Reply :ok only after the reservation is committed. Reject a duplicate acquisition for an ID retained in its bucket. Acknowledgement within the budget authorizes dispatch. Expire the concurrency hold after the TTL and account conservatively for attempts whose terminal is unknown.

settle/5 accepts the first terminal for an attempt. Repeated, conflicting or no-longer-retained reports leave the quota unchanged. :unused proves the attempt never started. An attempted outcome carries the qualified token total or :unknown and an optional provider cooldown in milliseconds. An unknown total keeps the estimate charged. The adapter receives no model content or credentials from the request.

Summary

Types

The configured ceilings for this bucket. An absent ceiling is unlimited.

The PostgreSQL quota schema was confirmed missing.

The evidence that closes a permit.

A fresh physical attempt, estimated tokens and its relative dispatch lifetime.

An available token total with its model accounting quality, or unknown use.

Callbacks

Commits a fresh permit, or refuses it with a retry delay or an operational error.

Clears the key's provider cooldown while preserving its reservations and usage.

Idempotently applies a terminal report to the committed attempt.

Types

limits()

@type limits() :: %{optional(:rpm | :tpm | :concurrency) => pos_integer()}

The configured ceilings for this bucket. An absent ceiling is unlimited.

migration_required()

@type migration_required() :: {:rate_limiter_migration_required, 1}

The PostgreSQL quota schema was confirmed missing.

outcome()

@type outcome() :: :unused | {:attempted, usage(), non_neg_integer() | nil}

The evidence that closes a permit.

request()

@type request() :: %{
  id: String.t(),
  key: ExAgent.RateLimiter.key(),
  limits: limits(),
  estimated_tokens: non_neg_integer(),
  ttl_ms: pos_integer()
}

A fresh physical attempt, estimated tokens and its relative dispatch lifetime.

usage()

@type usage() :: {:reported | :normalized | :mixed, non_neg_integer()} | :unknown

An available token total with its model accounting quality, or unknown use.

Callbacks

acquire(handle, request, timeout_ms)

@callback acquire(handle :: term(), request(), timeout_ms :: pos_integer()) ::
  :ok
  | {:deny, pos_integer() | :infinity}
  | {:error,
     :rate_limiter_unavailable
     | :deadline_exceeded
     | :result_unconfirmed
     | migration_required()}

Commits a fresh permit, or refuses it with a retry delay or an operational error.

reset_cooldown(handle, key, timeout_ms)

@callback reset_cooldown(
  handle :: term(),
  ExAgent.RateLimiter.key(),
  timeout_ms :: pos_integer()
) ::
  :ok
  | {:error,
     :rate_limiter_unavailable | :result_unconfirmed | migration_required()}

Clears the key's provider cooldown while preserving its reservations and usage.

settle(handle, key, id, outcome, timeout_ms)

@callback settle(
  handle :: term(),
  ExAgent.RateLimiter.key(),
  id :: String.t(),
  outcome(),
  timeout_ms :: pos_integer()
) ::
  :ok
  | {:error,
     :rate_limiter_unavailable
     | :deadline_exceeded
     | :result_unconfirmed
     | migration_required()}

Idempotently applies a terminal report to the committed attempt.