Production guard for AI workloads — hard spend caps, intelligent retries, automatic provider failover, and health tracking, for deployed services.
ai-prod-guard wraps your existing provider calls with a single createRuntimeGuard() instance that enforces limits before each request goes out — for AI businesses that need runtime protection across multiple services.
Per-request and per-session spend limits are enforced before the network call is made, so a runaway retry loop can't spiral into a surprise bill.
429, 408, and 5xx errors are retried automatically with Retry-After and exponential backoff, up to maxRetries.
On exhausted retries or a non-retryable error (401, 403), the guard switches to the next fallback provider and marks the failed one unhealthy until it auto-recovers.
npm install ai-prod-guardRequires Node.js 18+. TypeScript library, no CLI.
Configure a primary and fallback providers, then call guard.call() instead of calling the provider SDK directly.
import { createRuntimeGuard } from "ai-prod-guard";
const guard = createRuntimeGuard({
maxCostPerRequest: 1.0,
maxCostPerSession: 50.0,
maxRetries: 3,
primaryProvider: {
name: "openai",
call: (req, signal) => openai.chat.completions.create(req, { signal }),
estimateCost: () => 0.03,
},
fallbackProviders: [
{
name: "anthropic",
call: (req, signal) => anthropic.messages.create({ ...req, signal }),
estimateCost: () => 0.02,
},
],
});
const response = await guard.call((provider, signal) =>
provider.call(
{ model: "gpt-4o", messages: [{ role: "user", content: "Generate report" }] },
signal
)
);const { confirmedUsd, potentialUsd, totalExposureUsd } = guard.getSpendSummary();
// confirmedUsd: successfully completed calls (definitely billed)
// potentialUsd: timed out or disconnected calls (may have billed)
// totalExposureUsd: confirmed + potential + in-flight
console.log(`Confirmed: $${confirmedUsd}, Potential: $${potentialUsd}`);npm install ai-prod-guard — no account needed to get started.