Idempotency and retries
Retry an action call safely with an Idempotency-Key, and learn why Anlyon never resends a side effect it cannot confirm.
Two things get confused here, so this page separates them:
- Idempotency is how you retry safely. You send an
Idempotency-Key, and a retry with the same key is answered from the first call instead of running again. - Automatic retry is something Anlyon does for event delivery and deliberately does not do for an action invocation.
Why it matters for an agent
Agents and their frameworks retry. A tool call times out, the model asks again, a worker restarts and replays its queue. Without a key, each of those can become a second refund or a second email.
- Risk. A retry with the same key never reaches the destination again. A retry of an action is answered from the recorded invocation for as long as that invocation exists, not only for a cache window.
- Effort. The SDKs send a key on every invoke and reuse it on their own transport retries. You pass your own key only where your code can call
invoketwice for one business operation. - Cost. A replayed call is not executed, admitted or charged again.
Send an idempotency key
Send Idempotency-Key on a mutation. The key is claimed atomically before the handler runs, so concurrent retries perform the side effect at most once. Keys are 1 to 255 characters.
import { Client } from '@anlyonhq/sdk';
const agent = new Client({ apiKey: process.env.ANLYON_API_KEY! });
const orderId = 'ord_42';
const { data } = await agent.actions.invoke(
'refund-order',
{ charge: 'ch_3P9x', amount: 12000 },
{ idempotencyKey: `refund:${orderId}` },
);
console.log(data!.id, data!.status);curl -s -i -X POST https://api.anlyon.com/api/v2/actions/refund-order/invoke \
-H "Authorization: Bearer $ANLYON_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: refund:ord_42" \
-d '{ "input": { "charge": "ch_3P9x", "amount": 12000 } }'import os
from anlyon import Client
agent = Client(api_key=os.environ["ANLYON_API_KEY"])
order_id = "ord_42"
result = agent.actions.invoke(
"refund-order",
{"charge": "ch_3P9x", "amount": 12000},
idempotency_key=f"refund:{order_id}",
)
print(result.data["id"], result.data["status"])Choose a key derived from the operation, not from the attempt. refund:ord_42 is the point; refund:${Date.now()} defeats it.
The TypeScript SDK from @anlyonhq/sdk 2.2.0, and the Python SDK anlyon, send an idempotency key on every invoke and generate one when you do not pass one. The generated key is reused on each transport retry of that one call, so a resend after a lost response is answered from the recorded invocation. A generated key covers one call to invoke: two calls are two invocations. When your own code can call invoke twice for one business operation, pass your own key. The generated key is not returned in the response.
How a repeated key is answered
For operations other than action invocations, the rules are:
| Situation | What happens |
|---|---|
| Same key, same request | The original response replays, with an X-Idempotent-Replay: true header. |
| Same key, different request | 409 with error code idempotency_key_reuse. “Same request” means the same method, path, workspace, environment, caller (key, OAuth app or console user, and end user) and JSON body. |
| Duplicate arrives while the first is still in flight | It waits up to 10 seconds, then replays. If the first has not finished, the duplicate gets 409 idempotency_request_in_progress with Retry-After: 1. |
The original request failed (non-2xx) |
The key is freed, so you can retry it. Only successful responses are stored. |
| The original succeeded with a response over 256 KB | The response cannot be replayed. A retry gets 409 idempotency_result_not_stored, and the operation is not run again. |
| More than 24 hours later | Stored responses are kept for 24 hours. For most operations the key is then unknown again. Action invocations are the exception, below. |
Action invocations keep their key for life
On POST /api/v2/actions/{ref}/invoke, the key is also recorded on the invocation itself and kept for as long as the invocation exists. For gated actions, invocation creation, approval admission and approval linkage commit together. A refused admission leaves no invocation or reserved key.
| Situation | What happens |
|---|---|
| Same key, same request, any time later | Answered from the invocation as it is now, with X-Idempotent-Replay: current-state: 200, or 202 while it still waits for approval. It is not executed, admitted or charged again. |
The invocation failed, or is unknown |
The retry shows that outcome. The key is not freed: running the action again is a new decision. Resolve an unknown one first, then invoke with a new key and retryOf. See Receipts and grades. |
The request was refused before an invocation existed (invalid input, action disabled, 402 PAYMENT_REQUIRED, 429 QUOTA_EXCEEDED, 409 END_USER_NOT_CONNECTED) |
Nothing was recorded, so the key is still unused. |
| Same key from another credential, for another end user, or with a different body | 409 idempotency_key_reuse, without revealing the invocation. |
This holds for console sessions as well as API keys and OAuth, and when the short-term replay store has lost the key.
Operations that accept an idempotency key are marked in the API reference. Both SDKs accept one on every operation that supports it.
Name the business operation with operationKey
On a governed action you can also pass an operationKey: 1 to 200 characters of letters, digits and . _ : -. Repeating the same operationKey with the same request returns the existing effect, and nothing is sent again. Reusing it with a different request is refused with 409 OPERATION_KEY_REUSE. When you pass none, the Idempotency-Key is used.
await agent.actions.invoke(
'refund',
{ paymentIntent: 'pi_3Pabc', amount: 1250, currency: 'usd' },
{ operationKey: 'order-4411-refund', idempotencyKey: 'order-4411-refund' },
);curl -s -X POST https://api.anlyon.com/api/v2/actions/refund/invoke \
-H "Authorization: Bearer $ANLYON_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-4411-refund" \
-d '{ "input": { "paymentIntent": "pi_3Pabc", "amount": 1250, "currency": "usd" }, "operationKey": "order-4411-refund" }'agent.actions.invoke(
"refund",
{"paymentIntent": "pi_3Pabc", "amount": 1250, "currency": "usd"},
operation_key="order-4411-refund",
idempotency_key="order-4411-refund",
)Two different keys are two operations: two refunds of the same payment under two keys are two refunds. What bounds that is an impact limit.
Provider idempotency on governed actions
The Idempotency-Key above is between you and Anlyon. A governed action also has an identity between Anlyon and the provider, and it differs by adapter.
| Action | Key Anlyon sends to the provider | Replay of a lost write |
|---|---|---|
stripe.refund |
The effect’s Idempotency-Key, persisted before the write. The adapter treats the key as usable for 24 hours. |
Offered inside 24 hours. After that a repeat could create a second refund. |
resend.email_send |
The effect’s Idempotency-Key. As of 2026-10-03, Resend keeps keys for 24 hours and returns its original response for the same key and payload, sending nothing. |
Offered inside 24 hours. After that a repeat could send a second email. |
github.file_update |
None. The adapter sends the reviewed blob SHA as a precondition instead. | Not offered. |
| A declared action | None. | Not offered. A replay is refused before any write. |
A replay of a declared action answers 409 REPLAY_REFUSED with the reason in the message, and nothing is sent:
{
"success": false,
"error": { "code": "REPLAY_REFUSED", "message": "Replay refused (replay_unsupported): http.declared does not support replay: Not supported. Without a provider idempotency key a repeated write could apply twice, so a replay is refused before anything is sent." },
"requestId": "req_0123456789abcdef0123456789abcdef"
}
Reconciliation of a declared action repeats the read-back, never the write. See When the outcome is unknown.
Why an invocation is not retried for you
When Anlyon dispatches an action and the request times out, the socket dies after the request was written, or the destination answers 5xx, the destination may have processed it. Anlyon records that as unknown rather than failed, and stops.
It would be easy to retry. It would also be how one refund becomes two.
So the contract is: Anlyon tells you what it knows, including “I cannot confirm this”, and you reconcile with the destination before deciding. An unknown invocation carries a message saying so. See Receipts and grades for how to read the statuses.
An idempotency key does not make Anlyon send the request again. A retry with the same key and the same request is answered from the recorded invocation, and nothing reaches the destination. To run a failed invocation again, invoke with a new key and retryOf. An unknown invocation must be resolved first.
The SDKs do retry their own HTTP request to Anlyon on a timeout, a 429 or a 5xx from Anlyon, with the same idempotency key. That is safe for the reason above: the resend is answered from the recorded invocation. They do not resend a 409.
Retries that do happen
Event deliveries to your webhook subscriptions retry with backoff. Those retries apply to delivery, not to action invocations. A webhook delivery is safe to retry because the receiver is expected to be idempotent about it. A refund is not.
Errors you may see
| Status and code | When | What to do |
|---|---|---|
409 idempotency_key_reuse |
The key was used for a different request, caller or end user. | Use a new key for a new operation. Keep the same body to repeat one. |
409 idempotency_request_in_progress |
The first request with this key is still running. | Retry with the same key after Retry-After. |
409 idempotency_result_not_stored |
The first request succeeded and its response was too large to store. | Read the result another way (for example, list the resource). Do not re-run with a new key. |
409 BUDGET_EFFECT_ALREADY_RESERVED |
A reservation for this operation exists although the key record is gone. | Find the original outcome before choosing a new key. |
409 OPERATION_KEY_REUSE |
The operationKey names a different request. |
Repeat with the same request, or use a new operationKey. |
409 REPLAY_REFUSED |
A replay of an effect failed a control. | Reconcile or resolve the effect. |
How it behaves
- Idempotency keys are the caller’s. Two different keys are two operations. An impact limit bounds that.
- An action invocation is sent once. When the outcome cannot be confirmed, the invocation is recorded
unknownand you reconcile before retrying. - A declared action sends its write once. It sends no provider idempotency key. A replay is refused before anything is sent, and reconciliation repeats the read-back.
- A provider key is usable for 24 hours. The Stripe and Resend adapters offer a same-key replay inside that window.
- A GitHub file update is recovered by lookup. The adapter sends the reviewed blob SHA as a precondition, and reconciliation looks for the commit that carries the effect trailer.
Related
- Receipts and grades: reading
succeeded,failedandunknown, and retrying on purpose. - Governed effects: operation identity and replay.
- When the outcome is unknown
- Actions: defining and invoking the call.
- API reference: Invoke an action.

