agent-cost-guardrails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cortex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,163 @@
1
+ # agent-cost-guardrails
2
+
3
+ [![npm version](https://img.shields.io/npm/v/agent-cost-guardrails)](https://www.npmjs.com/package/agent-cost-guardrails)
4
+ [![PyPI version](https://img.shields.io/pypi/v/agent-cost-guardrails)](https://pypi.org/project/agent-cost-guardrails/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ Budget limits and cost guardrails for AI agents. Prevents runaway API spend with hard budget enforcement, circuit breakers, and per-agent cost tracking.
8
+
9
+ **Zero infrastructure required** — no gateway, no proxy, no external service. Pure JavaScript/TypeScript middleware that hooks into your agent at the process level.
10
+
11
+ Also available for Python: `pip install agent-cost-guardrails`
12
+
13
+ ## Features
14
+
15
+ - Hard budget limits with `BudgetExceededError` on overspend
16
+ - Per-call token limits and tokens-per-minute rate limiting
17
+ - Circuit breaker that trips after N consecutive violations
18
+ - Alert callbacks at configurable thresholds (50%, 80%, 100%)
19
+ - Cost breakdown by model and agent
20
+ - Bundled pricing for 30+ models (OpenAI, Anthropic, Google, Mistral, DeepSeek, Meta)
21
+ - Custom pricing overrides for any model
22
+ - Full TypeScript support
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install agent-cost-guardrails
28
+ ```
29
+
30
+ ## Quick Start
31
+
32
+ ### Context Manager Style
33
+
34
+ ```typescript
35
+ import { BudgetGuard } from 'agent-cost-guardrails';
36
+
37
+ const guard = new BudgetGuard({ maxUsd: 5.00 });
38
+
39
+ // Before each LLM call
40
+ guard.preCallCheck(estimatedTokens);
41
+
42
+ // After each LLM call - record actual usage
43
+ const cost = guard.postCallRecord('gpt-4o', inputTokens, outputTokens);
44
+
45
+ console.log(guard.costReport());
46
+ ```
47
+
48
+ ### Async Wrapper
49
+
50
+ ```typescript
51
+ import { BudgetGuard } from 'agent-cost-guardrails';
52
+
53
+ const guard = new BudgetGuard({ maxUsd: 5.00 });
54
+ const result = await guard.run(async (g) => {
55
+ g.preCallCheck();
56
+ const response = await callLLM();
57
+ g.postCallRecord('gpt-4o', response.usage.prompt_tokens, response.usage.completion_tokens);
58
+ return response;
59
+ });
60
+
61
+ console.log(guard.costReport());
62
+ ```
63
+
64
+ ### Functional Style
65
+
66
+ ```typescript
67
+ import { withBudget } from 'agent-cost-guardrails';
68
+
69
+ const report = withBudget({ maxUsd: 5.00 }, (guard) => {
70
+ guard.preCallCheck();
71
+ guard.postCallRecord('gpt-4o', 1000, 500);
72
+ return guard.costReport();
73
+ });
74
+ ```
75
+
76
+ ## Alert Callbacks
77
+
78
+ ```typescript
79
+ import { BudgetGuard } from 'agent-cost-guardrails';
80
+
81
+ const guard = new BudgetGuard({
82
+ maxUsd: 10.00,
83
+ alertThresholds: [0.5, 0.8, 1.0],
84
+ onAlert: (threshold, currentCost, maxBudget) => {
85
+ console.warn(`ALERT: ${threshold * 100}% budget used ($${currentCost.toFixed(4)}/$${maxBudget.toFixed(2)})`);
86
+ },
87
+ });
88
+ ```
89
+
90
+ ## Custom Pricing
91
+
92
+ ```typescript
93
+ import { setCustomPricing } from 'agent-cost-guardrails';
94
+
95
+ setCustomPricing({
96
+ 'my-fine-tuned-model': {
97
+ input_per_mtok: 5.0, // $5.00 per 1M input tokens
98
+ output_per_mtok: 15.0, // $15.00 per 1M output tokens
99
+ }
100
+ });
101
+ ```
102
+
103
+ ## Cost Report
104
+
105
+ ```typescript
106
+ const report = guard.costReport();
107
+ // {
108
+ // totalCostUsd: 0.0325,
109
+ // totalInputTokens: 5000,
110
+ // totalOutputTokens: 2000,
111
+ // totalCalls: 3,
112
+ // budgetUsd: 10.0,
113
+ // remainingUsd: 9.9675,
114
+ // costByModel: { 'gpt-4o': 0.0325 },
115
+ // costByAgent: { 'agent-1': 0.02, 'agent-2': 0.0125 },
116
+ // tokensByModel: { 'gpt-4o': { input: 5000, output: 2000 } }
117
+ // }
118
+ ```
119
+
120
+ ## Error Handling
121
+
122
+ ```typescript
123
+ import {
124
+ BudgetGuard,
125
+ BudgetExceededError,
126
+ CircuitBreakerTrippedError,
127
+ RateLimitExceededError,
128
+ } from 'agent-cost-guardrails';
129
+
130
+ const guard = new BudgetGuard({ maxUsd: 1.0, circuitBreakerMaxViolations: 3 });
131
+
132
+ try {
133
+ guard.preCallCheck(estimatedTokens);
134
+ // ... call LLM ...
135
+ guard.postCallRecord('gpt-4o', inputTokens, outputTokens);
136
+ } catch (e) {
137
+ if (e instanceof BudgetExceededError) {
138
+ console.error(`Budget exceeded: $${e.spent.toFixed(4)} / $${e.budget}`);
139
+ } else if (e instanceof CircuitBreakerTrippedError) {
140
+ console.error(`Circuit breaker tripped (${e.violations} violations)`);
141
+ guard.reset(); // Reset to restore
142
+ } else if (e instanceof RateLimitExceededError) {
143
+ console.error(`Rate limit: ${e.tokensPerMin} tokens/min (limit: ${e.limit})`);
144
+ }
145
+ }
146
+ ```
147
+
148
+ ## Supported Models (Bundled Pricing)
149
+
150
+ | Provider | Models |
151
+ |----------|--------|
152
+ | **OpenAI** | gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-4, gpt-3.5-turbo, o1, o1-mini, o1-pro, o3-mini, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano |
153
+ | **Anthropic** | claude-opus-4-6, claude-sonnet-4-6, claude-haiku-4-5-20251001, claude-3-5-sonnet-20241022, claude-3-5-haiku-20241022, claude-3-opus, claude-3-sonnet, claude-3-haiku |
154
+ | **Google** | gemini-2.0-flash, gemini-2.0-pro, gemini-1.5-pro, gemini-1.5-flash |
155
+ | **Mistral** | mistral-large-latest, mistral-small-latest |
156
+ | **DeepSeek** | deepseek-chat, deepseek-reasoner |
157
+ | **Meta** | llama-3.1-405b, llama-3.1-70b, llama-3.1-8b |
158
+
159
+ Unknown models return `0` cost (won't crash). Use `setCustomPricing()` for unlisted models.
160
+
161
+ ## License
162
+
163
+ MIT
package/dist/core.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ import { CustomPricing } from './pricing';
2
+ export type AlertCallback = (threshold: number, currentCost: number, maxBudget: number) => void;
3
+ export interface BudgetConfig {
4
+ maxUsd: number;
5
+ maxTokensPerCall?: number;
6
+ maxTokensPerMinute?: number;
7
+ alertThresholds: number[];
8
+ circuitBreakerMaxViolations: number;
9
+ onAlert?: AlertCallback;
10
+ customPricing?: CustomPricing;
11
+ }
12
+ export interface CostReport {
13
+ totalCostUsd: number;
14
+ totalInputTokens: number;
15
+ totalOutputTokens: number;
16
+ totalCalls: number;
17
+ budgetUsd: number;
18
+ remainingUsd: number;
19
+ costByModel: Record<string, number>;
20
+ costByAgent: Record<string, number>;
21
+ tokensByModel: Record<string, {
22
+ input: number;
23
+ output: number;
24
+ }>;
25
+ }
26
+ export declare class CostTracker {
27
+ private config;
28
+ private totalCost;
29
+ private totalInputTokens;
30
+ private totalOutputTokens;
31
+ private calls;
32
+ private costByModel;
33
+ private costByAgent;
34
+ private tokensByModel;
35
+ private tokenLog;
36
+ private alertedThresholds;
37
+ constructor(config: BudgetConfig);
38
+ get currentCost(): number;
39
+ get currentCalls(): number;
40
+ get remainingBudget(): number;
41
+ checkBudget(): void;
42
+ checkRateLimit(tokens: number): void;
43
+ record(model: string, inputTokens: number, outputTokens: number, agentId?: string): number;
44
+ private checkAlerts;
45
+ costReport(): CostReport;
46
+ reset(): void;
47
+ }
48
+ export declare class CircuitBreaker {
49
+ private maxViolations;
50
+ private violationCount;
51
+ private tripped;
52
+ constructor(maxViolations?: number);
53
+ get isTripped(): boolean;
54
+ get violations(): number;
55
+ recordViolation(): void;
56
+ recordSuccess(): void;
57
+ check(): void;
58
+ reset(): void;
59
+ }
60
+ export interface BudgetGuardOptions {
61
+ maxUsd?: number;
62
+ maxTokensPerCall?: number;
63
+ maxTokensPerMinute?: number;
64
+ alertThresholds?: number[];
65
+ circuitBreakerMaxViolations?: number;
66
+ onAlert?: AlertCallback;
67
+ customPricing?: CustomPricing;
68
+ }
69
+ export declare class BudgetGuard {
70
+ config: BudgetConfig;
71
+ tracker: CostTracker;
72
+ circuitBreaker: CircuitBreaker;
73
+ constructor(options?: BudgetGuardOptions);
74
+ preCallCheck(estimatedTokens?: number): void;
75
+ postCallRecord(model: string, inputTokens: number, outputTokens: number, agentId?: string): number;
76
+ costReport(): CostReport;
77
+ reset(): void;
78
+ run<T>(fn: (guard: BudgetGuard) => Promise<T>): Promise<T>;
79
+ }
80
+ export declare function withBudget<T>(options: BudgetGuardOptions, fn: (guard: BudgetGuard) => T): T;
81
+ //# sourceMappingURL=core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AACA,OA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AAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,SAAY,GAAG,MAAM;IAUrG,UAAU,IAAI,UAAU;IAIxB,KAAK,IAAI,IAAI;IAKP,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAGjE;AAED,wBAAgB,UAAU,CAAC,CAAC,EAC1B,OAAO,EAAE,kBAAkB,EAC3B,EAAE,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,CAAC,GAC5B,CAAC,CAGH"}
package/dist/core.js ADDED
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BudgetGuard = exports.CircuitBreaker = exports.CostTracker = void 0;
4
+ exports.withBudget = withBudget;
5
+ const errors_1 = require("./errors");
6
+ const pricing_1 = require("./pricing");
7
+ class CostTracker {
8
+ constructor(config) {
9
+ this.totalCost = 0;
10
+ this.totalInputTokens = 0;
11
+ this.totalOutputTokens = 0;
12
+ this.calls = 0;
13
+ this.costByModel = {};
14
+ this.costByAgent = {};
15
+ this.tokensByModel = {};
16
+ this.tokenLog = []; // [timestamp_ms, tokens]
17
+ this.alertedThresholds = new Set();
18
+ this.config = config;
19
+ }
20
+ get currentCost() {
21
+ return this.totalCost;
22
+ }
23
+ get currentCalls() {
24
+ return this.calls;
25
+ }
26
+ get remainingBudget() {
27
+ return Math.max(0, this.config.maxUsd - this.totalCost);
28
+ }
29
+ checkBudget() {
30
+ if (this.totalCost >= this.config.maxUsd) {
31
+ throw new errors_1.BudgetExceededError(`Budget exceeded: $${this.totalCost.toFixed(4)} >= $${this.config.maxUsd.toFixed(2)}`, this.totalCost, this.config.maxUsd);
32
+ }
33
+ }
34
+ checkRateLimit(tokens) {
35
+ if (!this.config.maxTokensPerMinute)
36
+ return;
37
+ const now = Date.now();
38
+ const cutoff = now - 60000;
39
+ this.tokenLog = this.tokenLog.filter(([t]) => t > cutoff);
40
+ const recentTokens = this.tokenLog.reduce((sum, [, c]) => sum + c, 0) + tokens;
41
+ if (recentTokens > this.config.maxTokensPerMinute) {
42
+ throw new errors_1.RateLimitExceededError(`Rate limit: ${recentTokens} tokens/min exceeds ${this.config.maxTokensPerMinute}`, recentTokens, this.config.maxTokensPerMinute);
43
+ }
44
+ }
45
+ record(model, inputTokens, outputTokens, agentId = 'default') {
46
+ var _a, _b;
47
+ const cost = (0, pricing_1.calculateCost)(model, inputTokens, outputTokens, this.config.customPricing);
48
+ this.totalCost += cost;
49
+ this.totalInputTokens += inputTokens;
50
+ this.totalOutputTokens += outputTokens;
51
+ this.calls += 1;
52
+ this.costByModel[model] = ((_a = this.costByModel[model]) !== null && _a !== void 0 ? _a : 0) + cost;
53
+ this.costByAgent[agentId] = ((_b = this.costByAgent[agentId]) !== null && _b !== void 0 ? _b : 0) + cost;
54
+ if (!this.tokensByModel[model])
55
+ this.tokensByModel[model] = { input: 0, output: 0 };
56
+ this.tokensByModel[model].input += inputTokens;
57
+ this.tokensByModel[model].output += outputTokens;
58
+ this.tokenLog.push([Date.now(), inputTokens + outputTokens]);
59
+ this.checkAlerts(this.totalCost, this.config.maxUsd);
60
+ return cost;
61
+ }
62
+ checkAlerts(currentCost, maxUsd) {
63
+ if (!this.config.onAlert)
64
+ return;
65
+ const ratio = maxUsd > 0 ? currentCost / maxUsd : 0;
66
+ for (const threshold of this.config.alertThresholds) {
67
+ if (ratio >= threshold && !this.alertedThresholds.has(threshold)) {
68
+ this.alertedThresholds.add(threshold);
69
+ this.config.onAlert(threshold, currentCost, maxUsd);
70
+ }
71
+ }
72
+ }
73
+ costReport() {
74
+ return {
75
+ totalCostUsd: parseFloat(this.totalCost.toFixed(6)),
76
+ totalInputTokens: this.totalInputTokens,
77
+ totalOutputTokens: this.totalOutputTokens,
78
+ totalCalls: this.calls,
79
+ budgetUsd: this.config.maxUsd,
80
+ remainingUsd: parseFloat(Math.max(0, this.config.maxUsd - this.totalCost).toFixed(6)),
81
+ costByModel: { ...this.costByModel },
82
+ costByAgent: { ...this.costByAgent },
83
+ tokensByModel: Object.fromEntries(Object.entries(this.tokensByModel).map(([k, v]) => [k, { ...v }])),
84
+ };
85
+ }
86
+ reset() {
87
+ this.totalCost = 0;
88
+ this.totalInputTokens = 0;
89
+ this.totalOutputTokens = 0;
90
+ this.calls = 0;
91
+ this.costByModel = {};
92
+ this.costByAgent = {};
93
+ this.tokensByModel = {};
94
+ this.tokenLog = [];
95
+ this.alertedThresholds.clear();
96
+ }
97
+ }
98
+ exports.CostTracker = CostTracker;
99
+ class CircuitBreaker {
100
+ constructor(maxViolations = 3) {
101
+ this.violationCount = 0;
102
+ this.tripped = false;
103
+ this.maxViolations = maxViolations;
104
+ }
105
+ get isTripped() {
106
+ return this.tripped;
107
+ }
108
+ get violations() {
109
+ return this.violationCount;
110
+ }
111
+ recordViolation() {
112
+ this.violationCount += 1;
113
+ if (this.violationCount >= this.maxViolations) {
114
+ this.tripped = true;
115
+ }
116
+ }
117
+ recordSuccess() {
118
+ this.violationCount = 0;
119
+ }
120
+ check() {
121
+ if (this.tripped) {
122
+ throw new errors_1.CircuitBreakerTrippedError(`Circuit breaker tripped after ${this.violationCount} consecutive violations. Call reset() to restore.`, this.violationCount);
123
+ }
124
+ }
125
+ reset() {
126
+ this.violationCount = 0;
127
+ this.tripped = false;
128
+ }
129
+ }
130
+ exports.CircuitBreaker = CircuitBreaker;
131
+ class BudgetGuard {
132
+ constructor(options = {}) {
133
+ var _a, _b, _c;
134
+ this.config = {
135
+ maxUsd: (_a = options.maxUsd) !== null && _a !== void 0 ? _a : 10.0,
136
+ maxTokensPerCall: options.maxTokensPerCall,
137
+ maxTokensPerMinute: options.maxTokensPerMinute,
138
+ alertThresholds: (_b = options.alertThresholds) !== null && _b !== void 0 ? _b : [0.5, 0.8, 1.0],
139
+ circuitBreakerMaxViolations: (_c = options.circuitBreakerMaxViolations) !== null && _c !== void 0 ? _c : 3,
140
+ onAlert: options.onAlert,
141
+ customPricing: options.customPricing,
142
+ };
143
+ this.tracker = new CostTracker(this.config);
144
+ this.circuitBreaker = new CircuitBreaker(this.config.circuitBreakerMaxViolations);
145
+ }
146
+ preCallCheck(estimatedTokens) {
147
+ this.circuitBreaker.check();
148
+ this.tracker.checkBudget();
149
+ if (estimatedTokens !== undefined &&
150
+ this.config.maxTokensPerCall !== undefined &&
151
+ estimatedTokens > this.config.maxTokensPerCall) {
152
+ this.circuitBreaker.recordViolation();
153
+ throw new errors_1.BudgetExceededError(`Call token estimate ${estimatedTokens} exceeds per-call limit ${this.config.maxTokensPerCall}`, this.tracker.currentCost, this.config.maxUsd);
154
+ }
155
+ if (estimatedTokens !== undefined) {
156
+ this.tracker.checkRateLimit(estimatedTokens);
157
+ }
158
+ }
159
+ postCallRecord(model, inputTokens, outputTokens, agentId = 'default') {
160
+ const cost = this.tracker.record(model, inputTokens, outputTokens, agentId);
161
+ if (this.tracker.currentCost >= this.config.maxUsd) {
162
+ this.circuitBreaker.recordViolation();
163
+ }
164
+ else {
165
+ this.circuitBreaker.recordSuccess();
166
+ }
167
+ return cost;
168
+ }
169
+ costReport() {
170
+ return this.tracker.costReport();
171
+ }
172
+ reset() {
173
+ this.tracker.reset();
174
+ this.circuitBreaker.reset();
175
+ }
176
+ async run(fn) {
177
+ return fn(this);
178
+ }
179
+ }
180
+ exports.BudgetGuard = BudgetGuard;
181
+ function withBudget(options, fn) {
182
+ const guard = new BudgetGuard(options);
183
+ return fn(guard);
184
+ }
185
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;AA8PA,gCAMC;AApQD,qCAAmG;AACnG,uCAAyD;AA0BzD,MAAa,WAAW;IAYtB,YAAY,MAAoB;QAVxB,cAAS,GAAG,CAAC,CAAC;QACd,qBAAgB,GAAG,CAAC,CAAC;Q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KAAK,EAAE,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAI,EAAsC;QACjD,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC;CACF;AA7DD,kCA6DC;AAED,SAAgB,UAAU,CACxB,OAA2B,EAC3B,EAA6B;IAE7B,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC"}
@@ -0,0 +1,18 @@
1
+ export declare class GuardrailError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class BudgetExceededError extends GuardrailError {
5
+ spent: number;
6
+ budget: number;
7
+ constructor(message: string, spent?: number, budget?: number);
8
+ }
9
+ export declare class CircuitBreakerTrippedError extends GuardrailError {
10
+ violations: number;
11
+ constructor(message: string, violations?: number);
12
+ }
13
+ export declare class RateLimitExceededError extends GuardrailError {
14
+ tokensPerMin: number;
15
+ limit: number;
16
+ constructor(message: string, tokensPerMin?: number, limit?: number);
17
+ }
18
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,cAAe,SAAQ,KAAK;gBAC3B,OAAO,EAAE,MAAM;CAK5B;AAED,qBAAa,mBAAoB,SAAQ,cAAc;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;gBAEH,OAAO,EAAE,MAAM,EAAE,KAAK,SAAI,EAAE,MAAM,SAAI;CAOnD;AAED,qBAAa,0BAA2B,SAAQ,cAAc;IAC5D,UAAU,EAAE,MAAM,CAAC;gBAEP,OAAO,EAAE,MAAM,EAAE,UAAU,SAAI;CAM5C;AAED,qBAAa,sBAAuB,SAAQ,cAAc;IACxD,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;gBAEF,OAAO,EAAE,MAAM,EAAE,YAAY,SAAI,EAAE,KAAK,SAAI;CAOzD"}
package/dist/errors.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RateLimitExceededError = exports.CircuitBreakerTrippedError = exports.BudgetExceededError = exports.GuardrailError = void 0;
4
+ class GuardrailError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = 'GuardrailError';
8
+ Object.setPrototypeOf(this, new.target.prototype);
9
+ }
10
+ }
11
+ exports.GuardrailError = GuardrailError;
12
+ class BudgetExceededError extends GuardrailError {
13
+ constructor(message, spent = 0, budget = 0) {
14
+ super(message);
15
+ this.name = 'BudgetExceededError';
16
+ this.spent = spent;
17
+ this.budget = budget;
18
+ Object.setPrototypeOf(this, new.target.prototype);
19
+ }
20
+ }
21
+ exports.BudgetExceededError = BudgetExceededError;
22
+ class CircuitBreakerTrippedError extends GuardrailError {
23
+ constructor(message, violations = 0) {
24
+ super(message);
25
+ this.name = 'CircuitBreakerTrippedError';
26
+ this.violations = violations;
27
+ Object.setPrototypeOf(this, new.target.prototype);
28
+ }
29
+ }
30
+ exports.CircuitBreakerTrippedError = CircuitBreakerTrippedError;
31
+ class RateLimitExceededError extends GuardrailError {
32
+ constructor(message, tokensPerMin = 0, limit = 0) {
33
+ super(message);
34
+ this.name = 'RateLimitExceededError';
35
+ this.tokensPerMin = tokensPerMin;
36
+ this.limit = limit;
37
+ Object.setPrototypeOf(this, new.target.prototype);
38
+ }
39
+ }
40
+ exports.RateLimitExceededError = RateLimitExceededError;
41
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,cAAe,SAAQ,KAAK;IACvC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,MAAM,CAAC,cAAc,CAAC,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fig, BudgetGuardOptions, CostReport, AlertCallback } from './core';
3
+ export { BudgetExceededError, CircuitBreakerTrippedError, RateLimitExceededError, GuardrailError } from './errors';
4
+ export { getModelPrice, setCustomPricing, clearCustomPricing, calculateCost } from './pricing';
5
+ export type { ModelPrice, CustomPricing } from './pricing';
6
+ export declare const VERSION = "0.1.0";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9E,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACnH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC/F,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE3D,eAAO,MAAM,OAAO,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = exports.calculateCost = exports.clearCustomPricing = exports.setCustomPricing = exports.getModelPrice = exports.GuardrailError = exports.RateLimitExceededError = exports.CircuitBreakerTrippedError = exports.BudgetExceededError = exports.withBudget = exports.CircuitBreaker = exports.CostTracker = exports.BudgetGuard = void 0;
4
+ var core_1 = require("./core");
5
+ Object.defineProperty(exports, "BudgetGuard", { enumerable: true, get: function () { return core_1.BudgetGuard; } });
6
+ Object.defineProperty(exports, "CostTracker", { enumerable: true, get: function () { return core_1.CostTracker; } });
7
+ Object.defineProperty(exports, "CircuitBreaker", { enumerable: true, get: function () { return core_1.CircuitBreaker; } });
8
+ Object.defineProperty(exports, "withBudget", { enumerable: true, get: function () { return core_1.withBudget; } });
9
+ var errors_1 = require("./errors");
10
+ Object.defineProperty(exports, "BudgetExceededError", { enumerable: true, get: function () { return errors_1.BudgetExceededError; } });
11
+ Object.defineProperty(exports, "CircuitBreakerTrippedError", { enumerable: true, get: function () { return errors_1.CircuitBreakerTrippedError; } });
12
+ Object.defineProperty(exports, "RateLimitExceededError", { enumerable: true, get: function () { return errors_1.RateLimitExceededError; } });
13
+ Object.defineProperty(exports, "GuardrailError", { enumerable: true, get: function () { return errors_1.GuardrailError; } });
14
+ var pricing_1 = require("./pricing");
15
+ Object.defineProperty(exports, "getModelPrice", { enumerable: true, get: function () { return pricing_1.getModelPrice; } });
16
+ Object.defineProperty(exports, "setCustomPricing", { enumerable: true, get: function () { return pricing_1.setCustomPricing; } });
17
+ Object.defineProperty(exports, "clearCustomPricing", { enumerable: true, get: function () { return pricing_1.clearCustomPricing; } });
18
+ Object.defineProperty(exports, "calculateCost", { enumerable: true, get: function () { return pricing_1.calculateCost; } });
19
+ exports.VERSION = '0.1.0';
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+BAA8E;AAArE,mGAAA,WAAW,OAAA;AAAE,mGAAA,WAAW,OAAA;AAAE,sGAAA,cAAc,OAAA;AAAE,kGAAA,UAAU,OAAA;AAE7D,mCAAmH;AAA1G,6GAAA,mBAAmB,OAAA;AAAE,oHAAA,0BAA0B,OAAA;AAAE,gHAAA,sBAAsB,OAAA;AAAE,wGAAA,cAAc,OAAA;AAChG,qCAA+F;AAAtF,wGAAA,aAAa,OAAA;AAAE,2GAAA,gBAAgB,OAAA;AAAE,6GAAA,kBAAkB,OAAA;AAAE,wGAAA,aAAa,OAAA;AAG9D,QAAA,OAAO,GAAG,OAAO,CAAC"}
@@ -0,0 +1,13 @@
1
+ export interface ModelPrice {
2
+ inputPerMtok: number;
3
+ outputPerMtok: number;
4
+ }
5
+ export type CustomPricing = Record<string, {
6
+ input_per_mtok: number;
7
+ output_per_mtok: number;
8
+ }>;
9
+ export declare function setCustomPricing(prices: CustomPricing): void;
10
+ export declare function clearCustomPricing(): void;
11
+ export declare function getModelPrice(model: string): ModelPrice | null;
12
+ export declare function calculateCost(model: string, inputTokens: number, outputTokens: number, custom?: CustomPricing): number;
13
+ //# sourceMappingURL=pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE;IAAE,cAAc,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AA8ChG,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI,CAO5D;AAED,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAS9D;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,MAAM,CAAC,EAAE,aAAa,GACrB,MAAM,CASR"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setCustomPricing = setCustomPricing;
4
+ exports.clearCustomPricing = clearCustomPricing;
5
+ exports.getModelPrice = getModelPrice;
6
+ exports.calculateCost = calculateCost;
7
+ // Bundled pricing table — covers major models.
8
+ // Prices are per 1M tokens in USD.
9
+ const PRICING = {
10
+ // OpenAI
11
+ 'gpt-4o': { inputPerMtok: 2.50, outputPerMtok: 10.00 },
12
+ 'gpt-4o-mini': { inputPerMtok: 0.15, outputPerMtok: 0.60 },
13
+ 'gpt-4-turbo': { inputPerMtok: 10.00, outputPerMtok: 30.00 },
14
+ 'gpt-4': { inputPerMtok: 30.00, outputPerMtok: 60.00 },
15
+ 'gpt-3.5-turbo': { inputPerMtok: 0.50, outputPerMtok: 1.50 },
16
+ 'o1': { inputPerMtok: 15.00, outputPerMtok: 60.00 },
17
+ 'o1-mini': { inputPerMtok: 3.00, outputPerMtok: 12.00 },
18
+ 'o1-pro': { inputPerMtok: 150.00, outputPerMtok: 600.00 },
19
+ 'o3-mini': { inputPerMtok: 1.10, outputPerMtok: 4.40 },
20
+ 'gpt-4.1': { inputPerMtok: 2.00, outputPerMtok: 8.00 },
21
+ 'gpt-4.1-mini': { inputPerMtok: 0.40, outputPerMtok: 1.60 },
22
+ 'gpt-4.1-nano': { inputPerMtok: 0.10, outputPerMtok: 0.40 },
23
+ // Anthropic
24
+ 'claude-opus-4-6': { inputPerMtok: 15.00, outputPerMtok: 75.00 },
25
+ 'claude-sonnet-4-6': { inputPerMtok: 3.00, outputPerMtok: 15.00 },
26
+ 'claude-haiku-4-5-20251001': { inputPerMtok: 0.80, outputPerMtok: 4.00 },
27
+ 'claude-3-5-sonnet-20241022': { inputPerMtok: 3.00, outputPerMtok: 15.00 },
28
+ 'claude-3-5-haiku-20241022': { inputPerMtok: 0.80, outputPerMtok: 4.00 },
29
+ 'claude-3-opus-20240229': { inputPerMtok: 15.00, outputPerMtok: 75.00 },
30
+ 'claude-3-sonnet-20240229': { inputPerMtok: 3.00, outputPerMtok: 15.00 },
31
+ 'claude-3-haiku-20240307': { inputPerMtok: 0.25, outputPerMtok: 1.25 },
32
+ // Google
33
+ 'gemini-2.0-flash': { inputPerMtok: 0.10, outputPerMtok: 0.40 },
34
+ 'gemini-2.0-pro': { inputPerMtok: 1.25, outputPerMtok: 10.00 },
35
+ 'gemini-1.5-pro': { inputPerMtok: 1.25, outputPerMtok: 5.00 },
36
+ 'gemini-1.5-flash': { inputPerMtok: 0.075, outputPerMtok: 0.30 },
37
+ // Mistral
38
+ 'mistral-large-latest': { inputPerMtok: 2.00, outputPerMtok: 6.00 },
39
+ 'mistral-small-latest': { inputPerMtok: 0.10, outputPerMtok: 0.30 },
40
+ // DeepSeek
41
+ 'deepseek-chat': { inputPerMtok: 0.27, outputPerMtok: 1.10 },
42
+ 'deepseek-reasoner': { inputPerMtok: 0.55, outputPerMtok: 2.19 },
43
+ // Meta (via providers)
44
+ 'llama-3.1-405b': { inputPerMtok: 3.00, outputPerMtok: 3.00 },
45
+ 'llama-3.1-70b': { inputPerMtok: 0.88, outputPerMtok: 0.88 },
46
+ 'llama-3.1-8b': { inputPerMtok: 0.18, outputPerMtok: 0.18 },
47
+ };
48
+ let customPricing = {};
49
+ function setCustomPricing(prices) {
50
+ for (const [model, rates] of Object.entries(prices)) {
51
+ customPricing[model] = {
52
+ inputPerMtok: rates.input_per_mtok,
53
+ outputPerMtok: rates.output_per_mtok,
54
+ };
55
+ }
56
+ }
57
+ function clearCustomPricing() {
58
+ customPricing = {};
59
+ }
60
+ function getModelPrice(model) {
61
+ if (customPricing[model])
62
+ return customPricing[model];
63
+ if (PRICING[model])
64
+ return PRICING[model];
65
+ // Prefix matching (e.g., "gpt-4o-2024-05-13" -> "gpt-4o")
66
+ const keys = Object.keys(PRICING).sort((a, b) => b.length - a.length);
67
+ for (const key of keys) {
68
+ if (model.startsWith(key))
69
+ return PRICING[key];
70
+ }
71
+ return null;
72
+ }
73
+ function calculateCost(model, inputTokens, outputTokens, custom) {
74
+ let price = null;
75
+ if (custom && custom[model]) {
76
+ price = { inputPerMtok: custom[model].input_per_mtok, outputPerMtok: custom[model].output_per_mtok };
77
+ }
78
+ else {
79
+ price = getModelPrice(model);
80
+ }
81
+ if (!price)
82
+ return 0;
83
+ return (inputTokens * price.inputPerMtok / 1000000) + (outputTokens * price.outputPerMtok / 1000000);
84
+ }
85
+ //# sourceMappingURL=pricing.js.map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AG,EAAE,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,cAAc,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,eAAe,EAAE,CAAC;IACvG,CAAC;SAAM,CAAC;QACN,KAAK,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI,CAAC,KAAK;QAAE,OAAO,CAAC,CAAC;IACrB,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,YAAY,GAAG,OAAS,CAAC,GAAG,CAAC,YAAY,GAAG,KAAK,CAAC,aAAa,GAAG,OAAS,CAAC,CAAC;AAC3G,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "agent-cost-guardrails",
3
+ "version": "0.1.0",
4
+ "description": "Budget limits and cost guardrails for AI agents. Prevents runaway API spend with hard budget enforcement, circuit breakers, and per-agent cost tracking.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "README.md",
10
+ "LICENSE"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "test": "node --experimental-vm-modules node_modules/.bin/jest",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "keywords": [
18
+ "ai",
19
+ "agents",
20
+ "cost",
21
+ "budget",
22
+ "guardrails",
23
+ "llm",
24
+ "openai",
25
+ "anthropic",
26
+ "langchain",
27
+ "langgraph",
28
+ "token-limit",
29
+ "circuit-breaker"
30
+ ],
31
+ "author": "sapph1re",
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/sapph1re/agent-cost-guardrails.git"
36
+ },
37
+ "homepage": "https://github.com/sapph1re/agent-cost-guardrails",
38
+ "bugs": {
39
+ "url": "https://github.com/sapph1re/agent-cost-guardrails/issues"
40
+ },
41
+ "devDependencies": {
42
+ "@types/jest": "^29.5.14",
43
+ "jest": "^29.7.0",
44
+ "ts-jest": "^29.4.9",
45
+ "typescript": "^5.9.3"
46
+ },
47
+ "jest": {
48
+ "preset": "ts-jest",
49
+ "testEnvironment": "node",
50
+ "testMatch": [
51
+ "**/tests/**/*.test.ts"
52
+ ]
53
+ }
54
+ }