Sentry Answers>Node.js>

OpenAI API giving error: 429 Too Many Requests

OpenAI API giving error: 429 Too Many Requests

Matthew C.

The Problem

When making HTTP requests to the OpenAI API, you may get a “Too Many Requests” or “Rate limit reached” 429 error, with a message such as the following:

Click to Copy
Rate limit reached for gpt-4-0613 in organization org-exampleorgid on tokens per min. Limit: 10000.000000 / min. Current: 10020.000000 / min.

These errors occur when you exceed the rate limit of your project, organization, or model(s). Your rate limit is the maximum number of requests or tokens that can be submitted per minute or per day. You may also get a 429 error if you reach your usage limit.

The Solution

You need to control the number of requests you make, buy more credits, or increase your project or organization’s monthly spending limits.

Rate Limit Reached for Requests

A “Rate limit reached” error means that you are sending requests too quickly. You may have hit the rate limit set for your project or organization. You may also be rate limited by the AI model you’re using, as models have their own rate limits. You can learn more about rate limits in the OpenAI platform rate limit guide.

You can view the rate limits for your organization under the limits section of your account settings. You can avoid rate limit errors by retrying requests that get a rate limit error response with an exponential backoff. When retrying a request, add a short wait period before sending the request. If the request is unsuccessful, increase the wait period. Repeat this process until the request succeeds or the maximum number of requests is reached. This technique is known as automatic retries with an exponential backoff.

The OpenAI Node API library automatically retries 429 error requests with a short exponential backoff, from version 4 onward. You can use the maxRetries option to set the number of retries:

Click to Copy
// Configure the default for all requests const client = new OpenAI({ maxRetries: 5, // default is 2 }); // Configure per request await openai.chat.completions.create( { messages: [ { role: "user", content: "How can I fix a OpenAI API error: 429 Too Many Requests?", }, ], model: "gpt-4-0613", }, { maxRetries: 7, } );

Requests time out after 10 minutes by default. You can configure this with the timeout option:

Click to Copy
// Configure the default for all requests const client = new OpenAI({ timeout: 20 * 1000, // 20 seconds (default is 10 minutes) }); // Configure per request await openai.chat.completions.create( { messages: [ { role: "user", content: "How can I fix a OpenAI API error: 429 Too Many Requests?", }, ], model: "gpt-4-0613", }, { timeout: 60 * 1000, } );

Current Quota Exceeded

When you’ve exceeded your current quota, it means that you’ve run out of credits or you’ve reached your maximum monthly spending limit.

If you’ve used up all of your credits, you must buy more credits. If you’ve reached your maximum monthly spending limit, you must increase your usage limits. Your usage limit depends on your usage tier. You can view the usage limits for your organization under the limits section of your account settings.

  • SentryWorkshop: Debugging your Node.js Project With Sentry
  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • ResourcesBackend Error Monitoring 101
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.