Skip to content
Docs
Request access

Safe retries

A network can lose the answer to a write after the server has already done the work. If you send the request again, it might run twice. An Idempotency-Key header makes the retry safe on the routes that take it: the server returns the first answer and runs nothing a second time.

This page is for people who call the API themselves (a script, a proxy that retries). The command line and the app send no key. The same rules are in the API reference and the error reference.

  • You call the API yourself, from a script or through a proxy that retries. The command line and the app send no key.
  • A running server and a session or API token allowed to make the write.
  • A source of random keys, such as a UUID generator.

Make one new random key for each thing you mean to do, and reuse it only to retry that same request.

Terminal window
curl -X POST "$NURAMESH_URL/v1/runs/RUN_ID/stop" \
-H "Authorization: Bearer $NURAMESH_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: obviously-fake-example-key-0001" \
-d '{}'
  • Send the header exactly once. Two header lines are 400 idempotency_key_invalid.
  • The key is 16 to 128 characters: letters, digits, ., _ and -. Spaces at either end are ignored. Anything else is 400 idempotency_key_invalid. The error never repeats the key.
  • Never put a secret in a key. Make it random, such as a UUID.
  • The key counts only for the same organization, the same signed-in person or token, the same method and the same route. Another person sending the same key starts a separate request.
You send The server answers
The first request It runs. A 2xx answer is stored for 24 hours.
The same key and the same request again The stored answer, with the same status, body and the stored headers, plus Idempotent-Replayed: true. Nothing runs again.
The same key while the first request still runs 409 idempotency_in_progress with Retry-After: 1. Wait and send the same request again. A running request holds the key for 120 seconds; after that another attempt may take over.
The same key with a different request 422 idempotency_key_reused. The attempt is also written to the security log.
The same key after 24 hours A new request: it runs again.

“The same request” means the same method, path and query, the same media type and the same body. A JSON body is compared by its content, so the order of its fields does not matter; a body that holds a repeated field name, a decimal number or a number too large to be safe is compared byte for byte. Other headers, credentials and the key itself are not part of the comparison.

Only a 2xx answer is stored. A refusal (4xx) or an error (5xx) frees the key, so you may fix the request and send it again with the same key. An answer over 256 KiB, or one that sets a cookie, is never stored. A keyed request body over 1,000,000 bytes is refused with 413 too_large and nothing is stored.

A replay carries only these stored headers: content-type, cache-control and location. Nothing else from the first response comes back (no cookie, no date).

Two kinds of delivery apply. Exactly once: the change and the stored answer are written in one transaction, so either both exist or neither does. At least once: the route has side effects outside that transaction, such as a run start or a connection test. The answer is stored after the route has run. If it cannot be stored, you get 503 idempotency_unavailable and the detail says “The request may have been carried out”; check the current state before you retry.

  • POST /v1/auth/tokens/{token_id}/revoke (exactly once)
  • POST /v1/auth/users/{user_id}/disable (exactly once)
  • POST /v1/auth/users/{user_id}/enable (exactly once)
  • PUT /v1/auth/users/{user_id}/role (exactly once)
  • POST /v1/auth/users/{user_id}/mfa/reset (exactly once)
  • PUT /v1/auth/settings/mfa (exactly once)
  • POST /v1/auth/invites/{invite_id}/revoke (exactly once)
  • POST /v1/agents/{agent_id}/credentials/{credential_id}/revoke (exactly once)
  • POST /v1/agents/{agent_id}/credentials/revoke-all (exactly once)
  • POST /v1/runs (at least once)
  • POST /v1/runs/{run_id}/stop (exactly once)
  • POST /v1/approvals/{approval_id}/decision (exactly once)
  • POST /v1/approvals/{approval_id}/approve (exactly once)
  • POST /v1/approvals/{approval_id}/reject (exactly once)
  • POST /v1/budgets/{budget_id}/unblock (exactly once)
  • POST /v1/org/resume (exactly once)
  • POST /v1/org/roles/{role}/resume (exactly once)
  • POST /v1/org/breakers/{name}/reset (exactly once)
  • POST /v1/org/breakers/{name}/disable (exactly once)
  • POST /v1/org/breakers/{name}/enable (exactly once)
  • POST /v1/models/catalog/entries (exactly once)
  • POST /v1/models/catalog/edit (exactly once)
  • POST /v1/models/catalog/entries/edit (exactly once)
  • POST /v1/models/catalog/approve (exactly once)
  • POST /v1/models/catalog/entries/approve (exactly once)
  • POST /v1/models/catalog/pause (exactly once)
  • POST /v1/models/catalog/entries/pause (exactly once)
  • POST /v1/models/catalog/retire (exactly once)
  • POST /v1/models/catalog/entries/retire (exactly once)
  • POST /v1/models/catalog/remove (exactly once)
  • POST /v1/models/catalog/entries/remove (exactly once)
  • POST /v1/models/catalog/prices (exactly once)
  • POST /v1/models/catalog/regions (exactly once)
  • PUT /v1/models/catalog/regions (exactly once)
  • POST /v1/models/assignments (exactly once)
  • PUT /v1/models/assignments/{scope}/{subject} (exactly once)
  • POST /v1/models/connections/{provider}/remove (exactly once)
  • PUT /v1/models/connections/{provider} (exactly once)
  • POST /v1/runs/{run_id}/tasks/{task_id}/gates/{gate_id}/decision (at least once)
  • POST /v1/runs/{run_id}/tasks/{task_id}/gates/{gate_id}/decisions (at least once)
  • POST /v1/org/halt (at least once)
  • PUT /v1/org/prompt-cache (exactly once)
  • POST /v1/org/roles/{role}/pause (at least once)
  • POST /v1/models/connections/{provider}/test (at least once)
  • POST /v1/autonomy/grants (exactly once)
  • POST /v1/autonomy/grants/{grant_id}/revoke (exactly once)
  • PUT /v1/autonomy/ceilings/{role}/{kind} (exactly once)
  • PUT /v1/autonomy/settings/{name} (exactly once)
  • PUT /v1/autonomy/model-change-floor/{role} (exactly once)

