Legion.RateLimiter.Policy (Legion v0.5.0)

View Source

Defines the limits enforced by a Legion.RateLimiter.

Every policy has a rolling :window_ms. Within that window, it can limit new agents matching an identity, recorded token usage for those agents, or both:

%Legion.RateLimiter.Policy{
  window_ms: :timer.minutes(1),
  max_agents: 10,
  max_tokens: 100_000
}

Limits

  • :window_ms (required) - positive rolling-window duration in milliseconds.
  • :max_agents - maximum number of matching agent IDs started during the window, including sub-agents. 0 allows no agents; nil disables this limit.
  • :max_tokens - maximum recorded token total for matching agents during the window. Limits are checked when a turn starts and never interrupt a running turn, so one turn can carry the recorded total past the maximum before the next one is denied. 0 accepts no tokens and so denies every turn; nil disables this limit.

A policy with both optional limits set to nil is unrestricted. See Legion.RateLimiter for the adapter interface and Legion.RateLimiter.Postgres for the bundled implementation.

Summary

Functions

Returns :ok when policy is a usable policy.

Types

t()

@type t() :: %Legion.RateLimiter.Policy{
  max_agents: non_neg_integer() | nil,
  max_tokens: non_neg_integer() | nil,
  window_ms: pos_integer()
}

Functions

validate!(policy)

Returns :ok when policy is a usable policy.

Raises ArgumentError naming the offending field otherwise. Legion calls this when an agent starts, so a misconfigured policy fails at the agent that configured it rather than at the first limit evaluation.