Core concepts
Idempotency and retries
Retry after a lost response without paying twice.
A network error doesn't tell you whether the server got your request. If you create a job and the response is lost, sending the request again could start and charge a second job. Idempotency keys prevent that.
How it works
Every POST /v1/jobs and POST /v1/files needs an Idempotency-Key header. Use a fresh random value, like a UUID, for each new job. The key can be 1 to 200 visible ASCII characters with no spaces.
| You send | You get |
|---|---|
| A new key | A new job. |
| The same key and the same body | The original job, with header X-Idempotent-Replay: true. Nothing new is charged. |
| The same key with a different body | 409 idempotency_key_conflict. Use a new key for a different job. |
| A missing key | 400 idempotency_key_required |
Two bodies count as the same if they have the same model, input, quote, capability version, max_charge_amount and client_reference_id. Whitespace and key order don't matter, and "1.00" and "1.000000" are the same amount.
When to retry
- Timeout, dropped connection or 5xx: retry with the same key. The job may already exist, and the same key returns it instead of making another.
- 429: wait for the number of seconds in
Retry-After, then retry with the same key. - Any other 4xx: don't retry as is. Fix the request or your wallet, then send it with a new key.
- A job that failed: the key is used up. Replaying it returns the same failed job. To try again, send a new job with a new key. That job is charged separately.
What the clients do
The TypeScript and Python clients make a key for every submit() unless you pass one, reuse it for their own retries, and return it as idempotencyKey (TypeScript) or idempotency_key (Python). It is also on every error they raise. They retry up to 3 times with backoff, follow Retry-After, and never retry invalid input, auth, payment or conflict errors. They never start a new job after a failure.
import { Astorie } from "@astorie/client";
const client = new Astorie();
const input = {
prompt: "A red chair in a sunlit studio",
aspect_ratio: "1:1",
resolution: "1K",
output_format: "png",
};
const key = crypto.randomUUID();
await db.saveOrder(orderId, { idempotencyKey: key });
// Safe to call again after a crash: the same key returns the same job.
const { job, replayed } = await client.submit("google/nano-banana-2", input, {
maxChargeAmount: "1.000000",
idempotencyKey: key,
});Idempotency records are kept for at least seven days. Don't rely on replaying a key older than that.