跳到正文

API 文档 · 可靠性

重试建议

哪些错误该重试、怎么退避。

情况是否重试做法
网络超时、连接中断是带同一个 Idempotency-Key 重试
429 rate_limited是按 Retry-After 等待后重试
500 internal_error是指数退避,带同一个幂等键
422 asset_not_ready是等待素材变为 available
400 / 401 / 403 / 404 / 409否修正请求后再发
402 额度或限额不足否补充额度、调整 Key 限额或 max_budget 后再发
任务 failed视失败码而定见任务失败码的建议动作;重新提交需使用新幂等键

推荐退避:首次 1 秒,每次 ×2,加 0–500 毫秒随机抖动,最多 5 次。平台在内部已经做了渠道级重试与切换,你不需要为同一个任务重复提交。

Node.js
// 业务侧唯一 ID 作为幂等键:超时重试不会重复建任务
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 {
      // 网络错误:继续用同一个 key 重试
    }
    await new Promise((r) => setTimeout(r, 2 ** attempt * 1000 + Math.random() * 500));
  }
  throw new Error("submit failed");
}