Loop Detection Policy Template

Automatically detect when an AI agent enters an infinite loop or recursive call pattern and stop it before it burns through your budget. Govyn tracks request patterns per agent and triggers a circuit breaker when repetitive or runaway behavior is detected — no changes to your agent code required.

What this prevents

A CrewAI crew of three agents was tasked with researching a topic. The researcher agent couldn't find satisfactory sources and kept asking the writer agent to refine the query, which sent it back to the researcher — creating an infinite delegation loop. In 20 minutes the agents exchanged 800 messages, generating $340 in GPT-4o costs for zero useful output. Loop detection would have identified the repetitive pattern after 5 similar requests and halted the cycle automatically.

Policy template

Copy this into your govyn.yaml and adjust the values to match your requirements.

govyn.yaml
agents:
  autonomous_agent:
    loop_detection:
      enabled: true
      window: 60s
      max_identical_requests: 5
      max_requests_per_window: 50
      similarity_threshold: 0.85
      action: block
      cooldown: 300s
    rate_limit:
      requests_per_minute: 30
    budget:
      daily: $10.00
    notifications:
      on: [loop_detected]
      webhook: https://hooks.slack.com/your-channel

How it works

1

Govyn tracks request patterns per agent

Every request from an agent is fingerprinted based on content, model, and parameters. Govyn maintains a sliding window of recent requests for each agent key.

2

Similarity analysis runs on each request

New requests are compared against recent ones in the window. If a request is identical or highly similar (above the similarity_threshold) to recent requests, the repetition counter increments.

3

Circuit breaker triggers on threshold

When the repetition count exceeds max_identical_requests or total requests exceed max_requests_per_window, Govyn triggers the configured action — blocking further requests from that agent.

4

Agent is blocked during cooldown period

The agent receives a clear error response explaining it has been rate-limited due to loop detection. A notification is sent to your webhook. After the cooldown period, the agent can resume.

Configuration options

Option Description Example
loop_detection.window Time window for tracking request patterns 60s
loop_detection.max_identical_requests Max identical/similar requests before triggering 5
loop_detection.similarity_threshold Cosine similarity threshold for near-duplicate detection 0.85
loop_detection.action Action when loop is detected: block, warn, or throttle block
loop_detection.cooldown Time before the agent can resume after being blocked 300s

Add this policy to your config

Start Govyn with this policy in under 5 minutes. No code changes needed.

Get started

Related policy templates

Explore more