Margin
Margin is what a customer’s usage earned you, minus what it cost you, as a share of what it earned. Hardcount works it out per customer and per model, from your prices and the costs you pay your AI provider. Only you see it, never the customer.
Costs: what each event cost you
Section titled “Costs: what each event cost you”A provider cost is what an AI provider charges you per token, for one
provider, model and kind of token. Save one with PUT /:
{"provider": "anthropic", "model": "claude-sonnet-4.5", "token_type": "input", "unit_cost_cents": "0.00012"}unit_ is in US cents per token: 0.00012 cents is $1.20 per
million tokens. Provider costs are always in USD.
Hardcount records the cost on each usage event when it arrives: its quantity times the matching cost. A cost you save or change later applies only to events that arrive after it. So save your costs before the usage arrives.
- An event gets a cost only when it carries
provider,token_, and eithertype modelormodel_(the model name exactly as LiteLLM sent it). Other events, such as usage of a meter that is not for tokens, get no cost, andraw unresolved_does not count them. Their price still counts inevents revenue_(What margin leaves out).cents - When a LiteLLM record has a cost breakdown, Hardcount can take LiteLLM’s own cost for each kind of token instead of yours (when).
The margin report
Section titled “The margin report”GET / answers for one customer and a
time window. Left out, to is the current time and from is 30 days
before that time. A window can be at most one year.
| Field | What it is |
|---|---|
revenue_ |
What the window’s usage earns at the customer’s current prices. |
cost_ |
The recorded cost of the window’s events, as a decimal string in US cents. |
margin_ |
(revenue − cost) ÷ revenue, in hundredths of a percent: 5,000 is 50%. |
by_ |
The same three figures for each model. |
unattributed_ |
Revenue from prices that do not name a model, so it fits no model’s row. |
unresolved_ |
Token events with no cost found. |
The Margin card on the customer’s page in the dashboard shows the same report, starting on the current billing period.
Worked example: the quick start’s demo
Section titled “Worked example: the quick start’s demo”The quick start demo bills one customer for a month of Claude tokens. Its saved costs and the month’s usage:
| Model and tokens | Quantity | Cost per million | Cost |
|---|---|---|---|
| Sonnet 4.5 input | 400,000 | $1.20 | $0.48 |
| Sonnet 4.5 output | 175,000 | $4.50 | $0.7875 |
| Sonnet 4.5 cache read | 200,000 | $0.05 | $0.01 |
| Haiku 4.5 input | 600,000 | $0.80 | $0.48 |
| Haiku 4.5 output | 250,000 | $4.00 | $1.00 |
The invoice billed $3.88 for Sonnet ($1.20 + $2.62 + $0.06) and $1.85 for Haiku ($0.60 + $1.25). So:
| Revenue | Cost | Margin | |
|---|---|---|---|
| Sonnet 4.5 | $3.88 | $0.48 + $0.7875 + $0.01 = $1.2775 | (3.88 − 1.2775) ÷ 3.88 = 67.1% |
| Haiku 4.5 | $1.85 | $0.48 + $1.00 = $1.48 | (1.85 − 1.48) ÷ 1.85 = 20.0% |
| Total | $5.73 | $2.7575 | (5.73 − 2.7575) ÷ 5.73 = 51.9% |
The report gives the total as revenue_ 573, cost_ “275.75”
and margin_ 5188, since 297.25 ÷ 573 = 0.51876. The demo prints it
rounded: billed $5.73, cost $2.76, margin 51.9%.
When margin_bps is left out
Section titled “When margin_bps is left out”The report leaves margin_ out, rather than show a wrong figure, when:
- the revenue is not in USD. Costs are in USD, so a plan priced in EUR or GBP shows no margin. Revenue in two currencies shows none either.
- an event in the window has no cost.
unresolved_counts them. Save the missing cost, for example for a provider such asevents bedrock. - no event in the window has a cost. Save your costs first.
- the revenue is 0.
A row in by_ also leaves out its margin when it has revenue but no
recorded cost.
What margin leaves out
Section titled “What margin leaves out”- Base fees, credit and tax. Margin covers usage only.
- What was billed. Revenue is worked out again at each subscription’s current prices, over active subscriptions. After a price change, or for a canceled subscription, it differs from the invoices.
- Events with no cost labels. Their price still counts in
revenue_, socents margin_reads higher than your real margin.bps - Claude Haiku 5.5’s two prices. Haiku 5.5 has a higher price when the
prompt is over 100,000 tokens, and its events carry a
context_label that picks the price. A provider cost has notier context_label, so one cost covers both of the model’s prices. Your margin is wrong for one of them (see your margin).tier
More: every call, step by step: See your margin.
Next: Bill for AI tokens by model.
Stuck, or found a bug? Ask in Discussions · Open an issue