Skip to content

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

fix(agent-runtime): treat insufficient balance wording as terminal

xpeng5278-web0b4ea82c9abe
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.md
index 5bd8090c1..eed70d03f 100644
--- a/docs/spec/03-runtime/08-error-codes.md
+++ b/docs/spec/03-runtime/08-error-codes.md
@@ -421,10 +421,11 @@ replayed by either retry budget, and a delegate reports it as
`SUBAGENT_CONTEXT_OVERFLOW`. Explicit billing/account exhaustion codes and
messages (including OpenCode Zen's `FreeUsageLimitError`, `insufficient_quota`,
`credit_balance_exhausted`,
-organization/project spend-limit codes, `insufficient credits`, and
-`requires more credits`, and Zhipu/Z.ai JSON error code `1308` for a reached plan
-usage limit) map to terminal `PROVIDER_ERROR` even when the HTTP
-status is 429, and HTTP 402 is terminal `PROVIDER_ERROR` even when the body
+organization/project spend-limit codes, `insufficient credits`,
+`insufficient balance` / `balance is insufficient` (DeepSeek, SiliconFlow,
+MiniMax), and `requires more credits`, and Zhipu/Z.ai JSON error code `1308`
+for a reached plan usage limit) map to terminal `PROVIDER_ERROR` even when the
+HTTP status is 429, and HTTP 402 is terminal `PROVIDER_ERROR` even when the body
omits those phrases. Throughput rate-limit wording, including
Bedrock's `Throttling error: Too many tokens, please wait before trying again.`,
stays `PROVIDER_RATE_LIMITED`. An unprefixed JSON error body that carries an
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index 2c89cc909..f8fbd880f 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -732,6 +732,11 @@ describe("classifyAgentError", () => {
"Insufficient credits",
"Payment Required",
'429: {"error":{"message":"Insufficient credits"}}',
+ // DeepSeek / SiliconFlow / MiniMax use "balance", not "credits".
+ "Insufficient Balance",
+ "Sorry, your account balance is insufficient",
+ "insufficient balance of the account",
+ '{"code":30001,"message":"Sorry, your account balance is insufficient","data":null}',
])("treats HTTP 402 and credit-exhaustion wording as terminal: %s", (message) => {
expect(classifyAgentError(new Error(message))).toMatchObject({
code: "PROVIDER_ERROR",
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index 610d8d21a..bce9e7718 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -49,13 +49,14 @@ const RATE_LIMIT_PATTERN =
/**
* Billing, spending, credit, and disk exhaustion are terminal, including
* OpenAI's `insufficient_quota` type, OpenRouter-style "insufficient credits"
- * / "requires more credits" / "Payment Required" wording, and explicit 429
- * error codes. Checked before the status table because those conditions do
- * not recover by backing off. Generic "quota exceeded" wording remains
- * eligible for rate-limit classification below. A bare "credits" 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.
+ * / "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
+ * "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.
*/
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;
@@ -69,7 +70,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\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/i;
/**
* Whether a provider message reports a context overflow.