xtekky/gpt4free · error · RateLimitError
Response {response.status_code}: {message}
Error message
Response {response.status_code}: {message} What it means
Sync twin of the async helper: raise_for_status inspects a requests-style Response (no 'status' attribute, has status_code) and raises RateLimitError for HTTP 429 or 402. The raw response text is used as the message without the JSON unwrapping the async path does.
Source
Thrown at g4f/requests/raise_for_status.py:106
def raise_for_status(
response: Union[Response, StreamResponse, ClientResponse, RequestsResponse],
message: str = None,
):
if hasattr(response, "status"):
return raise_for_status_async(response, message)
if response.ok:
return
is_html = False
if message is None:
is_html = response.headers.get("content-type", "").startswith(
"text/html"
) or response.text.startswith("<!DOCTYPE")
message = response.text
if message is None or is_html:
if response.status_code == 520:
message = "Unknown error (Cloudflare)"
if response.status_code in (429, 402):
raise RateLimitError(f"Response {response.status_code}: {message}")
if response.status_code == 401:
raise MissingAuthError(f"Response {response.status_code}: {message}")
if response.status_code == 403 and is_cloudflare(response.text):
raise CloudflareError(f"Response {response.status_code}: Cloudflare detected")
elif response.status_code == 403 and is_openai(response.text):
raise MissingAuthError(f"Response {response.status_code}: OpenAI Bot detected")
elif response.status_code == 502:
raise ResponseStatusError(f"Response {response.status_code}: Bad Gateway")
elif response.status_code == 504:
raise RateLimitError(f"Response {response.status_code}: Gateway Timeout ")
elif response.status_code == 400 and "API key not valid" in message:
raise MissingAuthError(f"Response {response.status_code}: Invalid API key")
else:
raise ResponseStatusError(
f"Response {response.status_code}: {'HTML content' if is_html else message}"
)
View on GitHub (pinned to 973504e177)
Solutions
- Retry with exponential backoff and jitter.
- Add a client-side throttle (sleep between calls / token bucket).
- Rotate to another g4f provider.
- If 402: the endpoint is paid — fund the account or choose a free provider.
Example fix
from g4f.errors import RateLimitError
# before
for prompt in prompts:
resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}])
# after
for i, prompt in enumerate(prompts):
try:
resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}])
except RateLimitError:
time.sleep(min(60, 2 ** i))
resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}]) Defensive patterns
Strategy: retry
Type guard
from g4f.errors import RateLimitError
def is_rate_limit(err: BaseException) -> bool:
return isinstance(err, RateLimitError) Try / catch
from g4f.errors import RateLimitError
import time
for attempt in range(5):
try:
resp = g4f.ChatCompletion.create(model, messages)
break
except RateLimitError as e:
if 'Response 402' in str(e):
raise
time.sleep(min(60, 2 ** attempt)) Prevention
- Throttle sync loops with time.sleep between calls
- Cache responses for repeated prompts
- Branch on 402 vs 429 in the message
- Consider the async client to use semaphores for concurrency control
When it happens
Trigger: Any synchronous g4f call path using requests whose upstream provider answers 429 (quota exceeded) or 402 (payment required).
Common situations: Bursty sync scripts exhausting free-tier quota; provider throttles per-minute requests; endpoint requiring credits.
Related errors
- Response {response.status}: {message}
- No coins left. Log in with a different account or wait a whi
- Response {response.status_code}: Gateway Timeout
- Failed to chat: {response.status} {error_text}
- Failed to decode JSON from PhindAi response: {text}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/3fe74b1259d20e1a.
Report an issue: GitHub.