Retrying a failed request is one of the first resilience patterns any developer learns. For reads, it is nearly always safe. For writes, a careless retry is how one payment becomes two.
Agents make this harder. Ordinary software retries when its code says so. An agent may repeat an action because a tool response was unclear, because it lost track of what it already did, because its context was trimmed, or because it restarted and began the task again. Each repeat can look like a fresh, reasonable decision.
So a system that accepts agent writes cannot rely on the agent to avoid duplicates. It has to make repeats harmless.
Two layers of repetition
There are two separate places where a write can be repeated, and they need separate protection.
Between the agent and the gateway. The agent submits the same request again, deliberately or not.
Between the gateway and the system of record. The gateway calls an API, the answer does not arrive, and it has to decide whether to call again.
Teams often handle one and forget the other. A deduplicated agent request still produces a double charge if the layer below retries blindly.
The agent’s key
The standard tool for the first layer is an idempotency key: a value the caller generates for one logical action and sends with each attempt.
In Sefra, a key is required on every state-changing submission. A write without one is refused with IDEMPOTENCY_KEY_MISSING. Reads need none. The gateway then treats the key in three ways:
| Situation | Result |
|---|---|
| New key | A new execution |
| Same key, same content | The existing execution, marked replayed: true, with no new write |
| Same key, different content | Refused with IDEMPOTENCY_KEY_REUSED |
The third row is the one simple implementations miss. A key alone only shows that a request was seen before. Storing a fingerprint of the payload alongside it also catches a caller that reuses a key for a different action, for example the same key with a changed quantity. Silently returning the earlier result would tell the agent that its new request succeeded when nothing of the kind happened.
A refused request does not use up its key. If an order is rejected for a schema error, the agent can correct it and resubmit with the same key, and exactly one order is created.
When the agent changes the key
An idempotency key protects against repeats that the caller recognises as repeats. An agent that decides to do the task again may simply generate a new key.
That needs a business-level defence. A policy rule can read Sefra’s count of recent identical requests from the same principal and deny a repeat. In our starter procurement library, a rule named procurement.duplicate denies the same input sent with a new key.
Rate limits add a coarser protection for an agent stuck in a loop. In Sefra, a principal with too many submissions rejected by validation or policy in a short window is suspended until an operator lifts the suspension.
Sorting failures before retrying
At the second layer, the gateway needs to know what a failed call means. Most retry logic sorts failures into “worked” and “failed”. A network adds a third answer. We sort every attempt into four classes:
| Class | Example | Retry |
|---|---|---|
| Applied | A declared success, with the expected output fields | No need |
| Refused | A validation error from the system | No: it will not help |
| Not applied, transient | The connection failed before sending, or the system said it is overloaded | Yes |
| Ambiguous | A timeout, a reset after sending, most server errors | Only if the operation is safe to repeat |
The fourth class is wider than people expect. A generic server error can be raised after the database commit. So can a dropped connection. Neither tells you whether the write took effect.
Declaring how each operation repeats
Whether an ambiguous call can be repeated is a property of the operation, so it should be declared with the operation. Each Sefra connector operation has one of three idempotency modes.
header. The system supports idempotency keys. Sefra sends a stable token in a header such as Idempotency-Key, and the system recognises the repeat. The token is derived from the execution and step, so it stays the same across retries and worker restarts.
natural. The operation is harmless to repeat by its nature. Setting a status to cancelled is one example.
none. Repeating could do damage. After an ambiguous attempt, Sefra never calls it again.
For the first two modes, Sefra repeats the call up to a fixed limit, with backoff. Mode header sends the same token each time. If a worker is killed during a call and restarted, the execution ends succeeded with exactly one purchase order.
Ask the system before giving up
An operation in mode none may still be recoverable. A manifest can declare a probe: a read that tells whether the effect exists, usually a lookup by a natural key.
If the probe finds the effect, the step counts as succeeded and takes its output from the probe’s response. If it finds nothing, the step has failed. Say the connection drops after a vendor is created. The probe finds the vendor, the plan continues, and exactly one vendor exists.
When nothing can settle it
If repeats are unsafe or exhausted, and there is no probe or no definite answer from one, the machine has run out of knowledge. The honest response is to say so.
Sefra marks the step unknown and stops with needs_intervention. It does not retry and does not compensate. The agent is told not to submit the action again, and a person checks the system of record and settles the outcome.
A short checklist
If you are building this yourself, these are the parts to get right:
- Require an idempotency key on every write from an agent.
- Store a fingerprint of the content with the key, and refuse a mismatch.
- Keep the key free when a request is refused before acceptance.
- Add a business-level duplicate rule for agents that generate new keys.
- Classify failures, and treat a timeout after sending as ambiguous.
- Declare per operation whether it may be repeated.
- Use one stable token per step across every retry.
- Provide a lookup where the system allows it.
- Give the remaining cases to a person.
None of this is new to anyone who has built a payment system. What agents change is how often the unusual path runs, and who is on the other end when it does.