Gateways
A gateway resells other vendors’ models under ids like anthropic/claude-sonnet-4.5. What it actually charged depends on routing, its own fee and your BYOK terms, none of which are visible in a usage event. So MarginFuse does not guess: it uses the number the gateway itself reports.
OpenRouter
Every OpenRouter response carries a usage object with the provider-final cost. Forward it and the figure is observed rather than estimated.
import { MarginFuse, fromOpenRouter } from "marginfuse";
const mf = new MarginFuse({ apiKey: process.env.MARGINFUSE_KEY! });
const r = await openai.chat.completions.create({
model: "anthropic/claude-sonnet-4.5",
messages,
});
mf.track({
customerId: "cus_8x2m91",
feature: "ai_chat",
provider: "openrouter",
model: "anthropic/claude-sonnet-4.5",
// Forwards the gateway's own cost, and counts cached tokens once.
...fromOpenRouter(r.usage),
});prompt_tokensis a total. Cached reads and cache writes are already inside it. MarginFuse prices those as three separate charges and adds them up, so passing the total straight through charges every cached token twice, at the full uncached rate. On a heavily cached workload that can double the reported cost of a customer who is actually cheap.costis a floating point number. The small values cheap models produce stringify to exponent notation, and"1.2e-7"is rejected by the API as a decimal string. The helper formats fixed point to nano precision and rounds anything below a nano to zero rather than inventing precision it does not have.
These are not opinions about style. They are the two cases the shared conformance vectors exist to pin down, so every MarginFuse SDK in every language gets them identically right rather than each porting the arithmetic by eye.
When a gateway event has no cost
The helper omits costUsd entirely rather than sending zero, because zero is a claim that the call was free. MarginFuse then resolves the model id to the upstream vendor and prices it at that vendor’s list rate, labeling the result EST: the gateway’s own margin is not in that number, so it is a floor rather than a fact.
Ids that cannot be resolved honestly resolve to nothing at all. A :free variant or openrouter/auto gets no price rather than a confident wrong one, because overstating a free call’s cost understates your margin, which is worse than an admitted unknown.