Open SourceApache-2.0 License

badgr-llm-guard

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.

How it works

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.

1

Retry with Retry-After

429s wait for the Retry-After header, 408s retry immediately, and 5xx errors retry with exponential backoff. Other errors throw immediately.

2

Enforce spend cap before the call

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.

3

Produce a receipt

Every call returns a request ID, attempt count, status, cost, and running total spend — printed to the terminal, returned as JSON, or suppressed.

1

Install

Terminal
npm install badgr-llm-guard

Requires Node.js 18+.

2

Usage

Wrap your existing provider call with withGuard() and pass retry, timeout, and spend-cap options.

index.ts
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));

Example output

Receipt (terminal)
badgr-llm-guard receipt
  request-id  req_7f3a9b2c
  attempts    2 (1 retry after 429)
  status      200
  cost        $0.004
  total spent $0.008

Commands

badgr-llm-guard checkPrint which provider API keys are configured in the environment.
badgr-llm-guard demoShow a withGuard usage example.

What's included

Automatic retries for 429 (Retry-After), 408, and 5xx with exponential backoff
Hard spend cap enforced before each request, read from response headers
Per-call receipts: request ID, attempts, status, cost, running total
Works with any AI provider SDK — wraps any async function
Low-level createGuardedClient() HTTP client for providers without an SDK
Optional AI Badgr gateway fallback with an OpenAI-compatible base URL
receipt option: terminal, json, or none
Free, no signup required
View on GitHub →

Ready to try badgr-llm-guard?

npm install badgr-llm-guard — no account needed to get started.