Skip to content

API docs · Reliability

Retry guidelines

Which errors to retry and how to back off.

SituationRetry?How
Network timeout, dropped connectionYesRetry with the same Idempotency-Key
429 rate_limitedYesWait as indicated by Retry-After, then retry
500 internal_errorYesExponential backoff, same idempotency key
422 asset_not_readyYesWait until the asset is available
400 / 401 / 403 / 404 / 409NoFix the request before sending it again
402 insufficient quota or limitNoTop up, or adjust the key limit or max_budget, before sending it again
Task failedDepends on the failure codeFollow the suggestion for the failure code; resubmissions need a new idempotency key

Recommended backoff: start at 1 second, double each time, add 0–500 ms of random jitter, and stop after 5 attempts. The platform already retries and fails over between channels internally, so you never need to resubmit the same task.

Node.js
// Use your own unique business ID as the idempotency key: retries after a timeout never create a duplicate task
async function submit(order) {
  const key = `order-${order.id}-v${order.revision}`;
  for (let attempt = 0; attempt < 5; attempt++) {
    try {
      const res = await fetch("https://cn-api.aitoken.tools/v1/videos/generations", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${process.env.AITOKEN_API_KEY}`,
          "Idempotency-Key": key,
          "Content-Type": "application/json",
        },
        body: JSON.stringify(order.payload),
      });
      if (res.status < 500 && res.status !== 429) return res.json();
    } catch {
      // Network error: retry with the same key
    }
    await new Promise((r) => setTimeout(r, 2 ** attempt * 1000 + Math.random() * 500));
  }
  throw new Error("submit failed");
}