# AgentGuard – Zero-dependency loop and spend guardrails for AI agents

> Source: <https://github.com/nemor1-bit/agentguard>
> Published: 2026-12-01 02:49:30+00:00

**Key takeaway:** AgentGuard wraps agent tools with a @guard.protect decorator that enforces per-action and per-session spend caps, kills loops after max_consecutive_repeats identical tool calls, blocks duplicate (action, args, kwargs) retries within a 300-second idempotency window, and pauses irreversible actions fo

**The zero-dependency runtime seatbelt, loop killer, and human-approval gate for AI Agents.**

[https://nemor1-bit.github.io/agentguard/](https://nemor1-bit.github.io/agentguard/)

AgentGuard protects production AI agents from the quiet failures that dollar caps alone miss—stopping consecutive tool loops, duplicate retries on timeouts, unverified side effects, and unapproved irreversible actions.

```
pip install git+https://github.com/nemor1-bit/agentguard.git
python
from agentguard import AgentGuard

guard = AgentGuard(
    max_spend=50,                       # Max \$50 per action
    max_total_spend=500,                # Max \$500 across the session
    max_consecutive_repeats=4,          # Kill stuck loops repeating the same tool
    idempotency_window=300,             # Block duplicate retries within 5 minutes
    require_approval_for=["send_email", "charge_card"],
    approval_callback=lambda action, cost: input(f"Approve {action}? (y/n): ") == "y",
    webhook_url="https://slack.com"
)

# Protect any tool with human approval + verify-after-act
@guard.protect(
    cost_fn=lambda user_id, amount: amount,
    verify_fn=lambda result, user_id, amount: result.get("paid") is True
)
def charge_card(user_id, amount):
    return {"paid": True, "tx_id": "tx_991"}

charge_card("usr_42", 25)  # Pauses for human approval -> executes -> verifies effect
charge_card("usr_42", 25)  # 🛑 Blocked automatically (duplicate action within 300s)
```

- 
**Aggregate Dollar-Velocity Caps (`max_spend_per_minute`):** Stops rapid spend bleed (e.g., ten $2 calls in 60 seconds) even when every single action is under`max_spend` .
- 
**Strict Tool Allowlisting (`allowed_tools`):** Eliminates keyword whack-a-mole by blocking any tool not explicitly allowlisted.
- 
**Dry-Run / Shadow Preview Mode (`dry_run=True`):** Simulates full agent workflows through every policy check and outputs a projected spend receipt (`get_dry_run_summary()` ) before committing real side effects.
- 
**Consecutive Tool-Loop Detection (`max_consecutive_repeats`):** Halts runs when an agent calls the same tool repeatedly—even if arguments change slightly or calls are spaced out.
- 
**Human-in-the-Loop Approval Gate (`require_approval_for`):** Pauses execution on irreversible actions (` send` ,`pay` ,`delete` ) until a human approves via callback or webhook.
- 
**Duplicate-Action Prevention (Idempotency):** Automatically hashes`(action, args, kwargs)` to block double-charges and duplicate emails after API timeouts.
- 
**Verify-After-Act (`verify_fn`):** Runs a lightweight post-execution check to catch silent failures when an agent reports success without the real-world effect occurring.
- 
**Two-Phase Variable Cost Accounting:** Supports dynamic pre-flight`cost_fn` estimation plus`record_actual_cost()` post-call reconciliation.
- 
**OpenAI Tool Validation & Alerts:** Native`guard_openai_tool_call()` , keyword kill-switches, timestamped audit logs, and Slack/Discord alerts.
- 
Need distributed Redis idempotency, SOC2 compliance logs, and RBAC for 10,000+ agents? [Join the Enterprise Waitlist](https://nemor1-bit.github.io/agentguard/) .
- 
Need hands-on help wiring AgentGuard into your production OpenAI or LangChain agents? [Book a Priority Setup & Custom Guardrails session ($750)](https://buy.stripe.com/00wbJ2ceT7yd9hA5oh3F600) .
