> ## Documentation Index
> Fetch the complete documentation index at: https://docs.argil.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotency

> Retry a generation request safely: the same key never starts or bills twice.

Networks drop connections and clients time out. If you retry `POST /v2/generations` after a failure you could not see, the first request may have gone through, and you would pay for two generations. An **idempotency key** makes the retry safe.

## Send a key

Generate a unique key for each generation you want, such as a UUID, and send it in the `Idempotency-Key` header. Keep the same key for every retry of that request.

```bash theme={null}
curl https://api.argil.ai/v2/generations \
  -H "Authorization: Bearer $ARGIL_API_KEY" \
  -H "Idempotency-Key: 2f6c1d3a-7b4e-4c2a-9e8f-0d1b5a6c7e93" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "nano-banana-2.1",
    "mode": "text-to-image",
    "input": { "prompt": "Studio photo of a ceramic mug with steam, soft light" }
  }'
```

The key is 1 to 255 printable ASCII characters. A key derived from your own ids (`order-1042-hero-v1`) works too, as long as it is unique to one generation.

## What happens on a retry

| The retry has | Argil answers |
| - | - |
| The same key and the same request | `201` with the generation the first request created, in its **current** state, and `Idempotent-Replayed: true`. Nothing is started or billed again. `Prefer: wait` works as usual. |
| The same key and a different request | `422 idempotency_key_reused`. Nothing is created. Use a new key for a new request. |
| The same key, after you deleted its generation | `422 idempotency_key_reused`. Nothing is created. Use a new key. |
| A new key | A new generation. |

**The same request** means the same `model`, `mode`, `input`, `metadata`, `webhook_url` and `max_credits`. The order of JSON keys does not matter.

**Concurrent requests** with the same key are safe: one creates the generation and the others answer it, with `Idempotent-Replayed: true`. Only one is billed. In the rare case a concurrent request cannot read it yet, it gets `409 idempotency_key_in_use`: retry the same request in a few seconds.

## Lifetime

* **How long a key holds.** For good: a key stays bound to the generation it created, even after you [delete the generation](https://docs.argil.ai/v2/generate/generations.md#deleting).
* **Scope.** Keys are scoped to your workspace: two workspaces can use the same key.

## Which requests take a key

`POST /v2/generations`, the request that spends credits, and the two that start an agent's turn:

* **`POST /v2/sessions`:** a retry returns the session the key started, in its current state.
* **`POST /v2/sessions/{id}/messages`:** a retry returns the session with the turn the key started, even while that turn runs or after it answered. Keys are per session.

For these two, Argil does not compare the requests: a key used again returns what it first started. Use a new key for each new session and each new message.

Other requests do not use the header:

* **`POST /v2/assets`:** sending the same file or URL again returns the asset you already have.
* **`DELETE` and `PATCH`:** repeating them changes nothing more.
* **`POST /v2/webhook_endpoints`:** check `GET /v2/webhook_endpoints` before creating one again.

## Errors that are safe to retry

A request refused with a [problem](https://docs.argil.ai/v2/platform/errors.md) created nothing. Retry it with the same key after fixing the cause, or after the delay `Retry-After` gives. A `5xx` or a dropped connection may or may not have created the generation: retry with the same key, and Argil sorts it out.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.