The flow
- An AI request completes in your app.
- Read the model and token usage, or the provider-reported cost.
- Send it through the AgentMargin SDK to the authenticated local API, which stores it in PostgreSQL.
- Record that customer's revenue through the same SDK or API.
/v1/analyticsfeeds the local dashboard: contribution by customer.
AI request completes
↓
Read model + token usage (or provider-reported cost)
↓
AgentMargin SDK → authenticated local API → PostgreSQL
↑
Record customer revenue through SDK/API
↓
/v1/analytics → local dashboard → contribution by customerThree cost states
Every usage event carries one of three cost states. Only the first two count towards a customer's margin.
| State | Where it comes from | Counts towards margin |
|---|---|---|
| Reported | The provider-reported cost on the event. A reported $0 is valid. | Yes |
| Estimated | Tokens × the workspace price version in effect at the event's timestamp. | Yes |
| Unknown | No reported cost and no usable price version at ingestion time. | No. The customer's final contribution and margin are suppressed. |
A missing price is not a $0 expense. See Unknown costs.
The math
- Revenue and costs are stored in USD microdollars (
1 USD = 1,000,000 micros) and calculated with integers. - Prices are versioned, with separate input, output and cached-input token rates.
- Cached input tokens are a subset of input tokens.
- AI contribution = revenue − tracked AI inference cost. It isn't gross profit or net profit.
An illustrative example
| Customer (fictional) | Revenue | Tracked AI cost | AI contribution |
|---|---|---|---|
| Customer A | $29.00 | $12.00 | $17.00 |
| Customer B | $29.00 | $42.00 | −$13.00 |
| Customer C | $29.00 | Unknown | Not shown |
Illustrative numbers, not usage data from the repository.
Where it runs
Everything runs on your machine. The API binds to 127.0.0.1 by default, data lives in your PostgreSQL, and the dashboard is served at 127.0.0.1:8787/dashboard.