Agent 调用工具总会失败,网络抖、参数错、服务挂。分享一下我用的重试模式,比较稳。
核心是:区分"可重试错误"(超时、限流)和"不可重试错误"(参数校验失败、权限拒绝),前者指数退避重试,后者直接把错误回喂给模型让它调整。
async function callTool(tool, input, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
return await tool.run(input);
} catch (e) {
if (!e.retryable || i === retries - 1) return { error: e.message };
await sleep(2 ** i * 500);
}
}
}
把错误回喂给模型这一步很关键,比无脑重试聪明多了。大家都有啥重试策略?