Skip to content

AgentMarginProduct

How it works

AgentMargin records model usage and revenue against the same customer, then shows what each customer contributes after tracked AI inference cost.

v0.3.0-alpha · experimentalLocalhost onlySource: README.md

The flow

  1. An AI request completes in your app.
  2. Read the model and token usage, or the provider-reported cost.
  3. Send it through the AgentMargin SDK to the authenticated local API, which stores it in PostgreSQL.
  4. Record that customer's revenue through the same SDK or API.
  5. /v1/analytics feeds the local dashboard: contribution by customer.
Data flow
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 customer

Three cost states

Every usage event carries one of three cost states. Only the first two count towards a customer's margin.

StateWhere it comes fromCounts towards margin
ReportedThe provider-reported cost on the event. A reported $0 is valid.Yes
EstimatedTokens × the workspace price version in effect at the event's timestamp.Yes
UnknownNo 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)RevenueTracked AI costAI contribution
Customer A$29.00$12.00$17.00
Customer B$29.00$42.00−$13.00
Customer C$29.00UnknownNot 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.