# HTTP 429 `rate_limit_exceeded` — API Store Hub

> VI: HTTP 429 xảy ra khi tần suất gửi request (RPM), lưu lượng token/phút (TPM) hoặc số kết nối đồng thời vượt ngưỡng cho phép của API key hoặc kênh upstream.
> EN: HTTP 429 occurs when requests per minute (RPM), tokens per minute (TPM), or concurrent in-flight streams exceed the key or upstream lane threshold.

- Canonical VI: https://apistorehub.com/vi/loi-api/429-rate-limit-exceeded
- Canonical EN: https://apistorehub.com/en/loi-api/429-rate-limit-exceeded
- Retry Policy: Retry up to 3 times using exponential backoff + jitter while honoring `Retry-After`, switching to a fallback model after attempt 2.

## Root Causes
- Firing unbounded concurrent batch requests without a client-side semaphore.
- Agent loops or webhook retries hammering the endpoint without backoff.
- Key-level RPM cap set lower than peak production traffic.

## Step-by-Step Resolution
1. Honor the `Retry-After` response header; otherwise apply exponential backoff with full jitter.
2. Cap concurrent in-flight calls using a Semaphore or message queue.
3. Fail over to a secondary model family after 2 retries to maintain low latency.
4. Review per-key RPM and spend caps inside Dashboard -> Keys.

## 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","messages":[{"role":"user","content":"ping"}]}'
```

## Production Fix (Python)
```
import random, time
from openai import OpenAI, RateLimitError

def call_with_backoff(client: OpenAI, model: str, fallback: str, messages: list):
    for attempt in range(4):
        try:
            target = model if attempt < 2 else fallback
            return client.chat.completions.create(model=target, messages=messages)
        except RateLimitError:
            if attempt == 3:
                raise
            time.sleep(min(20.0, (2 ** attempt) * 0.8 + random.uniform(0.1, 0.6)))
```

## FAQ
- **Does an HTTP 429 error deduct ST credits from my wallet?**: No. Failed 429 responses are not billed to your ST balance.
- **Does API Store Hub reduce 429 bursts compared to a single direct vendor key?**: Yes. Multi-lane upstream pooling and instant cross-family model fallback absorb traffic spikes.
- **What concurrency limit is recommended for batch jobs?**: Start at 8-16 concurrent workers and scale up while monitoring latency and error rates.
