Loop Detection
Deterministic sliding-window detection of repeating agent action patterns.
Agents get stuck. They re-run the same search, re-call the same tool, or alternate between two steps forever. Traditional debuggers can't see these semantic loops. SteerPlane detects them deterministically: no LLM judge, no probabilistic scoring.
How it works
The loop detector maintains a bounded history of recent action names (default window W = 8) and
runs a sliding-window pattern match in O(W²) time. It stays sub-millisecond at any realistic window size.
from steerplane import LoopDetector
detector = LoopDetector(window_size=8, min_repetitions=2)
for action in agent_actions:
result = detector.record_action(action)
if result.loop_detected:
raise RuntimeError(f"Loop: {result.pattern} ×{result.repetitions}")For each candidate pattern length, the detector counts consecutive repetitions anchored at the most recent actions and declares a loop once the repetition threshold is met.
What it catches
| Input (window) | Detected pattern |
|---|---|
[A, A, A, A] | [A]: single-action loop |
[A, B, A, B, A, B] | [A, B]: alternating loop |
[A, B, C, A, B, C] | [A, B, C]: multi-step loop |
[X, A, B, A, B, A, B] | [A, B]: offset / phase-shifted loop |
[A, B, C, D, E, F] | none: no loop |
While loop detection is on (the default), a confirmed loop always terminates the run immediately, whatever the kill/alert enforcement mode.
Turning loop detection off
The detector compares action names, so an agent that calls the same tool on purpose, for example once per item in a list, looks like a loop. For that kind of agent, turn loop detection off. Cost, step, runtime and policy limits still apply.
from steerplane import guard
@guard(max_cost_usd=10, max_steps=200, detect_loops=False)
def run_agent():
agent.run()const runAgent = guard(async () => { await agent.run(); }, {
maxCostUsd: 10,
maxSteps: 200,
detectLoops: false,
});detect_loops is also accepted by SteerPlane.run(), RunManager, the four framework
integrations and .steerplane.yml (defaults: detect_loops: false). It needs SDK 1.1.0 or later.
Two independent layers
- In-process: the SDK detects loops on action names as your agent runs.
- Network layer: the gateway detects loops on SHA-256 prompt hashes, catching repetition even when the SDK is bypassed.
Together they provide defense-in-depth.