Skip to main content

Module tool_call_loop

Module tool_call_loop 

Source
Expand description

Cross-turn tool-call loop guard — ported from omp packages/ai/src/utils/tool-call-loop-guard.ts.

MIT — attribution: adapted from omp (Can Berk Güder, earendil-works).

§Purpose

Models occasionally fixate on a single tool call: the same name and arguments across consecutive turns, with the same (often unhelpful) result. Left unchecked the loop burns the context window and the user’s quota indefinitely.

ToolCallLoopGuard records each completed assistant turn and fires when the same single-tool call (modulo argument key ordering) hits a configurable threshold. Multi-call turns reset detection.

Exempt tools (e.g. read for progressive file exploration, ls for directory walks) bypass detection — their repetition is legitimate.

Structs§

RepeatedToolCallDetection
Details surfaced when a loop is recognised. Hosts can render these into a steering message or abort the agent run.
ToolCallLoopGuard
Detects consecutive identical assistant tool calls across model turns.
ToolCallLoopGuardOptions
Runtime settings for cross-turn tool-call repetition detection.
ToolCallLoopTurn
One completed assistant turn for the guard to evaluate.
ToolCallRef
A tool call as seen by the guard. Hosts adapt their provider-native types into this shape.
ToolResultRef
A tool result as seen by the guard.