An engineering guide to building AI billing infrastructure in-house

Every AI agent run fans out into model calls and tool calls, and each one is a billable event. Lose one, and you've paid a provider for usage you never charge. Count one twice, and your customer pays for something they didn't use.

Most teams start by tracking usage inside the app. It works until the first crash mid-request, the first retry, or the first deploy in the middle of a busy hour. Then the numbers quietly drift.

I wrote an engineering guide for teams building AI billing in-house. It covers:

  • What product and revenue teams will ask for: wallets, prepaid credits, and margin per run and per customer

  • A data model built from five core entities

  • Why billing inside the app breaks, and the dedicated service to build instead

  • A reference architecture on AWS with Kinesis, ClickHouse, Redis and Postgres

  • The patterns that keep every number correct: idempotency at every hop, atomic wallet debits, and daily reconciliation

Detailed guide:

If you've built this yourself, I'd like to hear which part gave you the most trouble.

14 views

Add a comment

Replies

Be the first to reply

Have a question or a thought to share? Add a comment above to start the conversation.