SHIPPED

Recursive Loop Protection

Share

No other API proxy blocks agent loops at the gateway layer — most frameworks require you to instrument this in your application code. AI Security Gateway (AISG) detects and kills infinite retry loops automatically, before they reach any provider.

Autonomous AI agents can get stuck sending the same request hundreds of times, draining your budget in minutes. Gateway-level detection works across all frameworks, languages, and agent architectures with zero code changes.

The Problem

Agent frameworks like LangChain, CrewAI, AutoGPT, and custom agent loops share a common failure mode: when the model returns an unexpected response, the agent retries with the same prompt. If the model keeps returning the same response, the agent keeps retrying — indefinitely.

Real-world impact: A single misconfigured agent can send thousands of identical requests in minutes. At $3/1M input tokens for GPT-4.1, a loop sending 1,000-token prompts 10 times per second burns through $108 in an hour. With a longer context window, it's much worse.

How It Works

1

Fingerprint

Every request is fingerprinted using SHA-256 hashing of request content. This creates a unique signature for each distinct request pattern.

2

Count

A sliding window counter tracks how many times each fingerprint has been seen within the detection window.

3

Block

When a fingerprint exceeds the configurable threshold, the request is blocked with HTTP 429 and a structured error response.

4

Cool down

After blocking, a configurable cooldown prevents the same fingerprint from being accepted. This gives agents time to recover or for operators to intervene.

What the Client Sees

When a loop is detected, the client receives an HTTP 429 response with a structured error body containing retry guidance and diagnostic information. The error message clearly indicates that the request was blocked due to a repetitive pattern.

HTTP 429 — Loop Detected
{
  "detail": {
    "error": "recursive_loop_detected",
    "message": "Blocked: repetitive request pattern detected.
                This usually indicates an agent retry loop, infinite
                recursion, or misconfigured automation.",
    "cooldown_seconds": <configurable>
  }
}

Handling in the AISG SDK

Python — AISG SDK
from aisg import AISG
from aisg.exceptions import LoopDetectedError

client = AISG()
try:
    response = client.chat.completions.create(
        model="oah/llama-4-maverick",
        messages=[{"role": "user", "content": "Hello"}],
    )
except LoopDetectedError as e:
    print(f"Loop detected — cooldown: {e.cooldown_seconds}s")
    # Implement backoff or alert your team

Configuration

Loop detection is enabled by default with sensible thresholds tuned for production use. Three parameters are configurable:

  • Detection window — how far back to look for repeated patterns
  • Repeat threshold — how many identical requests trigger a block
  • Cooldown period — how long the fingerprint stays blocked after detection

Configuration details are available in the project dashboard and the self-hosted deployment guide.

False Positive Safety

The fingerprinting algorithm is designed to detect genuine loops while avoiding false positives during normal usage:

  • Normal conversations with evolving messages are never affected
  • Different models get independent counters
  • Different API keys get independent counters
  • Any variation in request content produces a different fingerprint
  • Only truly identical, repeated requests within the detection window trigger protection

Batch Processing & Test Suites

Running legitimate high-frequency identical requests? The repeat threshold is configurable per project. Adjust it in your project settings before starting batch evaluation jobs, automated test suites, or benchmark runs.

Common scenarios where you may need to increase the threshold:

  • Batch evaluation with a fixed template prompt across many inputs
  • CI/CD test suites sending the same test prompt repeatedly
  • Load testing or benchmarking with identical payloads

Webhook Integration

Loop detection events are available as webhook notifications. Subscribe to the loop.detected event to get real-time alerts when agent loops are blocked.

Webhook payload — loop.detected
{
  "event": "loop.detected",
  "timestamp": "2026-05-22T14:30:00Z",
  "project_id": "proj_abc123",
  "request_id": "req_def456",
  "data": {
    "model": "oah/llama-4-maverick",
    "cooldown_seconds": <configurable>
  }
}

Related Documentation