Token Metering for Billing
Meridian streams prompt and completion token counts back on every chat response so you can meter usage, attribute cost per customer, and bill with confidence. This recipe walks through capturing usage payloads, persisting them, and rolling them up into invoice line items.
1.Capture usage from each response
Every Meridian completion returns a usage object containing prompt, completion, and total token counts. Tag each call with a customer id so you can attribute spend later.
const res = await fetch("https://llm.getnimbus.net/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.MERIDIAN_KEY}`,
"Content-Type": "application/json",
"X-Customer-Id": customerId,
},
body: JSON.stringify({
model: "azure/model-router",
messages: [{ role: "user", content: prompt }],
}),
});
const data = await res.json();
const { prompt_tokens, completion_tokens, total_tokens } = data.usage;2.Persist with a stable schema
Write one row per request to your usage table. Keep the model id, token splits, and a millisecond timestamp so you can replay any billing period without recomputing from logs. Index oncustomer_idand a truncated day column for cheap rollups.
- customer_id, model, prompt_tokens, completion_tokens, ts_ms
- Use append-only writes; never mutate historical rows
- Snapshot pricing per model at write time for audit safety
3.Roll up into invoice line items
At the close of each billing period, aggregate by customer and model, multiply by your per-1k-token rate, and apply your markup. Meridian charges Meridian rates to you; you charge your retail rate to the customer. The delta is your margin and shows up cleanly on the invoice as a single billed quantity per model tier.