Table of Contents

Class ToolLoopDetector

Namespace
JD.AI.Core.Safety
Assembly
JD.AI.Core.dll

Detects repetitive tool invocation patterns that indicate no-progress loops. Tracks a rolling window of recent calls and identifies:

  • Same-tool/same-args repetitions (stuck loops).
  • Ping-pong patterns between two tools/agents.
  • High-frequency bursts of any single tool.

Thread-safe — designed to be shared across a session.

public sealed class ToolLoopDetector
Inheritance
ToolLoopDetector
Inherited Members

Constructors

ToolLoopDetector(int, int, int, int)

public ToolLoopDetector(int windowSize = 50, int repetitionWarningThreshold = 3, int repetitionHardStopThreshold = 5, int pingPongThreshold = 4)

Parameters

windowSize int
repetitionWarningThreshold int
repetitionHardStopThreshold int
pingPongThreshold int

Properties

CurrentWindowCount

Returns the current invocation count in the window.

public int CurrentWindowCount { get; }

Property Value

int

PingPongThreshold

Number of alternating A→B→A→B calls that constitutes a ping-pong loop.

public int PingPongThreshold { get; }

Property Value

int

RepetitionHardStopThreshold

Number of identical (tool+args) calls in the window that triggers a hard stop.

public int RepetitionHardStopThreshold { get; }

Property Value

int

RepetitionWarningThreshold

Number of identical (tool+args) calls in the window that triggers a warning.

public int RepetitionWarningThreshold { get; }

Property Value

int

WindowSize

Maximum number of records kept in the rolling window.

public int WindowSize { get; }

Property Value

int

Methods

RecordAndEvaluate(string, string?, string?, string?)

Records a tool invocation and returns a detection result.

public LoopDetectionResult RecordAndEvaluate(string toolName, string? argsFingerprint = null, string? outputFingerprint = null, string? agentId = null)

Parameters

toolName string
argsFingerprint string
outputFingerprint string
agentId string

Returns

LoopDetectionResult

Reset()

Clears all recorded history (e.g. after a circuit breaker reset).

public void Reset()