_cost tag to an event’s metadata when you ingest it. From there, costs roll up through the Metrics API right next to revenue — giving you margin, not just turnover.
How it works
- You ingest an event through the Event Ingestion API as usual.
- You add a
_costobject inside that event’smetadata. - Macropay aggregates the cost and exposes it through the Metrics API alongside billed revenue.
The _cost tag
Add _cost to any event’s metadata. Here is the minimal shape:
Fields
What to track
_cost is deliberately generic: anything that costs you money per unit of activity is fair game. Three common patterns:
- AI & LLM calls
- Infrastructure
- Third-party services
The headline use case. Record what each model call cost, attributed to the customer who triggered it. Stash token counts in metadata too, so you can later slice cost by model and prompt size.
TypeScript
Routing model traffic through the Macropay AI proxy (
POST /ai/v1/chat/completions) captures token cost for you automatically — no manual rate math, no _cost plumbing. Use manual _cost tagging when you call providers directly or need to fold in costs the proxy can’t see.Why margin matters for agents
If you bill autonomous agents, cost events are half of a profitability equation. The other half is what the agent earned: with the Signals API you certify the value an agent delivered, and Macropay computes agentic margin — billed revenue versus the AI cost (COGS) behind it, per agent. Tag every model call, tool invocation, and downstream service with_cost, and that COGS figure becomes real instead of a guess. An agent that bills well but burns more in tokens than it earns shows up immediately, before it quietly erodes your margin.
Practical tips
Ingest cost as it happens
Ingest cost as it happens
Record the cost on the same code path that incurs it — right after the API call returns. Real-time ingestion keeps your margin metrics live and avoids batch reconciliation drift.
TypeScript
Don't round away sub-cent costs
Don't round away sub-cent costs
The 12-decimal precision exists so a single token or a single API hit never rounds to zero. At scale, those fractions are your real cost.
0.000125 cents is $0.00000125 — preserved exactly.Carry context next to the cost
Carry context next to the cost
A bare number tells you how much; surrounding metadata tells you why. Tag the model, feature, or workload so you can group spend by driver later.