Budget Guards First
"STOP before exceeding $X. Not alert after."
The Problem
Your agent calls the model in a loop. 10 iterations × $0.38/call = $3.80 for a single command.
No budget alert. No circuit breaker. Credits run out. You're surprised.
AWS has billing alerts. Your agent has nothing.
The Guard Decision Flow
Agent (loop iteration)
↓ check(estimatedCost)
Sayay Guard compares usage vs budget
↓ <80%
ALLOW
proceed normally
proceed normally
↓ 80-95%
WARN
continue + log
continue + log
↓ 95-100%
DEGRADE
use cheaper model
use cheaper model
↓ >100%
BLOCK
stop execution
stop execution
Try It: Threshold Simulator
Drag the slider to see what action triggers at each usage level:
ALLOW 72% of daily budget used
DIY: 60 Lines
The core interface is dead simple:
minimal-guard.ts
interface Guard {
check(userId: string, estimatedCost: number): Action;
record(userId: string, actualCost: number): void;
getUsage(userId: string): Usage;
}
type Action = 'allow' | 'warn' | 'degrade' | 'block';
interface Budget {
dailyUsd: number; // e.g. 5.00
monthlyUsd?: number; // e.g. 100.00
}
interface Thresholds {
warn: number; // 0.80 (80%)
degrade: number; // 0.95 (95%)
}
// Storage: Map in memory, KV, Redis, or Firestore
// Reset: daily at midnight UTC
// Degrade action: switch to cheaper model (Haiku instead of Sonnet)Or use Sayay (308 lines, zero deps, production-ready):
install
npm i @carloscortezcloud/sayay-guardusage
import { SayayGuard, MemoryStorage } from 'sayay';
const guard = new SayayGuard({
storage: new MemoryStorage(),
budget: { dailyUsd: 5.0, monthlyUsd: 100.0 },
thresholds: { warn: 0.8, degrade: 0.95 }
});
// Before each LLM call:
const action = guard.check(userId, estimatedCost);
if (action === 'block') throw new Error('Budget exceeded');
if (action === 'degrade') model = 'haiku'; // cheaper
// After each LLM call:
guard.record(userId, actualCost);Key Insight
"Degrade" is the killer feature. Not block. Not warn.
When budget hits 95%, automatically switch to a cheaper model. The user barely notices (Haiku is 90% as good as Sonnet for most tasks). But you save 12x on tokens.
It's the equivalent of spot instances for compute — same work, fraction of the cost.