Skip to content

fix: correct type comparison in rate_limit_sleep causing excessive sleep on TPM rate limit#1174

Open
octo-patch wants to merge 5 commits into
Pythagora-io:mainfrom
octo-patch:fix/issue-1121-rate-limit-token-comparison
Open

fix: correct type comparison in rate_limit_sleep causing excessive sleep on TPM rate limit#1174
octo-patch wants to merge 5 commits into
Pythagora-io:mainfrom
octo-patch:fix/issue-1121-rate-limit-token-comparison

Conversation

@octo-patch

Copy link
Copy Markdown

Fixes #1121

Problem

When OpenAI returns a 429 rate limit error due to tokens-per-minute (TPM) exhaustion, GPT Pilot sleeps for an excessively long time (commonly 7200 or 9600 seconds) instead of the short wait time indicated in the API response (e.g. 11 seconds).

Root cause: In core/llm/openai_client.py, the rate_limit_sleep method reads the x-ratelimit-remaining-tokens response header as a string, then compares it to the integer 0:

remaining_tokens = headers["x-ratelimit-remaining-tokens"]  # str, e.g. "0"
if remaining_tokens == 0:   # always False — str != int
    match = re.search(time_regex, headers["x-ratelimit-reset-tokens"])
else:
    match = re.search(time_regex, headers["x-ratelimit-reset-requests"])

Because "0" == 0 is always False, the code never reads x-ratelimit-reset-tokens (which holds the short per-minute token reset time). It always reads x-ratelimit-reset-requests instead, which can be set to a much longer duration (e.g. 2 hours = 7200 s) when the requests-per-day limit is the binding constraint.

Solution

Cast remaining_tokens to int before the comparison:

if int(remaining_tokens) == 0:

This ensures that when token usage is at the limit, the code correctly reads x-ratelimit-reset-tokens (the short, per-minute reset time) rather than x-ratelimit-reset-requests.

Testing

Manually reviewed the logic. The fix is a one-character type cast that makes the comparison semantically correct and matches the documented OpenAI rate limit header behavior.

@CLAassistant

CLAassistant commented Apr 18, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 4 committers have signed the CLA.

❌ Zvonimir Sabljic
❌ MarkoBosnjak98
❌ github-actions[bot]
❌ LeonOstrez


Zvonimir Sabljic seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@LeonOstrez LeonOstrez force-pushed the main branch 2 times, most recently from 90f59f5 to a372904 Compare June 8, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Rate limit Timeout Sleep WAY too long

4 participants