502 Bad Gateway error & 429 Too Many Requests error

Hi there! I’m working on creating a script that will handle thousands of queries. I’ve developed a function specifically for this purpose, which executes the requests in a loop. I would appreciate your input on whether you think this function is good enough for this purpose:

// Function to send queries to ChatGPT and get completions
async function queryChatGpt(queries) {
  const results = [];

  for (const query of queries) {

    let success = false;
    let completion;

    // Retry loop for handling errors
    for (let retry = 0; retry < 50; retry++) {
      try {
        completion = await openai.createChatCompletion({
          model: "gpt-3.5-turbo",
          messages: [{ role: "user", content: `${query}` }],
        success = true; // Set success flag if the request is successful
        break; // Break the retry loop if successful
      } catch (error) {
        console.error("An error occurred:", error);
        if (error.response?.status === 502) {
          console.log("502 Bad Gateway error. Retrying...");
        } else if (error.response?.status === 429) {
          console.log("429 Too Many Requests error. Retrying...");
        } else {
          throw error; // Re-throw other errors
        await delay(10000); // Delay for 10 seconds before retrying

    // Store the result only if it was successful
    if (success) {
      results.push({ query, completion });
      await appendResultsToFile([{ query, completion }]);
    } else {
      console.error("Failed to get completion for query:", query);

    await delay(5000); // Delay for 5 seconds before the next iteration

  return results;
1 Like

Welcome to our dev community!

I mean, if you’re staying under your rate limits, you should be fine…

You can view the rate limits for your organization under the rate limits section of the account management page.