1. Learn
  2. /
  3. Courses
  4. /
  5. Developing AI Systems with the OpenAI API

Connected

Exercise

Avoiding rate limits with retry

You've created a function to run Chat Completions with a custom message but have noticed it sometimes fails due to rate limits. You decide to use the @retry decorator from the tenacity library to avoid errors when possible.

Instructions

100 XP
  • Import the tenacity library with required functions: retry, wait_random_exponential, and stop_after_attempt.
  • Create an OpenAI API client.
  • Complete the retry decorators with the parameters required to start retrying at an interval of 5 seconds, up to 40 seconds, and to stop after 4 attempts.