# HTTP 402 `insufficient_quota` — API Store Hub

> VI: HTTP 402 xảy ra khi số dư Credit ST khả dụng nhỏ hơn mức giữ chỗ tối thiểu của lượt gọi, hoặc API key đã chạm hạn mức chi tiêu (`spend_limit`) cài sẵn.
> EN: HTTP 402 indicates that available ST wallet balance is below the request's atomic pre-hold estimate or the API key reached its configured spend cap.

- Canonical VI: https://apistorehub.com/vi/loi-api/402-insufficient-balance-quota
- Canonical EN: https://apistorehub.com/en/loi-api/402-insufficient-balance-quota
- Retry Policy: Do not retry HTTP 402 in a loop; trigger an operational low-balance alert immediately.

## Root Causes
- Wallet balance is below the pre-hold estimate for a high `max_tokens` or flagship model call.
- Multiple concurrent requests hold overlapping pre-holds simultaneously.
- API key reached its configured spend limit.

## Step-by-Step Resolution
1. Inspect wallet balance and per-key budget caps in Dashboard -> Wallet & Keys.
2. Top up ST credit (1 VND = 1 ST, automated 24/7 confirmation).
3. Pass realistic `max_tokens` values (e.g. 1024-4096) to shrink per-request pre-hold reservations.
4. Configure low-balance alerts and route simple tasks to economical Flash models.

## Diagnostic cURL
```bash
curl -i https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"deepseek-v4-flash","max_tokens":128,"messages":[{"role":"user","content":"check quota"}]}'
```

## Production Fix (TypeScript)
```
export async function guardQuotaCall<T>(fn: () => Promise<T>, alertLowBalance: () => Promise<void>): Promise<T> {
  try {
    return await fn();
  } catch (err: any) {
    if (err?.status === 402) {
      await alertLowBalance();
    }
    throw err;
  }
}
```

## FAQ
- **Is the full pre-hold permanently deducted?**: No. Pre-hold reserves an upper bound during execution and immediately releases the unused remainder upon settlement.
- **Why can 402 happen when the wallet still shows positive balance?**: Overlapping concurrent pre-holds or huge `max_tokens` estimates can exceed available balance. Lower `max_tokens` or keep a buffer.
- **Does ST wallet credit expire monthly?**: Standard prepaid ST wallet balance does not expire monthly.
