ai-spend-guard
v0.1.0
Published
Cost-control guardrails for OpenAI- and Hugging Face-compatible chat calls.
Downloads
33
Maintainers
Readme
AI Spend Guard
Stop your AI calls from wasting money.
The Problem
Running AI calls in production gets expensive fast. A single classification task might cost $0.66 with GPT-4o when GPT-4o-mini would've cost $0.02.
Without guardrails: $0.66
With Spend Guard: $0.02
Saved: 97%
Install
npm install ai-spend-guardQuick start (policy only)
import { createSpendGuard } from "ai-spend-guard";
const guard = createSpendGuard({
cacheRepeatedPrompts: true,
autoReroute: true,
maxCostPerCall: 0.2
});
const result = guard.evaluate({
task: "classification",
model: "gpt-4o",
prompt: "Classify this support ticket",
estimatedInputTokens: 180,
estimatedOutputTokens: 40
});
console.log(result.action, result.model, result.savingsUsd);Wrap live calls
import { createAIGuard } from "ai-spend-guard";
const guard = createAIGuard({
apiKey: process.env.HUGGINGFACE_API_KEY!,
provider: "huggingface",
huggingfaceModel: "deepseek-ai/DeepSeek-R1:fastest",
policy: { cacheRepeatedPrompts: true, autoReroute: true }
});
const response = await guard.chat({
model: "deepseek-ai/DeepSeek-R1:fastest",
messages: [{ role: "user", content: "Classify this ticket" }]
}, "classification");
console.log(response.choices?.[0]?.message?.content);Pricing
Model pricing lives in pricing.rates.json. You can override it with the AI_SPEND_GUARD_PRICING environment variable.
Development scripts
npm run check- type-check the projectnpm run build- build todist/npm run demo- run the local demo
Usage Guide
See the usage guide for integration examples, policy examples, and a middleware sample: USAGE.md
