Ship per-token billing for your AI product this afternoon
Copy-paste, production-shaped Python code plus a button-by-button Stripe walkthrough. Idempotency, retries, decimal money math, usage caps, abuse protection — the tricky parts are already handled.
One-time payment · No subscription · Instant download
Billing is the part nobody wants to rebuild at 2 a.m.
Token usage metering looks like a weekend project until you hit the edge cases. The kit covers them before you do.
Usage is never lost
MeterEvents report with UUID idempotency keys, exponential-backoff retries, a circuit breaker, and a local fallback queue when Stripe is unreachable. Webhook signatures verified and events de-duplicated.
Pricing that never loses money
validate_plan_economics() checks your plan price against your real backend cost — including Stripe and platform fees — and tells you SAFE or money-losing, plus the max tokens you can safely include.
Abuse protection built in
One customer on your priciest model can wipe a day of margin. Weighted quota units, hard USD caps, per-customer rate limits, daily sub-caps, and auto-throttling on 5× usage spikes — every block audit logged.
14 files, ready to drop into your project
The core engine has zero dependencies and runs on any Python 3.8+ setup. The Stripe SDK is only needed when you go live.
Code 3 files
- billing_module.py — full engine: separate input/output pricing, cache-read discounts & cache-write surcharges, tiered pricing (volume & graduated), minimum charge, 80/90/100% alerts, failed calls bill zero, batch billing, multi-tenant engine, multi-currency display, JSON-lines audit log. Money math in Decimal; Stripe's 12-decimal unit amounts handled.
- stripe_integration.py — MeterEvents reporting with retries, circuit breaker, fallback queue; webhook verification & de-dup; metered subscription helpers.
- quickstart.py — one runnable file demoing every feature and printing real billing results.
Docs guides
- 10-minute Quickstart — install, run, see money calculated
- Stripe Metered Billing setup guide — exact clicks, fields & example values, test mode → live mode
- Webhook integration guide — Flask/FastAPI/Django examples, retry semantics, edge cases
- 10 billing anti-patterns + audit playbook — how token billing goes wrong, and how the kit prevents each one
- 2026 model pricing cheat sheet — verified prices for GPT-5.6, Claude, DeepSeek V4 (peak/off-peak), Gemini, with sources
- Hybrid pricing guide — fixed fee + quota + overage math that never loses money
- Anti-abuse guide — weighted quota, caps, anomaly detection, chargeback prevention, copy-paste Fair Use Policy
Legal & extras included
- LICENSE.md — Basic vs Extended terms
- DISCLAIMER.md — AS IS warranty & trademark notice
- 9 validated plan presets — budget, smart, premium model routes
- requirements.txt — stripe only, for the live integration
python3 code/quickstart.py