#[Fix] Rate limit | Too Many requests. While using the OpenAI API.

2 messages · Page 1 of 1 (latest)

echo lake
#

Many people are having issues with rate limits since the last few days.
It is a common issue and there are 2 possible scenarios for this:

1- you are consistently getting this error
2- you get this randomly

For case 1, it is not a problem, it is working as intended. You must slow down your requests.

For case 2. You are in the clear, it is more related to the API itself having issues. The solution for that on your code is to catch the exception and try again.

Here is a small example with JS that illustrates the problem

async function chatBotReply(message, i = 0) {
  try {
    // this function is just an example that would return the response, it is not implemented on this example
    return await getGPTResponse(message); 

  } catch (error) {
     // 429 Too Many Requests || 503 Service Unavailable
     // For those error codes, we attempt to do the request again.
    if ((error.response.status == 429 ||  error.response.status == 503) && i < 3) {
      await sleep(5000);
      return chatBotReply(message i + 1);

    } else {
      // it is a different error or we retried too many times already.
      // For either case, we don't make further requests.
      return null;

    }
  }
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

In case the request fails, it will wait 5 seconds and re-try the request to a maximum of 5 times.

This is just an example for people with simple chat bots and small scripts. For a system that handles massive amounts of requests, you will need something more sophisticated like a queue, but if you are on this level, you don't need this tutorial either =P

#

[Fix] Rate limit | Too Many requests. While using the OpenAI API.