- Installation
- Quick Start
- Core Functions
- Configuration Options
- Instance Methods
- Supported Models
- Advanced Usage
- TypeScript Support
npm install agent-guardOr download directly:
curl -O https://raw.githubusercontent.com/dipampaul17/AgentGuard/main/agent-guard.jsconst agentGuard = require('agent-guard');
const guard = agentGuard.init({ limit: 50 });
// Your AI agent code continues normally
// AgentGuard automatically tracks costsInitialize AgentGuard with specified configuration.
const guard = await agentGuard.init({
limit: 50, // Required: cost limit in USD
webhook: '...', // Optional: webhook URL
silent: false, // Optional: suppress console output
mode: 'kill', // Optional: action on limit exceeded
redis: '...' // Optional: Redis URL for multi-process
});Parameters:
options(Object): Configuration object
Returns:
AgentGuardInstance: Instance with methods for runtime control
Example:
// Basic usage
const guard = agentGuard.init({ limit: 10 });
// Advanced configuration
const guard = await agentGuard.init({
limit: 100,
webhook: 'https://hooks.slack.com/services/...',
mode: 'throw',
redis: 'redis://localhost:6379'
});Manually update model prices from live APIs.
await agentGuard.updatePrices();Note: This is called automatically on initialization but can be triggered manually.
- Type:
number - Description: Maximum cost in USD before action is taken
- Example:
limit: 50(stop at $50)
- Type:
string | null - Default:
null - Description: URL to receive notifications when limit is reached
- Supported: Slack, Discord, custom endpoints
- Example:
webhook: 'https://hooks.slack.com/services/...'
- Type:
boolean - Default:
false - Description: Suppress console output for cost updates
- Example:
silent: true
- Type:
'kill' | 'throw' | 'notify' - Default:
'kill' - Description: Action when cost limit is exceeded
'kill': Immediately terminate the process'throw': Throw an error that can be caught'notify': Log warning and continue execution
- Example:
mode: 'throw'
- Type:
string | null - Default:
null - Description: Redis connection URL for multi-process budget tracking
- Example:
redis: 'redis://localhost:6379'
- Type:
boolean - Default:
true - Description: Whether AgentGuard monitoring is active
- Example:
enabled: process.env.NODE_ENV === 'production'
Get current accumulated cost in USD.
const currentCost = guard.getCost();
console.log(`Current cost: $${currentCost.toFixed(4)}`);Returns:
number: Current cost in USDnull: When using Redis mode (multi-process tracking)
Get current cost limit.
const limit = guard.getLimit();
console.log(`Cost limit: $${limit}`);Returns:
number: Cost limit in USD
Update cost limit dynamically.
guard.setLimit(100); // Increase limit to $100Parameters:
newLimit(number): New cost limit in USD
Change the action mode dynamically.
guard.setMode('notify'); // Switch to notify-only modeParameters:
newMode('kill' | 'throw' | 'notify'): New mode
Disable AgentGuard monitoring.
guard.disable(); // Stop tracking costsReset cost counter and clear logs.
await guard.reset(); // Reset to $0Note: In Redis mode, this clears the shared budget.
Get array of all tracked API calls.
const logs = guard.getLogs();
logs.forEach(log => {
console.log(`${log.model}: $${log.cost.toFixed(4)}`);
});Returns:
[
{
timestamp: 1677652288000,
model: 'gpt-4',
cost: 0.0045,
tokens: { input: 100, output: 50 },
url: 'https://api.openai.com/v1/chat/completions'
},
// ...
]gpt-4- $0.03/1K input, $0.06/1K outputgpt-4-turbo- $0.01/1K input, $0.03/1K outputgpt-3.5-turbo- $0.0015/1K input, $0.002/1K outputgpt-4o- $0.0025/1K input, $0.01/1K outputgpt-4o-mini- $0.00015/1K input, $0.0006/1K output
claude-3-opus- $0.015/1K input, $0.075/1K outputclaude-3-sonnet- $0.003/1K input, $0.015/1K outputclaude-3-haiku- $0.00025/1K input, $0.00125/1K outputclaude-3-5-sonnet- $0.003/1K input, $0.015/1K output
Unknown models use default pricing: $0.01/1K input, $0.03/1K output
// All processes share the same budget
const guard = await agentGuard.init({
limit: 100,
redis: process.env.REDIS_URL || 'redis://localhost:6379'
});
// Note: getCost() returns null in Redis mode
// Budget is tracked centrally across all processesconst guard = agentGuard.init({
limit: 10,
mode: 'throw'
});
try {
// Your AI agent code
await runExpensiveAgent();
} catch (error) {
if (error.message.includes('AGENTGUARD_LIMIT_EXCEEDED')) {
console.log('Cost limit reached, handling gracefully...');
// Clean up, save state, etc.
} else {
throw error;
}
}const guard = agentGuard.init({ limit: 50 });
// Adjust based on user tier
if (user.plan === 'premium') {
guard.setLimit(500);
} else if (user.plan === 'basic') {
guard.setLimit(50);
}
// Switch modes based on environment
if (process.env.NODE_ENV === 'development') {
guard.setMode('notify'); // Don't kill in dev
}const guard = agentGuard.init({
limit: 100,
webhook: process.env.WEBHOOK_URL
});
// Webhook receives POST with:
{
"text": "AGENTGUARD: COST LIMIT EXCEEDED - Saved you ~$400.00",
"timestamp": "2024-01-20T10:30:00.000Z",
"cost": 100.2345,
"limit": 100
}<script src="https://unpkg.com/agent-guard/dist/agent-guard.min.js"></script>
<script>
const guard = AgentGuard.init({
limit: 25,
silent: false
});
// Browser agent code
async function runAgent() {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
// ...
});
console.log(await response.json()); // Tracked by AgentGuard
}
</script>AgentGuard includes TypeScript definitions:
import { init, AgentGuardConfig, AgentGuardInstance } from 'agent-guard';
const config: AgentGuardConfig = {
limit: 50,
mode: 'throw',
webhook: process.env.SLACK_WEBHOOK
};
const guard: AgentGuardInstance = await init(config);
// Type-safe access to methods
const cost: number | null = guard.getCost();
const logs = guard.getLogs(); // Typed as ApiCallLog[]- Initialize Early: Call
init()before any AI API usage - Set Appropriate Limits: Consider your use case and budget
- Use Throw Mode for Graceful Handling: Allows cleanup before exit
- Monitor in Production: Use webhooks for alerts
- Test Limits: Verify protection works in development
- Multi-Process Apps: Use Redis for shared budgets
- Regular Price Updates: Prices update automatically, but can be triggered manually
- Ensure you're logging API responses:
console.log(response) - Check that response includes usage/token information
- Verify AgentGuard is initialized before API calls
- AgentGuard falls back to local tracking on Redis errors
- Check Redis URL and connectivity
- Monitor console warnings for connection issues
- Verify webhook URL is correct and accessible
- Check for network/firewall issues
- Monitor console for webhook errors
- Ensure
mode: 'kill'is set (default) - Check if another error handler is catching the exit
- Verify the limit hasn't been increased dynamically