fix(llm): add exponential backoff retry for API calls#38
Open
fryrice2000 wants to merge 2 commits intoNarcooo:masterfrom
Open
fix(llm): add exponential backoff retry for API calls#38fryrice2000 wants to merge 2 commits intoNarcooo:masterfrom
fryrice2000 wants to merge 2 commits intoNarcooo:masterfrom
Conversation
chatCompletion and chatWithTools now auto-retry on transient errors (429/502/503/ECONNRESET/ETIMEDOUT) with exponential backoff + jitter. Client errors (401/403/400) fail immediately without retry. - New: packages/core/src/llm/retry.ts (withRetry utility) - New: packages/core/src/__tests__/retry.test.ts (15 test cases) - Modified: packages/core/src/llm/provider.ts (wrap both public functions)
3b894b1 to
f126843
Compare
f126843 to
615e33b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复内容
LLM 调用层(chatCompletion / chatWithTools)无重试机制,
遇到 429/502/ECONNRESET 等瞬时 API 错误时会导致管线直接崩溃。
修改方案
测试
✅ 已覆盖的场景
错误类型 示例 行为
限频 429 Rate Limit 重试 ✅
网关错误 502 Bad Gateway 重试 ✅
服务不可用 503 Service Unavailable 重试 ✅
连接重置 ECONNRESET 重试 ✅
连接超时 ETIMEDOUT 重试 ✅
DNS 解析失败 ENOTFOUND 重试 ✅
Socket 断开 socket hang up 重试 ✅
fetch 层失败 TypeError: fetch failed 重试 ✅
中文包装的 429 "请求过多" (wrapLLMError 输出) 重试 ✅
认证失败 401 Unauthorized 不重试 ✅
权限拒绝 403 Forbidden 不重试 ✅
请求格式错误 400 Bad Request 不重试 ✅
无效 API Key invalid_api_key 不重试 ✅