Case study
LLM Usage & Cost Engine
Tracks every LLM call by user, project, and model — with receipts.
sanitized project coverSummary
Designed a usage and cost engine that meters every LLM call, applies pricing, and auto-generates receipts for finance. Built to keep AI-product economics defensible.
Problem
AI products fall over financially when the team can't explain a single line item on the bill. Token counts, model upgrades, retries, and silent provider pricing changes all add up — and finance ends up with a number they can't tie back to a user or a feature. Without per-call attribution, optimization is guesswork and pricing is a leap of faith.
Approach
I designed the engine around one rule: every LLM call gets metered before the response is returned. Each event captures model, prompt and completion tokens, tenant, user, and the originating project or feature, then runs through a pricing schema that maps providers and models to current rates. Receipts are generated on the fly, not in a nightly batch — so finance sees the same number the product team sees, in real time. The pricing schema is structured so A/B-testing model substitution is a config change: swap a feature from one model to another, and the engine attributes cost cleanly across the experiment. The data layer lives in Postgres with indices tuned for tenant and time-range queries, because the most common read patterns are "what did tenant X spend last month" and "which feature is burning the most tokens." The engine plugs in front of any LLM relay; the contract is one usage event per call.
Architecture
Result
The cost engine runs in production with full per-call attribution and live receipts. Product, finance, and engineering see the same numbers, which means model decisions get made on evidence instead of vibes. Shipped, indexed, and powering real billing flows.
Highlights
- Per-call accounting with model, token, and tenant attribution
- Receipts generated on the fly, not in nightly batches
- Pricing schema that supports A/B-tested model substitution
Have something similar?