Invental/Review lessons/Writes that span systems

Writes that span systems

Usage metering must never fail the request

Metering and audit writes are side effects of a request, not part of its result: if recording usage can throw, a successful (and already paid-for) model call can reach the user as a failure, or a DB blip can hide the real error.

01 · Before and afterThe pattern

TypeScript
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 wins

02 · 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.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.