AgentMarginBlog

Know your margin, not just your tokens

Model dashboards tell you how many tokens you spent. They can't tell you which customer is profitable. That takes usage and revenue on the same customer.

Concepts2 min read

Most model dashboards answer one question well: how many tokens did we spend? For an AI product with paying customers, the more useful question is which customer is profitable? A total token bill can't answer that, because it never meets your revenue.

Put cost and revenue on the same customer

AgentMargin records model usage and revenue against the same customer ID, then subtracts one from the other. Two fictional customers paying the same plan can look like this:

Customer (fictional)RevenueTracked AI costAI contribution
Customer A$29.00$12.00$17.00
Customer B$29.00$42.00−$13.00

Same price, opposite outcomes. Customer B's AI usage outweighs what they pay, and a token total would hide that inside an average.

What AI contribution means

AI contribution is revenue minus tracked AI inference cost. It isn't gross profit or net profit: hosting, payment fees, support and other spend aren't included. The narrow definition is on purpose, so the number means exactly one thing.

How the data comes together

  1. After an AI request completes, read the model and token usage, or the provider-reported cost.
  2. Send it through the TypeScript SDK to your local AgentMargin API, which stores it in PostgreSQL.
  3. Record the customer's revenue through the same SDK or API.
  4. The analytics endpoint and local dashboard show contribution by customer.

Money is stored as integer USD microdollars (1 USD = 1,000,000 micros), so there's no floating-point drift in the totals.

When a cost is unknown

Some events arrive without a reported cost and without a matching price. AgentMargin marks those unknown and withholds that customer's final margin rather than guessing. There's a separate post on why.

Try it with fictional customers

The one-command demo seeds three fictional customers: one profitable, one loss-making and one with unknown cost. It needs Node.js 22.18+ and OrbStack or Docker, and no OpenAI account.

bash
git clone https://github.com/rzkarsyad/agentmargin.git
cd agentmargin
npm ci
npm run demo
All posts