Skip to content

fix: eager fallback on billing 402#23707

Open
luoxiao6645 wants to merge 1 commit into
NousResearch:mainfrom
luoxiao6645:fix/23138-billing-fallback
Open

fix: eager fallback on billing 402#23707
luoxiao6645 wants to merge 1 commit into
NousResearch:mainfrom
luoxiao6645:fix/23138-billing-fallback

Conversation

@luoxiao6645

Copy link
Copy Markdown

Summary

Fix eager fallback when the primary provider returns HTTP 402 / FailoverReason.billing.

The real bug was not in cron wiring. cron/scheduler.py already passes both
fallback_model and credential_pool into AIAgent. The failure was in the
eager-fallback guard: billing exhaustion was grouped with rate limits, then
_pool_may_recover_from_rate_limit() could still block fallback while waiting
for credential-pool rotation.

That is wrong for billing/account exhaustion. Rotating credentials should not
delay fallback for FailoverReason.billing.

Fixes #23138.

Changes

  • teach _pool_may_recover_from_rate_limit() about the failover reason
  • force it to return False for FailoverReason.billing
  • pass classified.reason into the eager-fallback guard in run_conversation()
  • add regression tests for:
    • helper-level billing/pool behavior
    • eager fallback with a multi-entry credential pool
    • run-level HTTP 402 -> immediate fallback

Why this fixes the issue

Before this change, a primary 402 Insufficient Balance could be treated as
"maybe credential rotation can recover", which prevented eager fallback from
activating even though fallback_providers was configured.

After this change, 402 -> FailoverReason.billing skips pool-rotation gating
and switches to the fallback provider immediately.

Validation

Passed:

  • venv\Scripts\python.exe -m pytest tests/run_agent/test_run_agent.py -k billing_error_triggers_eager_fallback_before_retry_budget -q
  • venv\Scripts\python.exe -m pytest tests/run_agent/test_provider_fallback.py tests/agent/test_credential_pool_routing.py tests/agent/test_error_classifier.py -q

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder duplicate This issue or pull request already exists labels May 11, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #23323 — same fix: bypass credential-pool recovery guard for HTTP 402 (billing) errors in eager fallback path. Both fix #23138.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Eager fallback on HTTP 402 (FailoverReason.billing) does not activate; cron job loops on dead primary until output-length crash

2 participants