Fatal API errors (credit exhaustion, invalid key) silently retry instead of aborting the build
Problem
When the Claude API returns a fatal, non-retryable error — such as "Credit balance is too low" or an invalid API key — the build does not stop. Instead it burns through multiple retry layers, fails with a generic and misleading error (e.g. "Product manager failed to produce a valid PRD"), and only aborts after exhausting all retry budgets.
Observed in run run_20260409_102707_rfkaa60a: four run_product_manager attempts completing in ~6–8 seconds each (far too fast for real work), all failing silently. Root cause buried in stderr:
Credit balance is too low
Root Cause
Three retry layers, none of which know the API is fatally unavailable:
Layer 1 — Harness schema retries (agentfield/harness/_runner.py)
When the claude CLI exits with code 1, ClaudeCodeProvider catches the exception and returns RawResult(is_error=True) but leaves failure_type = FailureType.NONE (the default). NONE is in the _retryable set {CRASH, NO_OUTPUT, NONE}, so _handle_schema_with_retry fires up to schema_max_retries (default 2) more attempts against a dead API key.
Layer 2 — AgentField SDK execution retries
The SDK retries failed reasoner executions. A RuntimeError("Product manager failed to produce a valid PRD") looks like a transient schema failure, so the SDK retries the full plan execution.
Layer 3 — Generic error message obscures root cause
Because no fatal-error check existed before the if result.parsed is None guard, the credit error is swallowed and replaced with "Product manager failed to produce a valid PRD", making diagnosis very difficult.
Fix (applied in this commit)
Added swe_af/execution/fatal_error.py with:
FatalHarnessError(RuntimeError)— a distinct sentinel type for non-retryable API failurescheck_fatal_harness_error(result)— inspectsresult.error_messagefor fatal patterns and raises immediately
Patterns detected: credit balance is too low, invalid api key, invalid x-api-key, your api key is not valid, authentication failed, account has been disabled / is disabled
Updated call-sites:
swe_af/reasoners/pipeline.py—run_product_manager,run_architect,run_tech_lead,run_sprint_plannerswe_af/reasoners/execution_agents.py—run_git_initswe_af/execution/envelope.py—unwrap_call_result()now raisesFatalHarnessErrorinstead ofRuntimeErrorwhen the envelope error message matches a fatal pattern
Remaining Gap (requires SDK fix)
The harness schema retries in agentfield/harness/_runner.py still fire on fatal errors because ClaudeCodeProvider does not set failure_type = FailureType.API_ERROR when stderr contains billing/auth errors. The fix needed in the SDK:
# agentfield/harness/providers/claude.py — in the except block:
stderr_output = "\n".join(stderr_lines).strip()
fatal_keywords = {"credit balance", "invalid api key", "unauthorized", "account has been disabled"}
is_fatal = any(k in stderr_output.lower() or k in str(exc).lower() for k in fatal_keywords)
return RawResult(
...,
failure_type=FailureType.API_ERROR if is_fatal else FailureType.NONE,
)
And _handle_schema_with_retry should treat FailureType.API_ERROR as non-retryable and return immediately.
Impact
Before: build with exhausted credits retries 4+ times over ~47 seconds, fails with misleading error, no indication of root cause.
After: build fails on first attempt with FatalHarnessError: Fatal API error (non-retryable): Credit balance is too low.