← Back

Home · Guides · Idempotent Ingest

Why does idempotency matter when you record AI cost events?

By the Ingenious AI team · Published 2026-11-14 · Reviewed 2026-11-14

Short answer: Networks retry. Workers resend. Webhooks redeliver. If your cost-ingest endpoint treats a redelivered event as a new event, the client's spend — and any bill built on it — inflates by exactly the failures of your transport layer. Idempotency keys fix this: each event carries a unique key, the receiver remembers keys it has seen, and a duplicate delivery is acknowledged but never counted twice. OutcomeMeter's ingest API works this way, so a duplicate delivery never charges twice.

Try the free demo measurement See pricing Start free — one provider key

What is an idempotency key?

An idempotency key is a unique identifier attached to an event or request so that delivering the same event more than once has the same effect as delivering it once. In OutcomeMeter, cost events and outcome records carry idempotency keys: the first delivery writes the record, subsequent deliveries with the same key are recognized as duplicates and write nothing new.

The duplicate is not hypothetical — it is the default failure mode

Distributed systems redeliver as a matter of design. An HTTP client that times out cannot know whether the server processed its request, so it retries — correctly. A queue redelivers a message whose worker died mid-processing. A webhook provider retries until it sees a success response. Every one of these mechanisms will, in production, deliver some events twice. The only question is whether your ingest counts them twice.

For most event streams a duplicate is a nuisance. For billing events it is a financial error with a victim: the client's attributed spend rises, their budget burns faster, their cost per outcome distorts, and the invoice — if you bill pass-through — overcharges them. Idempotency is therefore not an engineering nicety in a metering system; it is the integrity property the money depends on.

How the key works in practice

The sender generates a unique key per logical event — typically a UUID, or a deterministic hash of the event's identity (client, task, timestamp, sequence) — and includes it with every delivery attempt of that event. The receiver stores keys alongside the records they created. On a new delivery, the receiver checks the key first: unseen, the event is processed and the key stored; seen, the delivery is acknowledged as a duplicate and nothing is written.

OutcomeMeter applies this to both streams that carry money-adjacent facts: cost events through the attribution ingest API, and outcome records through the outcome API. The rule is stated plainly in the product's terms of behaviour: a duplicate delivery never charges twice. Invalid events, by contrast, are rejected outright and write nothing — duplicates are honoured as already-counted, garbage is refused entirely.

Why 'just dedupe later' fails

A tempting shortcut is to ingest everything and deduplicate during reporting. It fails three ways. First, identity: without a key recorded at ingest, later dedupe must guess which rows are duplicates, and two genuinely identical legitimate events — same client, same task, same cost, a minute apart — are indistinguishable from one redelivery. Second, timing: budgets and alerts act during the month, long before reporting runs, so duplicated spend triggers real alerts and real client conversations. Third, trust: a dispute pack that contains raw duplicates invites the client to wonder what else was not controlled.

Keys at the door solve all three: identity is explicit, every downstream computation inherits the cleanliness, and the record itself demonstrates the discipline.

Choosing keys that actually stay unique

The key must be unique per logical event and stable across retries of the same event — the two failure modes are generating a fresh key per attempt (defeats the purpose entirely, and is a real bug seen in the wild) and reusing keys across different events (suppresses legitimate data, which is worse than duplicating it, because missing spend understates a client's costs invisibly).

Deterministic keys derived from the event's own identity are robust where the source system has a natural event ID — use it. Where it does not, generate a UUID at the moment the event is created, store it with the event in the sender's outbox, and retry with the stored key. The outbox pattern — record first, deliver second, retry with the same key — is the sender-side half of the contract.

Idempotency beyond ingest: corrections and webhooks

The same principle guards the other write paths. Outcome corrections supersede rather than edit, and a redelivered correction with the same key must not stack supersessions. The signed outcome webhook pairs idempotency with its HMAC signature: the signature proves the delivery is genuinely from the account's configured source, and the key proves it is not a second counting of the same fact. Authenticity and uniqueness are separate properties; money-grade ingest needs both.

Budget alerts inherit the discipline too: an alert that fired must not fire again for the same threshold, which is idempotency applied to notifications. A metering system that double-counts in any of these places eventually produces a number someone must apologize for.

What to verify in any metering tool you adopt

Ask three questions of any system that will hold billing-adjacent events. Does the ingest API accept idempotency keys, and is the duplicate behaviour documented — acknowledged, not errored, and never double-written? Are invalid events rejected without partial writes? And can the stored record show you the key, so a dispute can be settled by inspecting the actual deliveries? A vendor that answers these cleanly has thought about money; one that has not will teach you the lesson expensively.

OutcomeMeter's answers are in its behaviour: keys on cost and outcome ingest, duplicates never counted twice, invalid events rejected and writing nothing, append-only records thereafter. That combination — not any single feature — is what makes the numbers on the invoice boring, and boring is the highest praise billing data can earn.

Make your event ingest duplicate-proof: the 5-action checklist

Whether you send events to OutcomeMeter or run your own pipeline, the contract is the same — verify each property explicitly.

  1. Attach a key to every event: a UUID or your source system's event ID, generated once per logical event.
  2. Store the key in an outbox: retries must resend the stored key, never mint a fresh one per attempt.
  3. Test the duplicate: deliver one event twice in staging and confirm the recorded totals move once.
  4. Test the invalid event: malformed payloads must be rejected with nothing written — no partial rows.
  5. Inspect keys in a dispute drill: pick a recorded event and trace its deliveries by key before a client ever asks.

Worked example: a fictional retry storm that billed nobody twice

A fictional agency, Wexford Lane, sends cost events from a queue worker. One evening its provider's network path degrades: the worker's deliveries time out at the client side while succeeding at the server, so the worker retries aggressively. Over three hours, 4,180 logical events are delivered 11,940 times. Because each event carries its stored idempotency key, the ledger records 4,180 events; the client's attributed spend for the evening is $312.44 — exactly the work performed.

The counterfactual is easy to compute and hard to survive: naive ingest would have recorded roughly $893 of spend, blown the client's monthly budget in a night, fired both budget alerts, and put a fictional number on a real invoice. The agency only learns the storm happened because delivery logs show the retries. In the ledger, correctly, nothing happened at all.

OutcomeMeter runs this measurement on your own provider spend: connect a key on the free tier, watch the demo measurement first if you want to see the shape of the output, and read accepted-outcome economics when you are ready to value accepted work. The guides index collects every guide in this series.

Frequently asked questions

What is an idempotency key in event ingest?

A unique identifier attached to an event so repeated deliveries of the same event are counted once. The receiver stores the key with the record it created; a later delivery carrying the same key is acknowledged as a duplicate and writes nothing new.

Why do duplicates happen if my code is correct?

Because the transport cannot distinguish 'request lost' from 'response lost'. A client that times out after the server processed its event will retry, correctly, and the event arrives twice. Correct systems assume redelivery and defend against double-counting.

What does OutcomeMeter do with a duplicate event?

It recognizes the idempotency key and does not count the event again — a duplicate delivery never charges twice. Invalid events are a different case: they are rejected and write nothing at all.

Can two legitimate events share a key by accident?

Only if the sender reuses keys, which is a sender bug with a serious symptom: the second event is suppressed as a duplicate and spend goes missing. Keys must be unique per logical event — use the source system's event ID or a UUID stored in an outbox.

Does the outcome webhook use idempotency too?

Yes, alongside its HMAC-SHA256 signature. The signature proves the delivery genuinely comes from the account's configured source; the idempotency key proves it is counted once. Money-adjacent ingest needs both properties.

Keep reading

← Back