ExAgent.UsageLimits (ExAgent v0.1.0)

Copy Markdown View Source

Run-level safety net: caps on requests and token usage, enforced before each model request.

Any nil field is unchecked. When a limit is exceeded the run terminates with {:error, {:usage_limit_exceeded, which, value}} instead of looping or burning cost.

Example

alias ExAgent.UsageLimits

agent =
  ExAgent.new(
    model: "openai:gpt-4o",
    usage_limits: %UsageLimits{request_limit: 5, total_tokens_limit: 2000}
  )

Summary

Functions

Check the limits against accumulated usage and the upcoming request count.

Types

t()

@type t() :: %ExAgent.UsageLimits{
  input_tokens_limit: pos_integer() | nil,
  output_tokens_limit: pos_integer() | nil,
  request_limit: pos_integer() | nil,
  total_tokens_limit: pos_integer() | nil
}

Functions

check_before_request(limits, usage, request_count)

@spec check_before_request(t(), ExAgent.Message.Usage.t(), non_neg_integer()) ::
  :ok | {:error, {:usage_limit_exceeded, atom(), number()}}

Check the limits against accumulated usage and the upcoming request count.

request_count is the number of model requests already issued (0 before the first). Returns :ok or {:error, {:usage_limit_exceeded, which, value}}.