Insufficient USDC Balance Mid-Run: What the API Returns, and How to Detect It Before Your Batch Dies Halfway
Learn how to distinguish a drained USDC balance error from a vendor-side quota error when using an LLM API aggregator, and implement a pre-flight balance check to prevent long jobs from failing halfway through.
Why a Drained Balance Is Different
When you call models through an aggregator that charges per request against a prepaid USDC balance, running out of funds produces a distinct error. Unlike a vendor-side quota error, which comes from the model provider (e.g., rate limits or temporary unavailability), a balance error originates from the aggregator itself. Recognizing the difference is key to handling failures correctly.
The Error Shape: Aggregator vs. Vendor
Aggregator balance error typically includes an HTTP status like 402 Payment Required (or a custom code) and a JSON body with fields such as error.type = "insufficient_balance" and a human-readable message like "Insufficient USDC balance. Please top up." The request never reaches the model provider, so no tokens are consumed.
Vendor-side quota error often returns 429 Too Many Requests or 403 Forbidden, with messages referencing rate limits, quotas, or temporary unavailability from the model vendor. These errors may include retry-after headers or vendor-specific codes.
Key differences:
- Source: Balance errors come from the aggregator; quota errors come from the vendor.
- HTTP status: Balance errors frequently use 402; quota errors use 429 or other vendor-specific codes.
- Message content: Look for explicit mentions of "balance", "top up", or "USDC" in balance errors.
- Retry behavior: Retrying a balance error without topping up will never succeed; retrying a quota error might succeed after a delay.
Detecting a Balance Error in Your Code
Check the HTTP status and response body. For example, in Python:
import requests
resp = requests.post(url, headers=headers, json=payload)
if resp.status_code == 402 or "insufficient_balance" in resp.text:
raise RuntimeError("USDC balance depleted. Top up to continue.")
Always log the full error response—it helps distinguish aggregator errors from vendor ones during debugging.
Pre-flight Balance Check Pattern
For long-running batch jobs, checking your balance before starting prevents mid-run failures. Use a lightweight endpoint (e.g., GET /v1/balance or a dedicated /account/balance route) that returns your current USDC balance. Compare it to an estimated cost per request (you pay official price × 1.3) multiplied by the number of requests, with a safety margin.
Example pattern:
- Fetch balance before the batch.
- Estimate total cost: For each request, compute the official price (if known) times 1.3, or use a conservative upper bound.
- Compare: If balance < estimated cost × 1.5 (buffer), abort and top up.
- Monitor during the run: Optionally, re-check balance periodically (e.g., every N requests) or handle 402 errors gracefully by pausing and alerting.
Handling Mid-run Depletion Gracefully
If a balance error occurs mid-run, your job should:
- Stop issuing new requests.
- Persist progress so you can resume after topping up.
- Distinguish this error from vendor quota errors to avoid unnecessary retries.
Top-up and Resume
Since the aggregator accepts USDC on Base without KYC, topping up is permissionless and fast. After topping up, resume your batch from the last successful request. Your API key remains valid, and no re-authentication is needed.
Summary
- Balance errors use distinct HTTP statuses and messages compared to vendor quota errors.
- Implement a pre-flight balance check for long jobs to avoid partial failures.
- Handle 402 errors by pausing and topping up, not by retrying blindly.