ExAgent.Tool.RepeatGuard (ExAgent v2.3.0)

View Source

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

Models sometimes loop: they call the same tool with the same arguments, get the same result and call it again 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 use the earlier result, change the arguments or explain what is blocking it.

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.

Options (struct fields):

  • :warn_at — repetition count that triggers the note (default 3, minimum 2).
  • :tools — :all (default) or the tool names to watch. Exclude tools that are expected to repeat, such as polling a job status.

Summary

Types

t()

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

Functions

after_model_request(cap, state)

Callback implementation for ExAgent.Capability.after_model_request/2.

after_tool_execute(cap, ctx, tool_call, result)

Callback implementation for ExAgent.Capability.after_tool_execute/4.

before_tool_execute(cap, ctx, tool_call)

Callback implementation for ExAgent.Capability.before_tool_execute/3.

new(opts \\ [])

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

Build a validated guard. Raises ArgumentError for invalid options.