A re-planning agent resends an intent with new arguments. Intent id, version and payload fingerprint tell a retry from a revision; 2 refunds paid instead of 6.
A retry and a revision look the same on the wire
Geetha's refund agent at Belgavi's Book Store works help-desk case CASE-4101. The customer reports a damaged paperback, and the agent plans a refund of 30.00 for order BB-1201. The call to the refund service times out, so the agent's runtime sends the same request again. A minute later the planner restarts after a deployment, reloads the case and sends the refund once more. Then the agent reads the returns policy, notices that shipping is not refundable, and decides the refund should be 24.00. It sends a refund for BB-1201 again.
From the refund service's point of view all four calls are a refund for the same order from the same caller. Two of them are retries of one decision. One is a retry that crossed a restart. The last is a different decision: a revised command. An idempotency scheme has to tell these apart, and the two common schemes each get one case wrong.
A key minted per request treats the restart as a new request, because the restarted planner mints a new key. A key derived from the business intent, such as refund:BB-1201, stops the duplicate but swallows the revision: the service sees a key it already knows and replays the stored result of the first plan. The agent believes it asked for 24.00. The service paid 30.00 and told the agent the request succeeded.
This was always possible with human callers, but people rarely revise a command seconds after sending it. Agents do it routinely, because re-planning is how they work: they act, observe, and act again with better information. Existing services that were safe for one-shot clients need a rule for the second action.
What the receiver should decide
Give each business intent an identity and a version, and fingerprint the payload. The receiver then has a small, deterministic decision table:
| Incoming command | Receiver decision |
|---|---|
| New intent id | Accept and record version, fingerprint and state pending |
| Same id, same version, same fingerprint | Retry: replay the stored outcome |
| Same id, same version, different fingerprint | Refuse: a version may not change meaning |
| Same id, higher version, earlier version still pending | Revision: supersede the pending version |
| Same id, higher version, earlier version already took effect | Refuse: the change needs a correction command |
| Same id, lower version | Refuse as stale |
The agent stays responsible for deciding what it wants. The receiver stays responsible for deciding what that means for an effect that may already have happened. Nothing in the table depends on the model, on timing, or on how many times a message was delivered.
The third row deserves attention. The IETF draft for the HTTP Idempotency-Key header already says that a server should reject a request that reuses a key with a different payload. An implementation that stores only the key skips that comparison, so a buggy client that changes the amount while reusing the key gets the old result back silently. With agents the comparison is not optional, because the same intent will legitimately be sent with different arguments, and the version is what separates a legitimate revision from an accident.
The receiver in code
The companion example keeps the state in memory to make the rules visible. The fingerprint is a hash of the canonical JSON form, so key order does not matter but values do.
def submit(self, cmd):
iid, ver, payload = cmd['intent_id'], cmd['version'], cmd['payload']
fp = fingerprint(payload)
cur = self.records.get(iid)
if cur is None:
self.records[iid] = Record(iid, ver, payload, fp)
return {'outcome': 'accepted', 'version': ver}
if ver < cur.version:
return {'outcome': 'refused_stale_version', 'current_version': cur.version}
if ver == cur.version:
if fp == cur.fp:
return {'outcome': 'replayed', 'version': ver, 'state': cur.state}
return {'outcome': 'refused_payload_mismatch', 'version': ver}
if cur.state == 'applied':
return {'outcome': 'refused_already_applied', 'applied_version': cur.version}
cur.history.append({'version': cur.version, 'fp': cur.fp, 'state': 'superseded'})
cur.version, cur.payload, cur.fp = ver, payload, fp
return {'outcome': 'superseded_previous', 'version': ver}A separate settle step plays the payment job: it applies whatever is pending. That separation is realistic. Many services accept a command, store it and apply it later, which is exactly the window in which a revision can still win.
The worked example
The example runs one script of ten steps through three receivers. The script contains the first plan, a retry with the payload keys in a different order, the retry after a planner restart with a new request key, the revision to 24.00, a buggy resend of version 2 with 26.00, a second intent for order BB-1207, a settlement run, a revision of BB-1207 after it was already paid, a stale version 1 retry that arrives late, and a final settlement.
| Receiver | Refunds paid | Duplicates | Stale amount paid | Intents handled correctly |
|---|---|---|---|---|
| Key per request | 6 | 4 | 0 | 0 of 2 |
| Key equals intent id | 2 | 0 | 1 | 1 of 2 |
| Intent id, version and fingerprint | 2 | 0 | 0 | 2 of 2 |
The per-request key paid BB-1201 four times: 30.00 twice, 24.00 and 26.00. The intent-id key paid BB-1201 once, but at 30.00, and returned success for the revision. The versioned receiver paid 24.00 and 12.50, refused the 26.00 resend because version 2 already meant 24.00, refused the BB-1207 revision because version 1 had been paid, and refused the late version 1 retry because version 2 had superseded it.
The BB-1207 case is the one teams most often get wrong. Once an effect has happened, a revision is no longer a replacement; it is a new business operation, such as a partial reversal, with its own approval and its own audit trail. The receiver should say so explicitly rather than silently applying a second effect or silently ignoring the request.
Where the version comes from
The version must be assigned by the component that owns the plan, not by the transport. In practice that is the agent's orchestrator, which persists the plan step before it calls a tool. A workable rule is: the intent id is derived from the business object and the kind of effect, and the version increments each time the orchestrator writes a changed plan for that intent. A retry of the same plan step reuses the stored version and payload, including after a restart, because both come from the persisted step rather than from the model's latest output.
Do not let the model choose the version number. A model that regenerates a call after a restart can easily emit version 2 for what is really a retry with slightly different formatting. The fingerprint would catch a changed amount, but the receiver would treat a reformatted retry as a revision. Keep the model's output as the payload and let deterministic code assign identity.
Migrating an existing service
Most services already accept an idempotency key. The migration can be incremental:
- Start recording a payload fingerprint next to every stored key, and log, but do not yet refuse, any reuse with a different fingerprint. This measures how often clients already reuse keys incorrectly.
- Add an optional intent version field. Clients that do not send one keep today's behaviour.
- For agent callers, require intent id and version, and turn on refusal for same-version payload changes.
- Add the supersede path only for operations that have a pending state, and the refuse-after-effect path everywhere.
- Return the decision in the response body, so the agent's trace records whether a call was a replay, a supersession or a refusal.
Storage is usually a table keyed by intent id with the current version, fingerprint, state and outcome, updated in the same transaction that records the command. The version check is a compare-and-set on that row, which also serialises two concurrent revisions.
Failure modes
- Fingerprint too wide. Including a timestamp or trace id in the fingerprint turns every retry into a mismatch. Fingerprint the business arguments only.
- Fingerprint too narrow. Leaving out a field that changes the effect, such as currency, lets a real change replay an old result.
- Version reset on restart. If the orchestrator does not persist the version, a restart sends version 1 again for a new plan, and the receiver refuses it as stale or replays it. Persist plan steps before calling tools.
- Supersession after a partial effect. Some effects are multi-step. Define pending as no externally visible step having happened, not as the final step not having happened.
- Revision races settlement. The supersede check and the settlement job must take the same row lock, or a revision can be accepted while the old version is being paid.
When you do not need this
If a tool is read-only, or naturally idempotent, such as setting a status to a fixed value, a version adds nothing. If the agent never re-plans an effect after issuing it, because every effect goes through a human approval that freezes the arguments, the approval record already plays the role of the version. The scheme pays off for effects that are pending for a while, can be revised, and are expensive to undo: refunds, orders, account changes and outbound messages.
Verification
Test the decision table directly, one case per row, with the effect store inspected after each step. Then replay recorded agent traces through the receiver and count replays, supersessions and refusals. A trace that produces a payload-mismatch refusal is either a client bug or a missing version increment, and both are worth knowing about before an agent pays a refund twice.