Skip to content
Sonic AI

Idempotency in usage billing is a product feature

Idempotency keys stop usage and seat imports from billing twice when a file is re-uploaded or a sync retries. How to choose the key and where it applies.

Lokesh Mahala

Lokesh Mahala, CEO & Co-founder

· Updated · 3 min read

Two identical blue squares on merging lines, with only one blue square landing in the tray and an empty outline where the duplicate was dropped.
On this page

Idempotency in usage billing means that importing the same usage event twice still produces one billable row. Each event carries an idempotency key built from the columns that identify it, such as a transaction id, so a re-uploaded file, a retried webhook or an overlapping sync is recognised and skipped instead of billed again.

That makes it a product feature that finance owns, because it's the difference between "run the pipeline again" and "explain to the customer why they were billed twice". In high-volume usage-based billing, files arrive late, partial and out of order. Operators reprocess. Syncs run hourly. Webhooks retry. Without idempotency keys you're trusting luck.

What an idempotency key does in billing

An idempotency key identifies one logical event across retries:

  • Same key and same payload: no duplicate billable row.
  • Same key and changed payload: a conflict you must resolve deliberately.
  • New key: a new event, even if the row "looks similar".

You pick the columns (or formula) that uniquely identify a row in your export shape, and Sonic uses exactly those.

Idempotency keys in usage templates

A usage template defines how a file becomes billable events:

  • Which columns map to which meter
  • Transforms (unit conversion, rounding)
  • Customer resolution, by the customer ID you show customers or a fuzzy name match, never by legacy object ids
  • Idempotency key built from stable columns

The Process step on usage works like the one on seats: attaching a sample doesn't silently stage billable data. You run Process yourself when the sample and worksheet are ready. After a successful run, mapping tweaks reprocess through their own controls, without re-arming the whole step unless the sample file changes. The usage templates guide walks through the Process step in detail.

Usage sync from your database follows the same template rules: a marker column with an index, incremental reads, and no full-table replay each hour.

How seat snapshot imports avoid duplicates

Seat uploads key each imported daily row by a seat snapshot key built from the columns you configure. It never defaults to "all columns".

Already-imported seat snapshot rows skip insert while the running balance still advances. That lets you re-upload a month-end file without duplicating events for days 1–28 while still picking up days 29–31. Why seats are snapshots explains the balance logic.

Webhook usage takes the same path as a JSON upload. A record whose customer can't be identified fails before Process, and the records that go through each carry their own result.

What happens to the key when the sample file changes

When you switch samples or worksheets:

  • Previous manual picks survive only if still valid on the new sheet.
  • Columns that existed only on the old sheet drop off, so the key always matches the sheet you're processing.
  • Sheet columns are shown as options; nothing auto-checks without operator intent.

That prevents silent key changes that turn a safe re-run into a flood of duplicates.

How invoices avoid duplicate lines

Invoices avoid duplicate lines through product-line identity on open drafts. Ingestion idempotency works upstream; downstream, regenerating a draft after late events replaces the line for that product and period instead of stacking a second one.

Issued invoices don't accept new lines at all, which is a separate layer of safety. Why issued invoices never grow new lines covers what happens instead.

Watch out for: keys that change between runs

  • Keys must be stable, not clever. Transaction id beats "customer + date" if the vendor sends corrections with the same date.
  • Test on a small file before an hourly database sync. A bad mapping run 24 times a day is a long weekend.
  • Changing a live template changes future reads only. History does not rewrite.
  • Seat delta columns are not keys. They are not even import columns.
  • Webhook JSON must match template field paths. Excel formulas in a JSON template are a smell.
Lokesh Mahala

Written by

Lokesh Mahala

CEO & Co-founder, Sonic AI · LinkedIn

Lokesh is the Cofounder & CEO of Sonic AI. Before starting Sonic, Lokesh was an engineer at Stripe for 4 years building Stripe's Revenue and Finance Automation suite. He was a founding engineer at Recko (acquired by Stripe in 2021) where he built reconciliation solutions for B2C companies. He completed his bachelors from IIT Roorkee.

FAQ

What makes a good idempotency key for usage data?

A good idempotency key uses columns that never change for the same event, such as the vendor's transaction id. Combinations like customer plus date break when a vendor sends a correction with the same date, because the corrected row looks like a repeat. Pick the smallest set of columns that makes a row unique in your export, and test it on a small file first.

What happens if the same usage file is uploaded twice?

Rows whose idempotency key was already imported are skipped, so the second upload adds nothing billable. Rows with new keys, such as days added to the end of the file, come in as new events. A key that repeats with different values is a conflict to resolve on purpose, because it usually means the source data was corrected.

Is idempotency the same as deduplication?

Close, but idempotency is stricter. Deduplication looks for rows that look alike, which can drop two real events that happen to share values. An idempotency key states exactly which columns make an event unique, so two similar rows with different keys both count, and a retried row with the same key counts once.

Can idempotency fix a duplicate that was already billed?

No. Idempotency stops a duplicate at import, before it reaches an invoice. A duplicate that has already gone out on a sent invoice is corrected with a credit note, because sent invoices are never rewritten. That's why the key needs to be right before a file or sync starts feeding live invoices.

See Sonic run on one of your contracts.

A 30-minute call with the team, using your own contract.

Book a demo