Wrap any AI API call with automatic retries, spend caps, timeouts, and request receipts — so you never get a silent failure or a surprise bill.
AI API calls fail in frustrating ways: rate limits with no backoff, silent timeouts that hang forever, and costs that spiral when something loops. withGuard() adds a safety layer around any AI call.
429s wait for the Retry-After header, 408s retry immediately, and 5xx errors retry with exponential backoff. Other errors throw immediately.
Reads cost from x-badgr-cost-usd / x-cost-usd response headers; if the next call would exceed maxSpendUsd, it throws before the request is made.
Every call returns a request ID, attempt count, status, cost, and running total spend — printed to the terminal, returned as JSON, or suppressed.
npm install badgr-llm-guardRequires Node.js 18+.
Wrap your existing provider call with withGuard() and pass retry, timeout, and spend-cap options.
import { withGuard } from "badgr-llm-guard";
const guardedCall = withGuard({ maxRetries: 3, timeoutMs: 30_000, maxSpendUsd: 5 });
const result = await guardedCall(() => openai.chat.completions.create(request));badgr-llm-guard receipt
request-id req_7f3a9b2c
attempts 2 (1 retry after 429)
status 200
cost $0.004
total spent $0.008badgr-llm-guard checkPrint which provider API keys are configured in the environment.badgr-llm-guard demoShow a withGuard usage example.npm install badgr-llm-guard — no account needed to get started.