Typed Tool-Loop Failure Detector
Lift tool-loop detection from prompt-level rules (which the model can ignore) to a mechanical dispatch-boundary veto with five typed failure modes and per-tool caps.
Intent & Description
🎯 Intent
Prompt-level “don’t call X more than 3 times” isn’t enforcement — the model can ignore it. A dispatch-boundary check is actual enforcement.
📋 Context
The agent has a rich tool palette where loop bugs — same tool called repeatedly, or cycling through a small subset without progress — eat substantial budget before any safety net trips. A single global circuit-breaker catches extreme cases but hides the specific shape of the failure when it fires.
💡 Solution
A dispatcher pre-check function. On each tool call, append (timestamp, tool_name, hash(args)) to a bounded rolling window. Evaluate five rules: (1) generic-repeat: same (tool, arg-hash) at least N times; (2) unknown-tool-repeat: unregistered tool at least M times; (3) poll-no-progress: same tool with no state change at least K times; (4) ping-pong: alternating between two tools at least J cycles; (5) global-circuit-breaker: total tool calls in window at least G. Each rule supports per-tool overrides (known-bursty tools capped lower). On trip: return {error: ’tool_loop_detected’, mode:
Real-world Use Case
- Tool palette is rich enough that prompt-level loop rules aren’t reliably followed.
- Loop bugs are observable in telemetry and have wasted real budget historically.
- Per-tool calibration is feasible — known-bursty tools get individually tuned caps.
Source
Advantages
- Loop failures are caught at the dispatch boundary — not in prompt text the model may ignore
- Typed failure modes make triage and per-tool tuning meaningful rather than opaque
- Formatted refusal as a tool result keeps the model in the loop rather than crashing
Disadvantages
- Per-tool caps must be calibrated or legitimate bursty work trips the breaker
- Five typed modes is more state to maintain than a single global breaker
- A determined model can still loop on tools whose patterns the detector missed