Skip to main content

Detect and kill infinite AI agent loops before they burn your API budget.

Project description

🛡️ AgentLoopGuard

Detect and kill infinite AI agent loops before they burn your API budget.

PyPI version Python 3.9+ License: MIT


The Problem

AI agents get stuck in loops. Your agent calls the same tool 50 times. Or it generates nearly identical outputs over and over. Or it oscillates between two states forever.

Meanwhile, your API bill climbs: $0.12... $1.50... $15.00... $47.00... $412.00.

You wake up to a surprise bill because your agent ran all night doing nothing useful.

The Solution

from agentloopguard import LoopGuard

guard = LoopGuard(
    max_iterations=50,
    max_cost_usd=10.00,
    max_duration_seconds=300,
)

with guard.session() as session:
    for step in my_agent.run():
        session.record({
            "tool_name": step.tool,
            "tool_args": step.args,
            "output": step.result,
            "model": "gpt-4o",
            "input_tokens": step.input_tokens,
            "output_tokens": step.output_tokens,
        })
        # LoopGuard watches every call.
        # Loops die. Your budget lives.

Installation

pip install agentloopguard

Four Detection Engines

Engine What It Catches Default Threshold
Exact Repeat Same tool call repeated consecutively 3 repeats
Semantic Similarity Near-identical outputs (TF-IDF cosine sim) 92% similarity, 3 calls
Cost Velocity Spending money too fast $2/minute
Oscillation A→B→A→B going nowhere 3 cycles

All four run automatically on every session.record() call. Zero configuration needed.

Usage

As a Context Manager

from agentloopguard import LoopGuard

guard = LoopGuard(max_iterations=100, max_cost_usd=5.00)

with guard.session() as session:
    for i in range(200):  # Will be killed at iteration 100
        session.record({
            "tool_name": "search",
            "tool_args": {"query": "same thing"},
            "output": "same result",
            "model": "gpt-4o",
            "input_tokens": 100,
            "output_tokens": 50,
        })

As a Decorator

from agentloopguard import LoopGuard

guard = LoopGuard(max_cost_usd=10.00)

@guard.watch
def run_agent(prompt: str) -> str:
    # Your agent logic here
    ...

Custom Alert Handling

def my_alert_handler(detection_result):
    send_slack_notification(f"Loop detected: {detection_result.description}")

guard = LoopGuard(
    max_iterations=50,
    on_alert="callback",
    alert_callback=my_alert_handler,
)

Budget Tracking Only

from agentloopguard import BudgetTracker

tracker = BudgetTracker(max_cost_usd=20.00, max_tokens=1_000_000)

tracker.record(model="gpt-4o", input_tokens=500, output_tokens=200)
print(tracker.summary())
# {'total_cost_usd': 0.0055, 'total_tokens': 700, 'iterations': 1, ...}

Supported Models

Built-in pricing for:

  • OpenAI: gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo
  • Anthropic: claude-3.5-sonnet, claude-3-haiku, claude-3-opus
  • Google: gemini-1.5-pro, gemini-1.5-flash, gemini-2.0-flash

Configuration

Parameter Type Default Description
max_iterations int None Maximum number of agent steps
max_cost_usd float None Maximum total cost in USD
max_tokens int None Maximum total tokens (input + output)
max_duration_seconds float None Maximum wall-clock time
on_alert str "raise" Action on detection: "raise", "log", "callback"
alert_callback callable None Custom function called on detection
detectors list all four Which detection engines to use

Performance

  • <1ms overhead per record() call
  • Zero external dependencies — stdlib only
  • Thread-safe — works in async and multi-threaded agents
  • No network calls — everything runs locally

License

MIT — free forever. Use it in production. Use it in your startup. Use it everywhere.

Links


Built by developers who got a $400 surprise API bill.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentloopguard_sdk-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentloopguard_sdk-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file agentloopguard_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: agentloopguard_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for agentloopguard_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9211d2b86aec50830afc180da86397846055466f127c0955cc9f3e1e789aaab1
MD5 65357bb8280696926cd4b8a49a067d64
BLAKE2b-256 b8fd03299bd869c04db944ed14baa81db7831f69736880c5668f8348966bb24c

See more details on using hashes here.

File details

Details for the file agentloopguard_sdk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agentloopguard_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a3228e0934ae55ccccd0157e8825c94f25464114524458fcb909800bc297521
MD5 23c7aeace7d52d1b4182a5dec69137b5
BLAKE2b-256 284da8c328b196c849e5a776ed7448fddb4b8ce1f6f9a65006214af61fee02b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page