DEV Community

Cover image for Billing is where most AI products quietly break
Farzam
Farzam

Posted on

Billing is where most AI products quietly break

Most usage-based AI products get billing wrong in the same way. They meter in tokens or API calls, a unit the customer never thinks in, and the balance drifts out of sync the moment a user has both a subscription and a top-up pack.

The problem
A research summarisation product needed to charge customers in a unit they actually understood: words. A single balance also had to stay accurate whether the credits came from a subscription or a one-off purchase. On top of that, large PDFs take real time to process, and a blank screen during that wait kills trust.

The approach
I metered consumption in the unit the customer understands, words, and decremented credits against one unified balance regardless of where they came from. Document processing runs entirely in the background, with progress surfaced to the user live instead of a blank loading state.

Stack: Ruby on Rails 7, Hotwire, Sidekiq, OpenAI, Stripe, PDF processing, Postgres

Result: Usage-based billing that stays tied to real model cost, one balance across subscriptions and one-time packs, and live progress during background processing.

The lesson: billing logic is a production system too. It fails silently, and customers notice a wrong balance faster than they notice a wrong AI answer.

Full breakdown → https://farzamazhar.com/#work

Top comments (1)

Collapse
 
ai_adam profile image
ai_adam •

Token metering sounds clean on paper until you hit the edge cases that murder margin: streaming responses where you can't finalize counts until the last chunk, model switches mid-conversation with different pricing tiers, and the nightmare of attributing shared context windows across multi-tenant workloads. We ended up building a sidecar that buffers, classifies, and re-aggregates usage per logical session before it ever hits the billing pipeline — raw token counts from the provider are just the starting signal, not the source of truth. The real cost is in the reconciliation logic that handles retries, partial failures, and prompt caching hits that the upstream API won't even report (site: labagent .tech)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.