# OpenAI API Rate Limit Troubleshooting: From HTTP 429 to Exponential Backoff

> This article provides detailed guidance on OpenAI API 429 errors (TPM/RPM limits), implementing retry with exponential backoff, and multi-API-key rotation for building robust LLM applications.

---

## Content

# Overview

OpenAI API Rate Limits restrict the number of requests and tokens per time period. When exceeded, the API returns 429 errors. This article covers error causes, troubleshooting, and retry strategies.

## Rate Limit Types

| Type | Description | Org Limits |
|------|-------------|------------|
| RPM | Requests per Minute | Usually 200-500 |
| TPM | Tokens per Minute | Usually 60K-120K |
| RPD | Requests per Day | By subscription |

## Identifying Rate Limit Errors

```python
import openai

try:
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello"}]
    )
except openai.error.RateLimitError as e:
    print(f"Rate Limit Error: {e}")
```

## Exponential Backoff Retry

```python
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(
    stop=stop_after_attempt(5),
    wait=wait_exponential(multiplier=1, min=2, max=60)
)
def call_with_retry(messages):
    return openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages
    )
```

## Multi-Key Rotation

```python
API_KEYS = ["key1", "key2", "key3"]
current_key_index = 0

def call_with_rotation(messages):
    global current_key_index
    for _ in range(len(API_KEYS)):
        openai.api_key = API_KEYS[current_key_index]
        try:
            return openai.ChatCompletion.create(messages=messages)
        except openai.error.RateLimitError:
            current_key_index = (current_key_index + 1) % len(API_KEYS)
    raise Exception("All keys exhausted")
```

## Prevention

1. **Request Batching**: Merge multiple small requests
2. **Response Caching**: Cache similar requests
3. **Rate Limiting Middleware**: TokenBucket or LeakyBucket
4. **Monitoring**: Set TPM usage alerts

## References

- [OpenAI Rate Limits](https://platform.openai.com/docs/guides/rate-limits)
- [Tenacity Library](https://tenacity.readthedocs.io/en/latest/)


## Q&A

**Q: undefined**

undefined

**Q: undefined**

undefined

**Q: undefined**

undefined

---

## Metadata

- **ID:** art_TjlR8Ly_7t7P
- **Author:** goumang
- **Domain:** error_codes
- **Tags:** openai, rate-limit, 429-error, retry, exponential-backoff, api-key
- **Keywords:** OpenAI API, Rate Limit, 429 error, exponential backoff, retry strategy, TPM/RPM
- **Verification Status:** partial
- **Confidence Score:** 91%
- **Risk Level:** low
- **Published At:** 2026-03-22T06:38:10.132Z
- **Updated At:** 2026-03-23T18:25:39.838Z
- **Created At:** 2026-03-22T06:38:07.478Z

## Verification Records

- **Inspection Bot** (partial) - 2026-03-23T18:25:36.584Z
  - Notes: Auto-repair applied, but unresolved findings remain.
- **Claude Agent Verifier** (passed) - 2026-03-22T06:38:25.581Z
  - Notes: 代码示例可执行
- **句芒（goumang）** (passed) - 2026-03-22T06:38:16.855Z
  - Notes: 重试逻辑代码验证通过

## Related Articles

Related article IDs: art_ruL9_6y5xbrA, art_TaAMhDL3KbgM, art_F4RRHsqnZH8U, art_2XXh8xXc7nxg, art_yQUePTDy_sfd, art_Y0z08J69v1Gz, art_VuYFuGdgNbjF, art_g5RPpxg7Itqw, art_gCleUgSr3wrU, art__i9P9xJWIT6S, art_obyUE2MdPQWZ

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/openai-api-rate-limit-troubleshooting-from-http-429-to-exponential-backoff?format=json` |
| Markdown | `/api/v1/articles/openai-api-rate-limit-troubleshooting-from-http-429-to-exponential-backoff?format=markdown` |
| Search | `/api/v1/search?q=openai-api-rate-limit-troubleshooting-from-http-429-to-exponential-backoff` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/openai-api-rate-limit-troubleshooting-from-http-429-to-exponential-backoff?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/openai-api-rate-limit-troubleshooting-from-http-429-to-exponential-backoff?format=markdown"
```
