RateLimitError
anthropic.RateLimitError (HTTP 429)
Stack trace
anthropic.RateLimitError: Error 429: Rate limit exceeded - Too many requests per minute. Please slow down your request rate.
Why it happens
Anthropic enforces strict rate limits on API requests to prevent abuse and ensure fair usage. When your application sends more requests than allowed per minute, the API responds with a 429 RateLimitError to throttle traffic.
Detection
Monitor API responses for HTTP 429 status codes and log occurrences. Implement metrics or alerts on repeated 429 errors to catch rate limit breaches early.
Causes & fixes
Sending requests faster than Anthropic's allowed requests per minute quota.
Implement client-side rate limiting or exponential backoff to reduce request frequency below the allowed threshold.
Multiple concurrent processes or threads making API calls without coordination.
Centralize request dispatching or use a shared rate limiter across all processes to control total request rate.
Using an API key with a low rate limit tier and exceeding its quota.
Upgrade your Anthropic API plan to a higher tier with increased rate limits or optimize request usage.
Code: broken vs fixed
from anthropic import Anthropic
client = Anthropic(api_key="sk-...")
response = client.completions.create(model="claude-2", prompt="Hello") # triggers RateLimitError if too many calls import os
from anthropic import Anthropic, RateLimitError
import time
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
try:
response = client.completions.create(model="claude-2", prompt="Hello") # fixed: uses env var and handles rate limit
except RateLimitError:
print("Rate limit hit, retrying after delay...")
time.sleep(10) # wait before retry
response = client.completions.create(model="claude-2", prompt="Hello")
print(response) Workaround
Wrap API calls in try/except RateLimitError, then pause for a few seconds before retrying to avoid immediate repeated failures.
Prevention
Implement a client-side rate limiter or queue requests to ensure calls do not exceed Anthropic's documented requests per minute limits, and monitor usage metrics.