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");
}