Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): treat Anthropic credit-balance and DashScope Arrearage wording as terminal

xpeng5278-webc1970f15bf61
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index 70155677e..3cc0c6637 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -230,7 +230,9 @@ multiply the budget. The captured response status is applied before classifying
the provider message in both phases, so a generic 429 body still enters the
429 budget while known non-retryable classifications remain terminal. Explicit
billing/account exhaustion (including `insufficient_quota`, spend-limit
-codes, and Zhipu/Z.ai usage-limit code `1308`) is terminal `PROVIDER_ERROR` even with HTTP 429. The main session and
+codes, Zhipu/Z.ai usage-limit code `1308`, Anthropic "credit balance is too
+low", and DashScope `Arrearage` / "access denied … account is in good
+standing") is terminal `PROVIDER_ERROR` even with HTTP 429. The main session and
builtin subagents use the same controller and policy.
429 retries are silent at the transcript lifecycle: no intermediate assistant
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index 2db7f4bd3..8a2ef513c 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -765,6 +765,73 @@ describe("classifyAgentError", () => {
});
});
+ // Anthropic bills an empty wallet as HTTP 400 with this sentence. DashScope
+ // uses code Arrearage, sometimes with only the "good standing" sentence
+ // left. pi-ai often drops the status, and a gateway may relabel the same
+ // text 429. Backing off cannot add credits.
+ it.each([
+ "Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing to upgrade or purchase credits.",
+ '{"code":"Arrearage","message":"Access denied, please make sure your account is in good standing.","type":"Arrearage"}',
+ "Arrearage",
+ "Access denied, please make sure your account is in good standing.",
+ ])(
+ "treats Anthropic credit-balance and DashScope Arrearage wording as terminal: %s",
+ (message) => {
+ expect(classifyAgentError(message)).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: false,
+ });
+ expect(classifyAgentError(new Error(message))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: false,
+ });
+ expect(classifyAgentError(Object.assign(new Error(message), { status: 429 }))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: false,
+ });
+ expect(classifyAgentError(Object.assign(new Error(message), { status: 400 }))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: false,
+ });
+ },
+ );
+
+ it("treats a structured Arrearage code as terminal even when the message omits the word", () => {
+ expect(classifyAgentError(Object.assign(
+ new Error("Access denied"),
+ { code: "Arrearage" },
+ ))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: false,
+ });
+ });
+
+ it("does not treat nearby balance or access wording as account exhaustion", () => {
+ for (const message of [
+ "insufficient credentials",
+ "balance",
+ "credits",
+ "Access denied",
+ "Your account is in good standing.",
+ ]) {
+ expect(classifyAgentError(new Error(message))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: true,
+ });
+ }
+ expect(classifyAgentError(Object.assign(new Error("Access denied"), { status: 429 }))).toMatchObject({
+ code: "PROVIDER_RATE_LIMITED",
+ retriable: true,
+ });
+ expect(classifyAgentError(Object.assign(
+ new Error("Your account is in good standing."),
+ { status: 429 },
+ ))).toMatchObject({
+ code: "PROVIDER_RATE_LIMITED",
+ retriable: true,
+ });
+ });
+
it("treats a structured HTTP 402 as a terminal provider error", () => {
expect(classifyAgentError(Object.assign(
new Error("Payment Required"),
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index afe171c46..1e040ce09 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -51,15 +51,19 @@ const RATE_LIMIT_PATTERN =
* OpenAI's `insufficient_quota` type, OpenRouter-style "insufficient credits"
* / "requires more credits" / "Payment Required" wording, DeepSeek /
* SiliconFlow / MiniMax "Insufficient Balance" / "account balance is
- * insufficient", and explicit 429 error codes. Checked before the status
- * table because those conditions do not recover by backing off. Generic
+ * insufficient", and explicit 429 error codes. Anthropic's "credit balance
+ * is too low" and DashScope's `Arrearage` code, or "Access denied" paired
+ * with "account is in good standing", are the same terminal exhaustion.
+ * Checked before the status table because those conditions do not recover
+ * by backing off, including when a gateway labels them HTTP 429. Generic
* "quota exceeded" wording remains eligible for rate-limit classification
- * below. A bare "credits" or "balance" is not exhaustion, and
- * "insufficient credentials" must not match it either. HTTP 402 is
- * terminal in the status table even when the body omits these phrases.
+ * below. A bare "credits", "balance", "Access denied", or "account is in
+ * good standing" is not exhaustion, and "insufficient credentials" must
+ * not match it either. HTTP 402 is terminal in the status table even when
+ * the body omits these phrases.
*/
const EXHAUSTED_QUOTA_CODE_PATTERN =
- /\b(?:insufficient_quota|credit_balance_exhausted|organization_spend_limit_exceeded|project_spend_limit_exceeded|organization_usage_limit_exceeded|FreeUsageLimitError)\b/i;
+ /\b(?:insufficient_quota|credit_balance_exhausted|organization_spend_limit_exceeded|project_spend_limit_exceeded|organization_usage_limit_exceeded|FreeUsageLimitError|Arrearage)\b/i;
/**
* Zhipu/Z.ai (open.bigmodel.cn) error code 1308: the plan usage limit is
@@ -70,7 +74,7 @@ const EXHAUSTED_QUOTA_CODE_PATTERN =
const ZHIPU_USAGE_LIMIT_PATTERN = /"code"\s*:\s*"?1308(?![0-9])/;
const EXHAUSTED_QUOTA_MESSAGE_PATTERN =
- /\b(?:billing|spending|credit|disk)\s+quotas?\b|\b(?:organization|project)\s+spend\s+limit\s+(?:reached|exceeded)\b|\bquota\b.{0,100}\b(?:plan|billing)\b|\binsufficient\s+(?:credits|balance)\b|\bbalance\s+is\s+insufficient\b|\brequires\s+more\s+credits\b|\bpayment\s+required\b/i;
+ /\b(?:billing|spending|credit|disk)\s+quotas?\b|\b(?:organization|project)\s+spend\s+limit\s+(?:reached|exceeded)\b|\bquota\b.{0,100}\b(?:plan|billing)\b|\binsufficient\s+(?:credits|balance)\b|\bbalance\s+is\s+insufficient\b|\brequires\s+more\s+credits\b|\bpayment\s+required\b|\bcredit\s+balance\s+is\s+too\s+low\b|\bArrearage\b|\baccess\s+denied\b.{0,80}\baccount\s+is\s+in\s+good\s+standing\b/i;
/**
* Whether a provider message reports a context overflow.