A key on a route that is not listed here and not on the next two lists is 400 idempotency_key_unsupported (“Idempotency-Key not supported on this route yet”). Read routes (GET) never take a key and the server ignores the header on them; repeat them freely.

Starting a run (POST /v1/runs) uses the key a second way: the run id is made from the organization, the person, the route and the key. The same key and body is the same run. After the key has expired, the same body answers already_requested; another body gets 409 run_id_conflict. Without a key a retry starts a new run.

These routes answer 400 idempotency_key_unsupported when you send the header. The text says why:

  • A route that changes nothing says “This route changes nothing, so it takes no Idempotency-Key. Repeat it freely.”

  • A route whose answer or request carries a secret (a password, a token, a key, an invitation link) says “This route cannot be repeated safely with an Idempotency-Key. Send the request without the header.” The server never stores such an answer, so it could never be replayed.

  • POST /v1/config/validate

  • POST /v1/costs/quote

  • POST /v1/auth/login

  • POST /v1/auth/login/mfa

  • POST /v1/auth/login/mfa/enroll

  • POST /v1/auth/login/mfa/enroll/confirm

  • POST /v1/auth/refresh

  • POST /v1/auth/logout

  • POST /v1/auth/setup/redeem

  • POST /v1/auth/tokens

  • POST /v1/auth/invites

  • POST /v1/auth/invites/{invite_id}/resend

  • POST /v1/auth/invites/inspect

  • POST /v1/auth/invites/accept

  • POST /v1/auth/me/email

  • POST /v1/auth/email/verify

  • POST /v1/auth/password-reset/request

  • POST /v1/auth/password-reset/redeem

  • POST /v1/auth/users/{user_id}/password-reset

  • POST /v1/auth/tokens/revoke-by-link

  • POST /v1/agents/{agent_id}/credentials

  • POST /v1/agents/{agent_id}/credentials/{credential_id}/rotate

  • PUT /v1/models/connections/{provider}/key

On these routes the header is not read: no 400, nothing stored, and the request runs as if no key was sent. They hold a password, a one-time code or an operation that is safe to repeat itself.

  • POST /v1/auth/mfa/enroll
  • POST /v1/auth/mfa/enroll/confirm
  • POST /v1/auth/mfa/recovery-codes
  • POST /v1/auth/mfa/disable
  • POST /v1/auth/password/confirm
  • DELETE /v1/models/connections/{provider}/key

A stored answer is sealed (encrypted) with the organization’s content key before it is written. The key itself is stored only as a hash. The server keeps only these facts about a request: the key hash, a fingerprint of the request, the status and the sealed answer. It does not keep the request body.

Stored answers never hold a secret: a route whose answer carries one is on the refuse list above. If a person is erased, answers that name them are removed with them, and an organization whose content key was destroyed can no longer open any stored answer: a replay then answers 409 idempotency_response_unavailable (“the original request was carried out but its response can no longer be shown”). Check the current state and use a new key.

Keys are removed after 24 hours by a sweep that runs about every hour.

A keyed request needs the server’s content extra and its key ring.

  • No key ring, or it cannot be read: 503 idempotency_unavailable with Retry-After: 5. The request was not run. Send it again later, or without the header.
  • The store cannot be reached: the same 503, also not run.
  • An organization whose content key was destroyed: a keyed write is 409 org_key_destroyed, except the routes that only reduce access. Run stop, organization halt, role pause, disabling a person, and revoking a token or an agent credential run as if no key was sent.
  • The three revoke routes (token, agent credential, revoke-all) also run as if no key was sent when the key ring cannot be read, so revoking never waits on a key.
  • A request without the header is never affected by any of this.
  1. Make a random key and send the write.
  2. If you get no answer, or a 5xx, send the same request with the same key.
  3. On 409 idempotency_in_progress, wait one second and repeat.
  4. On 503 idempotency_unavailable for an at-least-once route, read the current state before you retry.
  5. Never reuse a key for a different request. That is 422 idempotency_key_reused.

To check the command line that a person uses to stop a run (it sends no key):

Terminal window
nuramesh stop --help

Send the same request twice with the same key. The second answer has the same status and body as the first, plus the header Idempotent-Replayed: true, and nothing ran a second time: a stopped run has one run.stop_requested entry in its ledger, not two.

You see Do this
400 idempotency_key_invalid The key is not 16 to 128 letters, digits, ., _ or -, or the header was sent twice
409 idempotency_in_progress The first request still runs. Wait the second of Retry-After and send the same request again
422 idempotency_key_reused The key was used for a different request. Make a new key for each thing you mean to do
503 idempotency_unavailable The answer could not be stored and the request may have been carried out. Check the current state before you retry
413 too_large A keyed request body is over 1,000,000 bytes
idempotency_key_unsupported The route refuses a key: see Routes that refuse a key

Was this page helpful?