fix(llm): 余额耗尽按「余额欠费」归因且不再重试进冷却
- 新增 LlmBalanceGuard:识别 insufficient_user_quota 等上游余额/额度字样 - SimilarAsin/AppearancePatent 客户端命中后一次就停,不落进不可达/冷却分支 - 单测:识别口径 3 例 + 两客户端各 1 例(只发 1 次、文案含余额欠费)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.nanri.aiimage.common.retry;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 上游 LLM 账户余额/额度耗尽的识别与归因(与连接层不可达分账)。
|
||||
*
|
||||
* <p>2026-10-27 现网实测:{@code ai.t8star.org} 返回 HTTP 403
|
||||
* {@code {"error":{"code":"insufficient_user_quota","message":"预扣费额度失败, 用户[151246]剩余额度: ฿0.029168…"}}}。
|
||||
* 这类失败连接已经建立、上游明确回了余额欠费:既不是本行数据问题,也不是"服务不可达"。
|
||||
* 不单独识别就会被通用重试阶梯连打三次,用户看到的仍是与真因无关的不可达/冷却文案。
|
||||
*
|
||||
* <p>识别只认余额/额度专属字样(ASCII 锚点优先,中文签名在容器日志里变了编码也还能命中);
|
||||
* 429 限流与 5xx 抖动不在此列,仍走原重试阶梯。
|
||||
*/
|
||||
public final class LlmBalanceGuard {
|
||||
|
||||
/** 行级/任务级统一归因文案:说清是上游账户欠费,不是这行数据的问题。 */
|
||||
public static final String REASON_LLM_BALANCE_DUE = "LLM 服务余额欠费(上游账户额度不足,非本行数据问题)";
|
||||
|
||||
private static final String[] MARKERS = {
|
||||
"insufficient_user_quota",
|
||||
"insufficient_quota",
|
||||
"insufficient balance",
|
||||
"预扣费额度失败",
|
||||
"额度不足",
|
||||
"余额不足",
|
||||
"余额欠费",
|
||||
};
|
||||
|
||||
private LlmBalanceGuard() {
|
||||
}
|
||||
|
||||
/** 顺着 cause 链检查异常消息是否命中余额/额度耗尽字样。 */
|
||||
public static boolean isQuotaExhausted(Throwable error) {
|
||||
for (Throwable current = error; current != null && current != current.getCause(); current = current.getCause()) {
|
||||
String message = current.getMessage();
|
||||
if (message == null || message.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String lower = message.toLowerCase(Locale.ROOT);
|
||||
for (String marker : MARKERS) {
|
||||
if (lower.contains(marker.toLowerCase(Locale.ROOT))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+9
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.LlmBalanceGuard;
|
||||
import com.nanri.aiimage.common.retry.LlmKeyGate;
|
||||
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
@@ -691,6 +692,14 @@ public class AppearancePatentLlmClient {
|
||||
if (LlmKeyGate.isRateLimited(ex)) {
|
||||
LlmKeyGate.penalizeRateLimited(apiKey, 2_000L);
|
||||
}
|
||||
if (LlmBalanceGuard.isQuotaExhausted(ex)) {
|
||||
// 余额欠费不是连接层故障,重试救不回来:一次就停,按欠费归因,不落进冷却分支
|
||||
unreachableCooldown.recordConnected(host);
|
||||
log.warn("[appearance-patent] LLM_BALANCE_DUE 上游账户余额不足,停止重试 attempt={} model={} err={}",
|
||||
attempt, model, failureMessage(ex));
|
||||
throw new IllegalStateException(
|
||||
LlmBalanceGuard.REASON_LLM_BALANCE_DUE + ";lastErr=" + failureMessage(ex), ex);
|
||||
}
|
||||
lastFailure = ex;
|
||||
if (UnreachableCooldown.isConnectFailure(ex)) {
|
||||
boolean entered = unreachableCooldown.recordConnectFailure(host, cooldownMillis);
|
||||
|
||||
+9
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.similarasin.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.LlmBalanceGuard;
|
||||
import com.nanri.aiimage.common.retry.LlmKeyGate;
|
||||
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
@@ -119,6 +120,14 @@ public class SimilarAsinLlmClient {
|
||||
// 429 时把同 key 的下一次许可整体后移,避免所有调用一起再撞
|
||||
LlmKeyGate.penalizeRateLimited(resolvedKey, 2_000L);
|
||||
}
|
||||
if (LlmBalanceGuard.isQuotaExhausted(ex)) {
|
||||
// 余额欠费不是连接层故障,重试救不回来:一次就停,按欠费归因,不落进冷却分支
|
||||
unreachableCooldown.recordConnected(host);
|
||||
log.warn("[similar-asin][llm] LLM_BALANCE_DUE 上游账户余额不足,停止重试 attempt={} model={} err={}",
|
||||
attempt, model, failureMessage(ex));
|
||||
throw new IllegalStateException(
|
||||
LlmBalanceGuard.REASON_LLM_BALANCE_DUE + ";lastErr=" + failureMessage(ex), ex);
|
||||
}
|
||||
lastFailure = ex;
|
||||
if (UnreachableCooldown.isConnectFailure(ex)) {
|
||||
connectFailures++;
|
||||
|
||||
Reference in New Issue
Block a user