-
Notifications
You must be signed in to change notification settings - Fork 419
fix: improve rate limit handling with exponential backoff #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leonvanzyl
merged 7 commits into
AutoForgeAI:master
from
cabana8471-arch:fix/rate-limit-handling
Feb 1, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf194ad
fix: improve rate limit handling with exponential backoff
cabana8471-arch ff1a63d
fix: address CodeRabbit review feedback
cabana8471-arch cf8dec9
fix: address CodeRabbit review - extract rate limit logic to shared m…
cabana8471-arch dcf8b99
fix: remove unused RATE_LIMIT_PATTERNS import
cabana8471-arch f018b4c
fix: address PR #109 review feedback from leonvanzyl
cabana8471-arch 88c6952
fix: address 3 new CodeRabbit review comments
cabana8471-arch 89f6721
fix: use clamp_retry_delay() for reset-time delays
cabana8471-arch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """ | ||
| Rate Limit Utilities | ||
| ==================== | ||
|
|
||
| Shared utilities for detecting and handling API rate limits. | ||
| Used by both agent.py (production) and test_rate_limit_utils.py (tests). | ||
| """ | ||
|
|
||
| import re | ||
| from typing import Optional | ||
|
|
||
| # Regex patterns for rate limit detection (used in both exception messages and response text) | ||
| # These patterns use word boundaries to avoid false positives like "PR #429" or "please wait while I..." | ||
| RATE_LIMIT_REGEX_PATTERNS = [ | ||
| r"\brate[_\s]?limit", # "rate limit", "rate_limit", "ratelimit" | ||
| r"\btoo\s+many\s+requests", # "too many requests" | ||
| r"\bhttp\s*429\b", # "http 429", "http429" | ||
| r"\bstatus\s*429\b", # "status 429", "status429" | ||
| r"\berror\s*429\b", # "error 429", "error429" | ||
| r"\b429\s+too\s+many", # "429 too many" | ||
| r"\boverloaded\b", # "overloaded" | ||
| r"\bquota\s*exceeded\b", # "quota exceeded" | ||
| ] | ||
|
|
||
| # Compiled regex for efficient matching | ||
| _RATE_LIMIT_REGEX = re.compile( | ||
| "|".join(RATE_LIMIT_REGEX_PATTERNS), | ||
| re.IGNORECASE | ||
| ) | ||
|
|
||
|
|
||
| def parse_retry_after(error_message: str) -> Optional[int]: | ||
| """ | ||
| Extract retry-after seconds from various error message formats. | ||
|
|
||
| Handles common formats: | ||
| - "Retry-After: 60" | ||
| - "retry after 60 seconds" | ||
| - "try again in 5 seconds" | ||
| - "30 seconds remaining" | ||
|
|
||
| Args: | ||
| error_message: The error message to parse | ||
|
|
||
| Returns: | ||
| Seconds to wait, or None if not parseable. | ||
| """ | ||
| # Patterns require explicit "seconds" or "s" unit, OR no unit at all (end of string/sentence) | ||
| # This prevents matching "30 minutes" or "1 hour" since those have non-seconds units | ||
| patterns = [ | ||
| r"retry.?after[:\s]+(\d+)\s*(?:seconds?|s\b)", # Requires seconds unit | ||
| r"retry.?after[:\s]+(\d+)(?:\s*$|\s*[,.])", # Or end of string/sentence | ||
| r"try again in\s+(\d+)\s*(?:seconds?|s\b)", # Requires seconds unit | ||
| r"try again in\s+(\d+)(?:\s*$|\s*[,.])", # Or end of string/sentence | ||
| r"(\d+)\s*seconds?\s*(?:remaining|left|until)", | ||
| ] | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| for pattern in patterns: | ||
| match = re.search(pattern, error_message, re.IGNORECASE) | ||
| if match: | ||
| return int(match.group(1)) | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def is_rate_limit_error(error_message: str) -> bool: | ||
| """ | ||
| Detect if an error message indicates a rate limit. | ||
|
|
||
| Uses regex patterns with word boundaries to avoid false positives | ||
| like "PR #429", "please wait while I...", or "Node v14.29.0". | ||
|
|
||
| Args: | ||
| error_message: The error message to check | ||
|
|
||
| Returns: | ||
| True if the message indicates a rate limit, False otherwise. | ||
| """ | ||
| return bool(_RATE_LIMIT_REGEX.search(error_message)) | ||
|
|
||
|
|
||
| def calculate_rate_limit_backoff(retries: int) -> int: | ||
| """ | ||
| Calculate exponential backoff for rate limits. | ||
|
|
||
| Formula: min(60 * 2^retries, 3600) - caps at 1 hour | ||
| Sequence: 60s, 120s, 240s, 480s, 960s, 1920s, 3600s... | ||
|
|
||
| Args: | ||
| retries: Number of consecutive rate limit retries (0-indexed) | ||
|
|
||
| Returns: | ||
| Delay in seconds (clamped to 1-3600 range) | ||
| """ | ||
| return int(min(max(60 * (2 ** retries), 1), 3600)) | ||
|
|
||
|
|
||
| def calculate_error_backoff(retries: int) -> int: | ||
| """ | ||
| Calculate linear backoff for non-rate-limit errors. | ||
|
|
||
| Formula: min(30 * retries, 300) - caps at 5 minutes | ||
| Sequence: 30s, 60s, 90s, 120s, ... 300s | ||
|
|
||
| Args: | ||
| retries: Number of consecutive error retries (1-indexed) | ||
|
|
||
| Returns: | ||
| Delay in seconds (clamped to 1-300 range) | ||
| """ | ||
| return min(max(30 * retries, 1), 300) | ||
|
|
||
|
|
||
| def clamp_retry_delay(delay_seconds: int) -> int: | ||
| """ | ||
| Clamp a retry delay to a safe range (1-3600 seconds). | ||
|
|
||
| Args: | ||
| delay_seconds: The raw delay value | ||
|
|
||
| Returns: | ||
| Delay clamped to 1-3600 seconds | ||
| """ | ||
| return min(max(delay_seconds, 1), 3600) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.