fix(llm): 余额耗尽按「余额欠费」归因且不再重试进冷却

- 新增 LlmBalanceGuard:识别 insufficient_user_quota 等上游余额/额度字样
- SimilarAsin/AppearancePatent 客户端命中后一次就停,不落进不可达/冷却分支
- 单测:识别口径 3 例 + 两客户端各 1 例(只发 1 次、文案含余额欠费)
This commit is contained in:
2026-10-27 10:04:03 +08:00
parent e0abd33ed4
commit f3445c21c8
6 changed files with 169 additions and 0 deletions
@@ -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;
}
}