Overview
Most "AI apps" quietly get billing wrong. The brief: integrate a slow, unreliable, per-token image API and charge users the provider's cost plus 25%, where part of the cost is only known after the image finishes. Users see a min / expected / max price before they click generate. The worst case is held from their balance, then the exact cost is charged and the rest released. Money is stored as integer picodollars (1 USD = 10^12), so nothing is ever rounded and every charge matches the provider's cost plus 25% to the last digit. Every balance change is a row in a credit ledger (hold, release, charge, purchase) with a unique index that makes double-charging impossible, even under races. The generation lifecycle is built for what real AI providers do: idempotency keys so retries never create a second billed job, Retry-After-aware backoff for rate limits and overloads, signed (HMAC-SHA256) webhooks verified on the raw body and deduplicated by event ID, and a status that only moves forward so late or duplicate events can't rewind it. A scheduled sync polls generations whose webhooks went missing. Failed images cost $0. An admin report shows credit spend, provider cost and profit per UTC day and per user, reconciled against the provider's own usage API. 61 tests cover the pricing maths, webhook dedup/ordering and billing. Built with Claude Code as a pair — every line read, every failure path tested, every number checked.
Engineering
Challenges
Solutions
Tech Stack
Project Info
Links