01 · Before and afterThe pattern
async function recordBestEffort(fn: () => Promise<void>, what: string) {
try { await fn(); } catch (e) { log.warn(`failed to record ${what}`, e); }
}
try { const out = await callModel(req); await recordBestEffort(() => usage.add(out), 'usage'); return out; }
catch (err) { await recordBestEffort(() => usage.fail(err), 'failure'); throw err; } // the original error wins02 · Run it on your appThe check
If your usage/billing table is down for five minutes, do AI requests still succeed and still show the real provider error?
If the honest answer is no or not sure, this is worth a look before it reaches production.