ExAgent.Tool.RepeatGuard (ExAgent v3.0.0)

View Source

Capability that tells the model when it keeps repeating an identical tool call.

Stability — Candidate.

Models sometimes call the same tool with the same arguments until the step budget runs out. Before each model request this capability counts the calls made since the latest user message. When the model's latest response repeats a call (same tool name and equal decoded arguments) for the :warn_at-th time or more, the request gets a short runtime note asking it to review the latest tool result and use it if it answers the request. A stateful tool can return a different value on each call.

If one response calls the same tool with different arguments, the note identifies each repeated set by its latest matching call's position in that response. Positions start at one and count all tool calls in model order.

The guard decodes JSON argument strings and compares the resulting Elixir terms. A custom model that alternates %{id: 7} and %{"id" => 7} gets separate counts in memory. Saving and loading the same history as JSON turns both keys into strings. For consistent counts, emit JSON objects with string keys at every level when your tools accept string-keyed arguments.

ExAgent.new(
  model: model,
  tools: tools,
  capabilities: [compaction, ExAgent.Tool.RepeatGuard.new(warn_at: 3)]
)

The note exists only in the projection sent to the model, like the final-step instruction: the canonical history and events never contain it (a persisted continuation's request record captures the projection actually sent). It is rebuilt from the history at the same position in every later request of the task, so the request prefix stays stable for provider prompt caches and for providers that check signed reasoning against it. It never blocks a call; use ExAgent.Permissions, :max_steps or usage limits to bound execution. List it after capabilities that rewrite the projection, such as compaction, so they do not treat the note as the user's request.

When multiple responses are identical, a trimmed projection is matched to their most recent occurrences after the original user request. Custom projections should keep the most recent equal exchanges. If they add more copies of a response than occurred, that response receives no note.

Options (struct fields):

  • :warn_at — repetition count that triggers the note (default 3, minimum 2).
  • :tools — :all (default), the names to watch, or {:except, names}. For example, {:except, ["job_status"]} watches every tool except that poll.

Summary

Functions

Build a guard. Duplicate options or invalid field values raise ArgumentError.

Types

t()

@type t() :: %ExAgent.Tool.RepeatGuard{
  tools: :all | [String.t()] | {:except, [String.t()]},
  warn_at: pos_integer()
}

Functions

new(opts \\ [])

@spec new(keyword()) :: t()

Build a guard. Duplicate options or invalid field values raise ArgumentError.