Fix rate limit sleep duration calculation#1168
Open
Krishnachaitanyakc wants to merge 1 commit intoPythagora-io:mainfrom
Open
Fix rate limit sleep duration calculation#1168Krishnachaitanyakc wants to merge 1 commit intoPythagora-io:mainfrom
Krishnachaitanyakc wants to merge 1 commit intoPythagora-io:mainfrom
Conversation
Replace timedelta.seconds with int(total_seconds()) in the rate limit handler to correctly compute sleep durations. The .seconds attribute only returns the seconds component within the current day (0-86399), which produces incorrect values for timedeltas spanning days (e.g. daily rate limits returning timedelta(days=1) would sleep 0 seconds) or negative timedeltas from clock drift (timedelta(seconds=-5) would sleep ~86395 seconds). The sleep duration is now clamped to a 1-3600 second range to prevent both zero-second busy loops and excessively long waits that make the tool unusable. Also adds a log.info() call so rate limit sleeps are always visible in logs regardless of error_handler availability. Fixes Pythagora-io#1121
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #1121
The rate limit handler in
BaseLLMClient.__call__usedwait_time.secondsto compute the sleep duration, which is incorrect fortimedeltaobjects that span multiple days. Python'stimedelta.secondsonly returns the seconds component within the current day (0-86399), not the total number of seconds:timedelta(days=1)(daily rate limit reset) has.seconds = 0, causing a zero-second sleep that immediately retries and gets rate-limited again in a busy looptimedeltafrom clock drift (e.g.timedelta(seconds=-5)=timedelta(days=-1, seconds=86395)) has.seconds = 86395, causing a ~24-hour sleepChanges
core/llm/base.py: Replacewait_time.secondswithint(wait_time.total_seconds())and clamp the result to a 1-3600 second range. This prevents both zero-second busy loops and excessively long waits (over 1 hour) that make the tool unusable. Also addslog.info()so rate limit sleeps are always logged.tests/llm/test_rate_limit.py: Add 8 new test cases covering normal durations, large values (capped at 3600s), daily rate limits, negative timedeltas, fractional seconds, and the case whererate_limit_sleepreturnsNone.Test plan
python3 -m pytest tests/llm/ -v)RateLimitError(whererate_limit_sleepreturnsNone) still raisesAPIErrorwithout sleeping