Idempotency
Networks drop, timeouts fire before the answer. To make sure a repeated launch is never charged twice, POST /generations requires an Idempotency-Key header.
http
Idempotency-Key: 6f1c2b0e-3f7a-4d1e-9b8a-2a4c5e7d9f10Rules
- The key is a string of printable ASCII characters without spaces, 1 to 80 long. A UUID works well.
- One key · one logical operation. A new generation · a new key.
- The key is remembered for 24 hours, per API key.
- Without the header the request is refused before any charge: 422
invalid_parameters,details.param = "Idempotency-Key".
What a repeat returns
| Repeat | Answer |
|---|---|
| Same key, same body | 200, the same generation, meta.replayed: true, no second charge |
| Same key, different body | 409 idempotency_conflict |
| The first request is still running | 409 idempotency_in_progress, retryable: true: retry in a few seconds |
How to retry
- Create the key before the first attempt and keep it with the task.
- On a timeout, a dropped connection,
429or5xx, retry with the same key and the same body. - A
200withreplayed: trueis your first generation; poll it from there.
python
key = str(uuid.uuid4())
for attempt in range(5):
try:
res = requests.post(f"{BASE}/generations", headers={**headers, "Idempotency-Key": key}, json=body, timeout=30)
except requests.RequestException:
time.sleep(2 ** attempt)
continue
if res.status_code in (200, 202):
break
err = res.json()["error"]
if not err["retryable"]:
raise RuntimeError(err["code"])
time.sleep(err.get("retry_after_seconds", 2 ** attempt))POST /estimate charges nothing and needs no key.