Rate Limits, Retries, and Idempotency
Build reliable API clients — rate limit headers, exponential backoff, idempotency keys.
What You Will Learn
- Handle rate limits gracefully.
- Implement exponential backoff.
- Use idempotency keys.
- Build reliable clients.
- Test reliability.
Why This Matters
Production API clients must handle rate limits, transient failures, and retries. Without these, your automation breaks at the worst time.
Concept Explained
GitHub API rate limits: 5000 req/hour for authenticated. Copilot endpoints may have lower limits. Handle 403/429 with Retry-After header. Use idempotency keys for write operations.
How It Works
Check rate limit headers (X-RateLimit-Remaining). On 403/429, wait per Retry-After, retry with exponential backoff. Use idempotency key for POST/PUT to prevent duplicates.
Step-by-Step Tutorial
1. Check rate limit headers
X-RateLimit-Remaining, X-RateLimit-Reset.2. On 403/429
Wait per Retry-After header. Retry with exponential backoff (1s, 2s, 4s, 8s, max 60s).3. Max retries
5. After that, fail loudly.4. Idempotency keys
For POST/PUT, send Idempotency-Key header. GitHub returns same response for same key.5. Test reliability
Simulate rate limits; verify client handles.Real-World Example
A team's CI Copilot dashboard broke every Monday morning — peak GitHub API traffic. Added rate limit handling with Retry-After and exponential backoff. Dashboard became reliable. Lesson: production API clients must handle rate limits.
Example Prompts / Commands / Code
import requests
import time
import uuid
class ReliableCopilotClient:
def __init__(self, org, token):
self.org = org
self.token = token
self.base = 'https://api.github.com'
self.max_retries = 5
def _headers(self, idempotency_key=None):
h = {
'Authorization': f'Bearer {self.token}',
'Accept': 'application/vnd.github+json'
}
if idempotency_key:
h['Idempotency-Key'] = idempotency_key
return h
def _request(self, method, url, **kwargs):
for attempt in range(self.max_retries):
r = requests.request(method, f'{self.base}{url}',
headers=self._headers(kwargs.pop('idempotency_key')),
**kwargs)
if r.status_code in (403, 429):
retry_after = int(r.headers.get('Retry-After', 60))
wait = min(retry_after, 2 ** attempt)
print(f'Rate limited. Waiting {wait}s...')
time.sleep(wait)
continue
if r.status_code >= 500:
wait = 2 ** attempt
print(f'Server error. Retrying in {wait}s...')
time.sleep(wait)
continue
return r
raise RuntimeError(f'Max retries ({self.max_retries}) exceeded')
def add_seat(self, login):
# Idempotency key prevents duplicate seat assignments
key = str(uuid.uuid4())
return self._request('POST', f'/orgs/{self.org}/copilot/seats',
json={'selected_usernames': [login]},
idempotency_key=key)
Common Mistakes
- No retry logic — single failure breaks automation.
- No idempotency key — duplicates on retry.
- Not checking rate limit headers — surprise 403s.
- Infinite retries — wedges the system.
Best Practices
- Check X-RateLimit-Remaining; slow down when low.
- On 403/429: wait per Retry-After, retry with backoff.
- Max 5 retries; fail loudly after.
- Use Idempotency-Key for POST/PUT.
- Test reliability with simulated failures.
Troubleshooting
| Problem | How to Fix |
|---|---|
| Still getting 403 | Reduce polling frequency. Or batch requests. |
| Duplicates on retry | Add Idempotency-Key header. |
Practical Exercise
Your Turn
Build a reliable Copilot API client with rate limit handling and retries. Test by simulating 429 responses.
Professional Challenge
Add idempotency to all write operations. Test by retrying with same key — verify no duplicates.
Key Takeaways
- Handle rate limits: check X-RateLimit-Remaining.
- On 403/429: Retry-After + exponential backoff.
- Max 5 retries; fail loudly after.
- Idempotency-Key for POST/PUT prevents duplicates.
- Test reliability with simulated failures.
Frequently Asked Questions
What's the rate limit?
Do Copilot endpoints have separate limits?
Further Reading
Official References
SEO Metadata
SEO title: Rate Limits, Retries, and Idempotency
Meta description: Build reliable API clients — rate limit headers, exponential backoff, idempotency keys.
Primary keyword: rate limits, retries, and idempotency
Secondary keywords: rate limits, retries, and idempotency
Search intent: Informational
URL slug: /copilot-api-rate-limits-retries-idempotency
Categories: AI Tools, GitHub Copilot
Tags: GitHub Copilot, Professional, REST API, Reliability, Rate Limits, IMCSEIAN, Tutorial, IMCSEIAN
Featured image concept: IMCSEIAN lesson card for Rate Limits, Retries, and Idempotency
Comments
Comments
Post a Comment