#5967·nanobot

Fallback models are skipped when a provider reports "insufficient credits" (HTTP 400)

Author: CarmeloCamposCreated Sep 29, 2026Updated Sep 29, 2026

Bug Description

When an OpenAI-compatible provider/gateway reports exhausted credits with the wording "insufficient credits", configured fallbacks are silently skipped** and the raw provider error is returned to the user. The agent appears to "stop working" even though agents.defaults.fallbackModels` is configured and valid.

Two independent gaps combine to cause this:

  1. LLMProvider._NON_RETRYABLE_429_TEXT_MARKERS (and the matching structured token set _NON_RETRYABLE_429_ERROR_TOKENS) recognize insufficient_quota, insufficient balance, out of credits, etc. — but not insufficient credits.

  2. Because that marker is missing, LLMProvider.is_arrearage_response() returns False, so _should_fallback() falls through to the blanket if status in {400, 404, 422}: return False and returns False.

The result is logged as:

WARNING | Primary model 'x' failed with non-fallbackable error: [400]: You have insufficient credits to make this request. Please purchase more credits to continue using the service.

Steps to Reproduce

Configure a fallback chain against a gateway that returns exhausted credits as HTTP 400:

json
{
  "agents": {
    "defaults": {
      "model": "some-model",
      "fallbackModels": ["backup-a", "backup-b"]
    }
  }
}

Primary provider returns:

HTTP/1.1 400 Bad Request
{"error":{"message":"You have insufficient credits to make this request. Please purchase more credits to continue using the service.","type":"invalid_request_error","code":"BAD_REQUEST"}}

Then send any message.

Expected Behavior

The arrearage/quota semantics should be recognized, so the configured fallback model is tried — exactly like the existing insufficient_quota and payment required cases already do.

Actual Behavior

_should_fallback() returns False, no fallback provider is instantiated, and the raw 400 error text is delivered to the user as the assistant reply. The session effectively stops working.

Root Cause

is_arrearage_response() is the first check inside _should_fallback():

python
@staticmethod
def _should_fallback(response: LLMResponse) -> bool:
    if LLMProvider.is_arrearage_response(response):
        return True
    ...
    if status in {400, 404, 422}:
        return False

so the HTTP-400 short-circuit is only reached when arrearage detection already failed. Arrearage detection is text/token based, and its marker list does not cover the insufficient credits phrasing:

python
_NON_RETRYABLE_429_TEXT_MARKERS = (
    "insufficient_quota",
    "insufficient quota",
    ...
    "insufficient balance",
    "insufficient_balance",
    "credit balance too low",
    "payment required",
    "out of credits",
    "out of quota",
    "exceeded your current quota",
)

Note "out of credits" is present but "insufficient credits" is not, which makes the omission easy to miss.

Suggested Fix

Add the missing phrasing to both the text-marker tuple and the structured-token frozenset. This is a data-only change; no control flow needs to move:

diff
 _NON_RETRYABLE_429_ERROR_TOKENS = frozenset({
     ...
     "insufficient_balance",
+    "insufficient_credits",
     "credit_balance_too_low",
 )

 _NON_RETRYABLE_429_TEXT_MARKERS = (
     ...
     "insufficient balance",
     "insufficient_balance",
+    "insufficient credits",
+    "insufficient_credits",
     "credit balance too low",
 )

I have a PR ready with this fix plus regression tests.

Impact

Affects any deployment behind an OpenAI-compatible gateway that phrases exhausted credit as insufficient credits (e.g. CLIProxyAPI / cli-proxy-api, some Azure/OpenRouter-compatible aggregators). Given that these gateways commonly rotate a shared credential pool across many models, a single exhausted credential can leave a whole model in a permanent arrearage state — so the fallback chain is exactly the mechanism users rely on, and it silently does nothing.

Environment

  • nanobot 0.3.5 (main @ 3cf6ed6)
  • Channel: Telegram
  • Provider: OpenAI-compatible gateway (providers.custom) returning 400 + insufficient credits