Lightning.Extensions.UsageLimiting behaviour (Lightning v2.13.4)

View Source

Rate limiting for Lightning workloads that depends on Runtime.

Summary

Callbacks

Checks the usage limits for a given project and user context, returning an error message if the limits are exceeded.

Returns run options based on the given project context. The run options include a timeout value and a flag for saving dataclips.

Increments the AI query count for a given chat session.

Limits specific actions based on an Action and Context.

Types

error()

@type error() :: {:error, error_reason(), message()}

error_reason()

@type error_reason() ::
  :too_many_runs | :runs_hard_limit | :exceeds_limit | :unknown_project

message()

@type message() :: Lightning.Extensions.Message.t()

Callbacks

check_limits(context)

@callback check_limits(context :: Lightning.Extensions.UsageLimiting.Context.t()) ::
  :ok | error()

Checks the usage limits for a given project and user context, returning an error message if the limits are exceeded.

Requires a Context struct containing the project_id and user_id.

Returns

  • {:ok} if the limits are within bounds.
  • {:error, reason, message} if the limits are exceeded.

get_data_retention_message(context)

@callback get_data_retention_message(
  context :: Lightning.Extensions.UsageLimiting.Context.t()
) ::
  message() | nil

get_data_retention_periods(context)

@callback get_data_retention_periods(
  context :: Lightning.Extensions.UsageLimiting.Context.t()
) :: [
  pos_integer(),
  ...
]

get_run_options(context)

@callback get_run_options(context :: Lightning.Extensions.UsageLimiting.Context.t()) ::
  Lightning.Runs.RunOptions.keyword_list()

Returns run options based on the given project context. The run options include a timeout value and a flag for saving dataclips.

Requires a Context struct containing the project_id.

Returns

  • A keyword list of run options including:
    • :run_timeout_ms: The run timeout in milliseconds.
    • :save_dataclips: A boolean indicating whether to save dataclips.

increment_ai_usage(t, map)

@callback increment_ai_usage(Lightning.AiAssistant.ChatSession.t(), map()) ::
  Ecto.Multi.t()

Increments the AI query count for a given chat session.

Requires a Lightning.AiAssistant.ChatSession struct.

Returns

  • An Ecto.Multi struct representing the transaction to increment AI queries.

limit_action(action, context)

@callback limit_action(
  action :: Lightning.Extensions.UsageLimiting.Action.t(),
  context :: Lightning.Extensions.UsageLimiting.Context.t()
) :: :ok | error()

Limits specific actions based on an Action and Context.

Returns

  • :ok if the action is allowed.
  • {:error, reason, message} if the action is not allowed.