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.
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
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.
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.
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.
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 startedRelated policy templates
Set daily and monthly spending limits for AI agents. Prevent runaway costs with hard budget caps enforced at the proxy level.
Instantly kill-switch all AI agent API access with a single command. Emergency lockdown policy for immediate agent shutdown.
Protect production environments from AI agent damage. Model restrictions, rate limits, and approval gates for high-risk operations.
Explore more
How the Meta email deletion incident could have been prevented with 4 lines of YAML, and why OpenClaw built-in limits are not enough.
FROM OUR BLOGHow smart model routing through a proxy cut our OpenAI and Anthropic bill from $2,140/mo to $578/mo. Zero code changes. Just YAML.
INTEGRATIONAdd budget controls and policy enforcement to CrewAI multi-agent crews using OpenAI. Govern every agent in your crew independently.
COMPARISONCompare Govyn and AgentBudget for AI agent cost control. Proxy-level enforcement vs in-code budget tracking.