Cost engine
Versioned model prices with input, cached and output token rates, computed in integer microdollars.
Illustrative data · Customers A–C are fictional
AgentMargin attributes AI inference cost and revenue to the same customer, so you can see who's profitable, who isn't, and where the cost is still unknown.

01 · The problem
Most model dashboards tell you how many tokens you spent. They don't answer the question that decides your pricing: which customer is profitable?
AgentMargin brings usage and revenue records together for the same customer, then separates the cost it knows from the cost it doesn't.
| Customer | Revenue | AI cost | Contribution |
|---|---|---|---|
| Customer A | $29.00 | $12.00 | $17.00 |
| Customer B | $29.00 | $42.00 | −$13.00 |
contribution = revenue − tracked AI cost
02 · How it works
Your app finishes an OpenAI call as usual. AgentMargin is a post-call helper, not a proxy or automatic capture.
One call maps the completed result to a usage event: model, token counts, response ID. Prompts and outputs never leave your app.
The authenticated API on 127.0.0.1 stores workspace-scoped, idempotent records. Retrying the same response is a no-op.
Record revenue for the same customer and /v1/analytics returns contribution, or holds it back when cost is unknown.
03 · Architecture
Everything runs on your machine. Usage and revenue meet in one local workspace.
Completes the AI request and resolves a trusted, server-side customer ID.
Source-first TypeScript client. Maps usage, records revenue, sends metadata only.
Authenticated /v1/events, /v1/revenue, /v1/analytics and pricing endpoints.
Workspace-scoped, idempotent records in integer USD microdollars.
Local React dashboard: customer economics, coverage, unknown-cost diagnostics.
04 · What works today
Versioned model prices with input, cached and output token rates, computed in integer microdollars.
Reported, estimated and unknown cost, kept apart. Incomplete data means no final margin.
Usage and revenue land on the same customer. Workspace-scoped and idempotent.
05 · Plugs into your stack
Map a completed Responses or Chat Completions result to an event in one call.
Append-only price versions with UTC dates and a full audit trail.
Compact dark customer economics, cost comparison, coverage and a pricing editor.
Authenticated endpoints for events, revenue, analytics and workspace model pricing, bound to 127.0.0.1.
When AI cost is unknown
If a model price is unknown, AgentMargin says so and holds back the final margin instead of showing you a comforting zero.
06 · Developers
Source-first client for events, revenue and analytics. Not published to npm yet.
Model, token counts and response ID. No prompts, no generated text.
Resolve the customer from server-side auth, never from browser input.
A route-handler sketch that tracks usage after the OpenAI call returns.
import OpenAI from "openai";
// source-first SDK: link the local workspace package (not on npm yet)
import { AgentMarginClient } from "@makinohq/agentmargin";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
const margin = new AgentMarginClient({
endpoint: process.env.AGENTMARGIN_ENDPOINT ?? "http://127.0.0.1:8787",
apiKey: process.env.AGENTMARGIN_API_KEY!,
});
const response = await openai.responses.create({ model, input, store: false });
// usage metadata only: no prompt, no generated text
await margin.trackOpenAIResponse(response, trustedCustomerId);margin −44.83% · 0 unknown-cost events
07 · Accuracy & security
08 · Quickstart
Docker or OrbStack plus Node 22.18+. The demo seeds three fictional customers. No OpenAI account or real customer data required.
Stop: npm run demo:stop · Reset (deletes demo data): npm run demo:reset
npm run demoDocker or OrbStack · Node 22.18+ · no OpenAI account needed
Open source · MIT · v0.3.0-alpha
Practical feedback matters more than stars. Tell us what data you have and what you wish you knew.