Docs menu

Guides

Loop detection

The most expensive agent failure mode is the quiet one: an agent re-issuing the same call in a cycle, each iteration burning tokens. RateGuard fingerprints payloads with SHA-256 and halts the pattern before it compounds.

How a loop is defined

A loop is an identical fingerprint reappearing at a higher sequence depth. Same-depth repeats are treated as retries (legitimate). Depths beyond LoopMaxDepth (default 50) halt regardless. Fingerprint state is LRU-bounded at 10K entries — no memory leaks.

Middleware wiring

Go — enable, then agents send headers
rg := rateguard.New(rateguard.Config{
    Preset:        "agent-orchestrator",
    LoopDetection: true,
    LoopMaxDepth:  50, // default
})

// Agents include on each request:
//   X-Sequence-Depth: 3              (required to activate the check)
//   X-Payload-Fingerprint: <sha256>  (optional — else SHA256(method+path+body))
//
// Detected loops → 429 {"error": "loop_detected", ...}

Library use (any SDK)

fp := rateguard.Fingerprint(systemPrompt, userInput, toolDefs)

allowed, reason := detector.Check(fp, depth) // records the observation
allowed, reason  = detector.Peek(fp, depth)  // pre-flight, records nothing

Tip

check_loop is one of the five MCP tools — an agent can ask "am I looping?" about its own behavior before issuing the call. Peek semantics: asking records nothing.