skip to Main Content

I’m encountering a "429 Too Many Requests" error when using the OpenAI API in my web application. I’ve implemented a retry mechanism with exponential backoff, but the error persists even after retries. Here’s the relevant part of my code:

async function getResponse() {
    const apiKey = 'YOUR_API_KEY';
    let retryCount = 0;
    const maxRetries = 10;
    let backoff = 1000; // initial backoff time in milliseconds

    while (retryCount < maxRetries) {
        try {
            const response = await fetch('https://api.openai.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey}`
                },
                body: JSON.stringify({
                    model: "gpt-3.5-turbo",
                    messages: [{ "role": "user", "content": userInput }]
                })
            });

            if (response.status === 429) {
                console.warn(`Rate limit exceeded. Retrying in ${backoff / 1000} seconds...`);
                await new Promise(resolve => setTimeout(resolve, backoff));
                backoff *= 2; // exponential backoff
                retryCount++;
            } else if (response.ok) {
                const data = await response.json();
                // Process response data
                break;
            } else {
                console.error('Unexpected error:', response);
                break;
            }
        } catch (error) {
            console.error('Error:', error);
            break;
        }
    }

    if (retryCount === maxRetries) {
        console.error('Max retries reached.');
    }
}

After creating the Api key from OpenAI I have paste it in the above code where it says ‘YOUR_API_KEY’

Despite handling retries and backoff properly, the "429 Too Many Requests" error still occurs frequently. Could anyone provide insights or suggestions on how to better manage this error, or any possible ways to optimize API calls to avoid hitting rate limits?

2

Answers


  1. Chosen as BEST ANSWER

    The issue was related to payment. Upgrading your OpenAI account doesn’t automatically include an API key subscription. You’ll need to purchase a subscription separately based on the pricing available on this page: OpenAI API Pricing.

    Once I subscribed, the problem was resolved.

    Thank you, everyone!


  2. Friend, read this section of the docs: Rate limits. You can extract the x-ratelimit-remaining-requests and x-ratelimit-remaining-tokens headers to know how many requests your API-key can still do without "too much" errors…

    Example:

        class ArtificialIntelligence
        {
          constructor ( Address, Credential )
          {
            this.#API = Address;
            this.#KEY = Credential;
            
            this.#DelayUpdate = 10 * 1000; // If no more remaining tokens/requests, then allow new queries after 10s.
            this.#LastUpdate = NaN;
            this.#RemainingTokens = 1;
            this.#RemainingRequests = 1;
          }
          
          async Query ( Predicate )
          {
            const Now = Date.now();
            
            if
            (
              ( ( this.#RemainingTokens == 0 ) || ( this.#RemainingRequests == 0 ) )
              &&
              ( ( Now - this.#LastUpdate ) < this.#DelayUpdate ) )
            {
              // Block the query?
              throw new Error('Too much request to the API.');
            }
            
            const Request = fetch
            (
              this.#API,
              {
                method: 'POST',
                headers:
                {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + this.#KEY
                },
                body: JSON.stringify
                ({
                    model: "gpt-3.5-turbo",
                    messages: [{ "role": "user", "content": Predicate }]
                })
              }
            );
            
            Request.then
            (
              Response =>
              {
                this.#LastUpdate = Now;
                this.#RemainingTokens = Number(Response.headers.get('x-ratelimit-remaining-tokens'));
                this.#RemainingRequests = Number(Response.headers.get('x-ratelimit-remaining-requests'));
                
                // Proccess the Response here (or later)...
                
                return Response;
              }
            );
            
            Request.catch
            (
              Exception =>
              {
                // Manage the Response errors here (or later)...
                
                console.warn(Exception);
              }
            );
            
            return Request;
          }
          
          get DelayTime ()
          {
            return this.#DelayUpdate;
          }
          
          set DelayTime ( New )
          {
            this.#DelayUpdate = Number(New);
          }
          
          get UpdateTime ()
          {
            return this.#LastUpdate;
          }
          
          get RemainingTokens ()
          {
            return this.#RemainingTokens;
          }
          
          get RemainingRequests ()
          {
            return this.#RemainingRequests;
          }
        }
    
    

    PS: As I don’t have an api key for chat-gpt I didn’t test it…

    NOTES:

    This is not a finished ready-to-use code. You’ll have to make your adaptations to it.

    By the way, with this logic, if the remaining tokens or requests reaches 0, then new queries are refused if Date.now() - this.#LastUpdate < this.#DelayUpdate and then an Error is thrown.

    At server-side, the API could still respond with error if the remaining tokens or requests data didn’t refresh… Change the this.#DelayUpdate to match the refresh rate of the api data.

    I hope it can help you manage the requests to the api better…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search