Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents
Project description
agent-loop-guard
Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents.
Install
pip install agent-loop-guard
Quick Start
from loop_guard import LoopGuard, Action
guard = LoopGuard()
for action in agent_actions:
decision = guard.check(tool=action.name, args=action.args)
if decision.action == Action.STOP:
print(f"Loop detected: {decision.reason}")
break
Why not just max_iter?
| Approach | What it catches | Limitation |
|---|---|---|
max_iter=10 |
Runaway agents | Kills long legitimate tasks; misses 3-step loops at step 9 |
| agent-loop-guard | Exact repeats, fuzzy repeats, A→B→C→A cycles, output stagnation | — |
max_iter is a blunt timeout. agent-loop-guard detects behavioral patterns — the agent doing the same thing over and over, even with slight variations.
Detection Strategies
| Strategy | What it detects | Confidence signal |
|---|---|---|
| Exact Repeat | Same (tool, args) called repeatedly |
Consecutive identical calls |
| Fuzzy Repeat | Near-identical args (Jaccard + edit distance) | Similarity > threshold |
| Cycle Detection | A→B→C→A→B→C repeating sequences | Pattern repetition count |
| Output Stagnation | Tool returns same output repeatedly | Output similarity > threshold |
All four strategies run on every call. The highest confidence wins.
API
guard = LoopGuard(
window_size=10, # actions to keep in memory
similarity_threshold=0.85, # fuzzy match threshold
)
decision = guard.check(
tool="web_search", # tool/function name
args={"query": "python"}, # arguments (dict or str)
output="Results: ...", # optional: enables stagnation detection
)
decision.action # Action.CONTINUE | WARN | STOP | ESCALATE
decision.reason # "Cycle detected: [search → parse → search] repeated 3 times"
decision.strategy # "cycle_detection"
decision.confidence # 0.0 ~ 1.0
decision.is_loop # True if STOP or ESCALATE
decision.should_warn # True if WARN
guard.reset() # reuse for next session
Action Escalation
Actions escalate with consecutive detections:
from loop_guard import ActionConfig
config = ActionConfig(
warn_threshold=2, # 2 consecutive hits → WARN
stop_threshold=4, # 4 consecutive hits → STOP
escalate_threshold=6, # 6 consecutive hits → ESCALATE
)
guard = LoopGuard(action_config=config)
Generic Callback
from loop_guard.integrations.generic import LoopGuardCallback
callback = LoopGuardCallback(
on_warn=lambda d: logger.warning(f"Loop warning: {d.reason}"),
on_stop=lambda d: raise_stop_error(d),
)
# In your agent loop:
decision = callback.before_tool_call("search", {"query": "test"})
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agent_loop_guard-0.1.1.tar.gz.
File metadata
- Download URL: agent_loop_guard-0.1.1.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52cc8c0572509696b073d79108a010be9fcce32ada8354f92c9900866073b07c
|
|
| MD5 |
f773f088448483385668face3c5c0bcb
|
|
| BLAKE2b-256 |
3349da649b8f53e0684e77a053b17b3f184f82281e9d5cd98611cbe3e3641ebe
|
Provenance
The following attestation bundles were made for agent_loop_guard-0.1.1.tar.gz:
Publisher:
publish.yml on QuartzUnit/agent-loop-guard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_loop_guard-0.1.1.tar.gz -
Subject digest:
52cc8c0572509696b073d79108a010be9fcce32ada8354f92c9900866073b07c - Sigstore transparency entry: 1178846130
- Sigstore integration time:
-
Permalink:
QuartzUnit/agent-loop-guard@1422d41a310e37ed6cef2ec164003dff2bef9c5f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/QuartzUnit
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1422d41a310e37ed6cef2ec164003dff2bef9c5f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file agent_loop_guard-0.1.1-py3-none-any.whl.
File metadata
- Download URL: agent_loop_guard-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f5d487f39f04120a44c3f0ab3d02a4ccaa130c6c8361e79e7b21f83029bb52a
|
|
| MD5 |
e20a46d983eca84f78a9c08dc5d894e0
|
|
| BLAKE2b-256 |
f1e43ab6c5a071297633f101f64c94d09583332df564e2f22575b00400a739cc
|
Provenance
The following attestation bundles were made for agent_loop_guard-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on QuartzUnit/agent-loop-guard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_loop_guard-0.1.1-py3-none-any.whl -
Subject digest:
5f5d487f39f04120a44c3f0ab3d02a4ccaa130c6c8361e79e7b21f83029bb52a - Sigstore transparency entry: 1178846137
- Sigstore integration time:
-
Permalink:
QuartzUnit/agent-loop-guard@1422d41a310e37ed6cef2ec164003dff2bef9c5f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/QuartzUnit
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1422d41a310e37ed6cef2ec164003dff2bef9c5f -
Trigger Event:
workflow_dispatch
-
Statement type: