๐ก๏ธ AgentGuard
July 31, 2025 ยท View on GitHub
๐จ The Problem
Your AI agent has a bug. It makes 1000 API calls in a loop. Your $2000 credit card gets charged.
This happens to developers every week:
- Infinite loops in AI workflows
- Testing with production API keys
- Agents that don't know when to stop
- One typo = hundreds of dollars gone
Existing tools only tell you after the damage is done.
๐ก The Solution
AgentGuard automatically kills your process before it burns through your budget.
// Add 2 lines to any AI project:
const agentGuard = require('agent-guard');
await agentGuard.init({ limit: 50 }); // \$50 budget limit
// Your code runs normally until it hits \$50, then AgentGuard stops it
const response = await openai.chat.completions.create({...});
Result: Instead of losing $2000, you lose $50 and get a detailed report.
๐ How It Works
graph TD
A["๐ค Your AI Agent Starts"] --> B["๐ฆ AgentGuard.init({ limit: \$50 })"]
B --> C["๐ Monitors All AI API Calls"]
C --> D["๐ฐ Tracks Real-Time Costs"]
D --> E{"๐ธ Cost โฅ \$50?"}
E -->|No| F["โ
Continue Running"]
E -->|Yes| G["๐ Emergency Stop"]
F --> C
G --> H["๐ Show Savings Report"]
H --> I["๐พ Log Final Statistics"]
style A fill:#e1f5fe
style B fill:#f3e5f5
style C fill:#fff3e0
style D fill:#fff3e0
style E fill:#ffebee
style G fill:#ffcdd2
style H fill:#e8f5e8
Real-Time Monitoring
sequenceDiagram
participant App as ๐ค Your App
participant AG as ๐ก๏ธ AgentGuard
participant API as ๐ AI API
participant User as ๐ค Developer
App->>AG: init({ limit: \$50, mode: "throw" })
AG->>App: โ
Protection Active
loop Every AI Call
App->>API: API Request (OpenAI/Anthropic)
API->>App: Response + Usage Data
AG->>AG: ๐ฐ Calculate Cost (\$0.0243)
AG->>AG: ๐ Update Total (\$47.23 / \$50)
alt Cost Under Limit
AG->>App: โ
Continue
else Cost Exceeds Limit
AG->>App: ๐ throw AgentGuardError
AG->>User: ๐ฑ Webhook Notification
AG->>User: ๐ฐ "Saved you ~\$200!"
end
end
Protection Modes
graph LR
subgraph "๐ ๏ธ Protection Modes"
A["mode: 'throw'<br/>๐ก๏ธ Safest"]
B["mode: 'notify'<br/>โ ๏ธ Warning"]
C["mode: 'kill'<br/>๐ Nuclear"]
end
subgraph "๐ฏ When Limit Exceeded"
A --> D["โ
Throws recoverable error<br/>๐ Allows cleanup<br/>๐ Graceful shutdown"]
B --> E["โ ๏ธ Logs warning<br/>๐ Continues monitoring<br/>๐จ Sends notifications"]
C --> F["๐ process.exit(1)<br/>๐ซ Immediate termination<br/>โก No cleanup"]
end
style A fill:#e8f5e8
style B fill:#fff3e0
style C fill:#ffebee
style D fill:#e8f5e8
style E fill:#fff3e0
style F fill:#ffebee
Why AgentGuard vs Other Tools?
| Tool | What It Does | When You Find Out About Problems |
|---|---|---|
| OpenAI Dashboard | Shows usage after it happens | Hours later via email |
| LangChain Callbacks | Tracks tokens in your code | After script finishes |
| tokencost | Estimates costs beforehand | Before you make calls |
| AgentGuard | Stops execution when limit hit | Immediately, mid-execution |
AgentGuard is the only tool that actually prevents runaway costs in real-time.
Quick Start
1. Install
npm install agent-guard
2. Add Protection
const agentGuard = require('agent-guard');
await agentGuard.init({ limit: 50 }); // \$50 budget
// Your existing AI code works unchanged
const response = await openai.chat.completions.create({...});
3. Watch It Work
๐ก๏ธ AgentGuard v1.2.1 initialized
๐ฐ Budget protection: \$50 (mode: throw)
๐ \$12.34 / \$50.00 (24.7%) | OpenAI API tracked
๐ COST LIMIT EXCEEDED - Saved you ~\$2000!
Quick Start
// Step 1: Add these two lines to your AI agent
const agentGuard = require('agent-guard');
await agentGuard.init({ limit: 25 }); // \$25 budget limit
// Step 2: Your existing code works unchanged
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello world' }]
});
console.log('Response:', response); // โ AgentGuard automatically tracks this
// Real-time protection: ๐ก๏ธ \$12.34 / \$25.00 49.4%
Production Configuration
try {
const guard = await agentGuard.init({
limit: 100, // Budget limit in USD
mode: 'throw', // Safe error vs 'kill' (hard exit)
webhook: 'https://hooks.slack.com/...', // Slack/Discord alerts
redis: 'redis://localhost:6379', // Multi-process shared budgets
privacy: true // Redact sensitive data
});
// Your AI agent code here...
} catch (error) {
if (error.message.includes('AGENTGUARD_LIMIT_EXCEEDED')) {
console.log('Budget protection activated:', error.agentGuardData);
// Handle gracefully: save state, notify, switch to cheaper model, etc.
}
}
What You'll See
--------------------------------------------------
๐ก๏ธ AgentGuard v1.2.0 initialized
๐ฐ Budget protection: \$25 (mode: throw)
๐ก Monitoring: console.log, fetch, axios, undici, got
--------------------------------------------------
\$0.23 / \$25.00 0.9% # Real-time cost tracking
\$2.45 / \$25.00 9.8% # Updates with each API call
\$20.10 / \$25.00 80.4% # Warning at 80%
\$24.89 / \$25.00 99.6% # Danger at 90%
๐ AGENTGUARD: COST LIMIT EXCEEDED - Saved you ~\$75.00
๐ฐ Total cost when stopped: \$24.89
๐ Budget used: 99.6%
๐ก๏ธ Mode: throw - gracefully stopping with recoverable error
๐ก Real-World Examples
Development Protection
// Protect development scripts from expensive mistakes
await agentGuard.init({ limit: 10, mode: 'throw' });
// Safely experiment with AI without surprise bills
Production Deployment
// Multi-process protection with Redis
await agentGuard.init({
limit: 1000,
mode: 'throw',
redis: 'redis://production:6379',
webhook: 'https://hooks.slack.com/alerts'
});
Browser Applications
<script src="https://unpkg.com/agent-guard@latest/dist/agent-guard.min.js"></script>
<script>
AgentGuard.init({ limit: 50, mode: 'notify' });
// Your browser AI agent runs with cost protection
</script>
Dynamic Budget Management
const guard = await agentGuard.init({ limit: 100 });
// Check costs anytime
console.log(`Spent: $${guard.getCost()}`);
// Adjust for high-priority tasks
if (urgentTask) guard.setLimit(500);
// Reset for new session
await guard.reset();
๐ฏ Live Protection Examples
See AgentGuard prevent real runaway costs:
# Runaway loop protection (simulates infinite AI loop)
node examples/runaway-loop-demo.js
# Real customer workflow with budget protection
node examples/real-customer-demo.js
# LangChain integration example
node examples/langchain-example.js
# Interactive browser demo
open examples/test-browser.html
What you'll see: Real-time cost tracking, automatic protection activation, and graceful error handling that saves money.
๐ง How Real-Time Protection Works
Automatic AI API Interception
No code changes needed - AgentGuard automatically monitors:
- fetch() - Global HTTP request interception
- axios - Automatic response processing
- undici/got - Modern Node.js HTTP clients
- console.log() - API response detection in logs
- http/https - Raw Node.js request monitoring
Accurate Cost Calculation
- Real tokenizers: OpenAI's
tiktoken+ Anthropic's official tokenizer - Live pricing: Fetches current rates from community sources
- Streaming support: Accumulates tokens from partial responses
- Multimodal: Handles images, audio, and complex content
- Smart fallback: Accurate estimation when tokenizers unavailable
Protection Activation
- Real-time tracking: Every API call updates budget
- Threshold warnings: Visual alerts at 80% and 90%
- Limit enforcement: Automatic protection when budget exceeded
- Graceful handling: Throws catchable error vs hard process exit
- Cost data: Detailed breakdown for recovery decisions
Supports all major providers: OpenAI, Anthropic, auto-detected from URLs
๐ What Gets Protected
- ๐ก๏ธ Infinite loops calling AI APIs
- ๐ก๏ธ Expensive model calls (GPT-4, Claude Opus)
- ๐ก๏ธ Recursive agent calls with bugs
- ๐ก๏ธ Development workflows with cost oversight
- ๐ก๏ธ Runaway RAG document processing
๐ Security & Reliability
Privacy Protection
await agentGuard.init({
privacy: true, // Redacts request/response content from logs
silent: true // Disables cost display for sensitive environments
});
- Data redaction: Request/response bodies marked as
[REDACTED] - URL filtering: Sensitive API endpoints optionally hidden
- Local operation: No data sent to external services
- Memory safety: Automatic cleanup of sensitive data
Failure Mode Safety
// Graceful degradation (recommended)
mode: 'throw' // Throws catchable AgentGuardError
mode: 'notify' // Warns but continues execution
mode: 'kill' // Hard process termination (use sparingly)
Why soft failures matter:
- โ Preserves database transactions
- โ Allows graceful cleanup
- โ Enables error recovery
- โ Protects worker threads
Multi-Process Protection
await agentGuard.init({
redis: 'redis://localhost:6379', // Shared budget across processes
limit: 100 // Combined limit for all instances
});
๐ ๏ธ API Reference
init(options)
Initializes AgentGuard with the specified options.
const agentGuard = require('agent-guard');
const guard = await agentGuard.init({
limit: 50, // Cost limit in USD
mode: 'throw', // 'throw' | 'notify' | 'kill'
webhook: null, // Webhook URL for notifications
redis: null, // Redis URL for multi-process tracking
privacy: false, // Redact sensitive data
silent: false // Hide cost updates
});
Guard Instance Methods
// Cost monitoring
guard.getCost() // Current total cost
guard.getLimit() // Current budget limit
guard.getLogs() // Detailed API call history
// Budget management
guard.setLimit(200) // Update budget limit
guard.reset() // Reset costs to \$0
// Configuration
guard.setMode('notify') // Change protection mode
guard.updatePrices() // Refresh live pricing data
๐ค Contributing
We love contributions! See our Contributing Guide for details.
git clone https://github.com/dipampaul17/AgentGuard.git
cd AgentGuard
node verify-installation.js
๐ License
MIT - Use anywhere, even commercial projects.
๐ Support
- ๐ Bug Reports: GitHub Issues
- ๐ฌ Questions: GitHub Discussions
- ๐ Documentation: API Reference โข Quick Start
๐ฆ What's Included
- โ Real-time protection - Autonomous cost prevention that actually works
- โ Production ready - TypeScript definitions, browser support, Redis integration
- โ Live examples - LangChain integration, runaway protection demos
- โ Comprehensive docs - API reference, security guide, comparison analysis
AgentGuard: The only tool that stops AI runaway costs before they happen.
Real-time budget enforcement โข Graceful error handling โข Zero code changes
โญ Star on GitHub โข ๐ฆ Install from NPM โข ๐ See Comparison