← Back to PRs

#23520: fix: trigger failover on Anthropic insufficient_quota (HTTP 400) (#23440)

by dissaozw open 2026-02-22 11:26 View on GitHub →
agents size: XS
Fixes #23440 ## Problem When Anthropic returns `insufficient_quota` with HTTP 400, `resolveFailoverReasonFromError()` mapped all HTTP 400 responses to `"format"` without checking the error message. This meant the error never triggered failover. ## Fix **4 files, 41 insertions:** 1. **`src/agents/pi-embedded-helpers/errors.ts`** — Added `"insufficient_quota"` to billing error patterns so message-based classification recognizes it. 2. **`src/agents/failover-error.ts`** — For HTTP 400, now checks the error message via `classifyFailoverReason()` before defaulting to `"format"`. This way `insufficient_quota` in the message body gets correctly classified as `"billing"`, triggering failover. 3. **`src/agents/failover-error.e2e.test.ts`** — Tests verifying HTTP 400 + `insufficient_quota` → `"billing"`, and plain HTTP 400 still → `"format"`. 4. **`src/agents/pi-embedded-helpers.isbillingerrormessage.e2e.test.ts`** — Tests for `isBillingErrorMessage` and `classifyFailoverReason` with Anthropic's `insufficient_quota` JSON payload. All 42 existing related tests pass. <!-- greptile_comment --> <h3>Greptile Summary</h3> Correctly fixes Anthropic's `insufficient_quota` error handling by checking HTTP 400 message content before defaulting to format error. The change adds `"insufficient_quota"` to billing error patterns and updates `resolveFailoverReasonFromError()` to inspect the message body for HTTP 400 responses, allowing quota errors to trigger failover as intended. - Added `"insufficient_quota"` string to billing error patterns in `errors.ts:593` - Modified HTTP 400 handling to check message via `classifyFailoverReason()` before defaulting to `"format"` in `failover-error.ts:169-178` - Comprehensive test coverage verifying both the fix (HTTP 400 + `insufficient_quota` → `"billing"`) and backward compatibility (plain HTTP 400 → `"format"`) <h3>Confidence Score: 5/5</h3> - Safe to merge with no concerns - The fix is minimal, targeted, and well-tested. It adds a single string pattern and refines HTTP 400 classification logic without breaking existing behavior. Tests verify both the new behavior (billing error detection) and backward compatibility (plain 400s remain format errors). The change follows the existing error classification pattern and is consistent with how other HTTP status codes are handled throughout the codebase. - No files require special attention <sub>Last reviewed commit: e396a0d</sub> <!-- greptile_other_comments_section --> <sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub> <!-- /greptile_comment -->

Most Similar PRs