文档
Idempotency keys and safe retries
POST /v1/generations requires an Idempotency-Key header of up to 255 printable ASCII characters: sending the same key with the same body returns the original job, so a network retry never charges twice.
Rules
| Situation | Result |
|---|---|
| New key | A new job is created and charged |
| Same key, same body | The original job, with header Idempotent-Replayed: true; no second charge |
| Same key, different body | 409 FLOW_IDEMPOTENCY_CONFLICT |
| Missing header | 400 FLOW_INVALID_REQUEST |
Key order inside the JSON body does not matter: {"a":1,"b":2} and {"b":2,"a":1} count as the same body.
Choosing keys
Derive the key from your own record, for example order-1234-image, so the same unit of work always maps to the same key. A random UUID per attempt defeats the purpose.
Retrying
- Retry on network errors, timeouts and errors with
retryable: true(429,503). - Reuse the same
Idempotency-Keyand body. - Back off: wait the
Retry-Afterseconds on a429, otherwise 1 s, 2 s, 4 s, up to about 30 s. - Do not retry
retryable: falseerrors unchanged; fix the request first.
常见问题
- Is the Idempotency-Key header required?
- Yes. POST /v1/generations answers 400 without it; the other endpoints do not need it.
- Can a retry charge me twice?
- No. A retry with the same Idempotency-Key and body returns the first job instead of creating a new one.
- What happens if I reuse a key with a different body?
- The API answers 409 FLOW_IDEMPOTENCY_CONFLICT and creates nothing.