LLM Usage Tracking
Meridian automatically meters every token routed through the gateway, but the real leverage comes from tagging calls with your own dimensions. This recipe shows how to attach metadata, query rolled-up usage, and wire alerts before a customer cost spike becomes a customer cost incident.
1. Tag every call with metadata
Pass a metadata object on each completion. Meridian indexes the keys you supply, so you can slice usage by user, feature, tenant, or environment without re-instrumenting later.
2. Query rolled-up usage
The usage API returns token counts and USD cost grouped by any tag you indexed. Cost is computed at gateway egress with the same 20% markup you see on the invoice, so dashboard numbers match billing to the cent.
3. Set budget alerts per dimension
Create alert rules in the Meridian admin console scoped to any metadata key. Alerts are advisory by default and never throttle traffic, which keeps a noisy tenant from taking down the rest of your fleet.
import { Meridian } from '@meridian/sdk';
const client = new Meridian({
apiKey: process.env.MERIDIAN_API_KEY,
});
// Track every LLM call with metadata
const response = await client.chat.completions.create({
model: 'azure/model-router',
messages: [{ role: 'user', content: 'Hello' }],
metadata: {
user_id: 'usr_42',
feature: 'support_chat',
environment: 'production',
},
});
// Query usage by tag
const usage = await client.usage.query({
start: '2026-06-01',
end: '2026-06-27',
groupBy: ['user_id', 'model'],
});
console.log(usage.totalTokens, usage.totalCostUsd);