0
0
Prompt Engineering / GenAIml~20 mins

Error handling and rate limits in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rate Limit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Rate Limits in API Calls

When using a machine learning API that enforces rate limits, what is the best practice to avoid hitting the limit?

ASend all requests as fast as possible and retry only if the server crashes.
BIgnore rate limits because the API will queue requests automatically.
CImplement a delay between requests to stay within the allowed number of calls per minute.
DUse multiple API keys simultaneously without restrictions.
Attempts:
2 left
💡 Hint

Think about how to respect the server's capacity to handle requests.

Predict Output
intermediate
2:00remaining
Output of Error Handling Code

What will be the output of the following Python code snippet that calls a machine learning API with error handling?

Prompt Engineering / GenAI
import time

class APIError(Exception):
    pass

def call_api():
    raise APIError('Rate limit exceeded')

try:
    call_api()
except APIError as e:
    print(f'Error caught: {e}')
    time.sleep(1)
    print('Retrying...')
A
Error caught: Rate limit exceeded
Retrying...
BError caught: Rate limit exceeded
CNo output, program crashes
DRetrying...
Attempts:
2 left
💡 Hint

Look at what happens inside the except block.

Model Choice
advanced
2:00remaining
Choosing a Strategy for Handling API Rate Limits

You have a machine learning model API with a strict rate limit of 5 requests per second. Which strategy best handles this limit while maximizing throughput?

AQueue requests and send them at a steady rate of 5 per second.
BSend requests randomly without delay and ignore errors.
CSend 10 requests at once and retry failed ones after 10 seconds.
DSend 1 request per minute to be safe.
Attempts:
2 left
💡 Hint

Consider how to keep requests within the allowed rate without wasting time.

Metrics
advanced
2:00remaining
Interpreting Error Rate Metrics

You monitor your ML API calls and see the following metrics over 1000 requests: 950 successful, 30 rate limit errors, 20 timeout errors. What is the error rate percentage?

A50%
B2%
C10%
D5%
Attempts:
2 left
💡 Hint

Error rate = (number of errors / total requests) * 100

🔧 Debug
expert
2:00remaining
Debugging API Rate Limit Handling Code

Given the following Python code snippet that calls an ML API, which option correctly identifies the bug causing the program to crash?

Prompt Engineering / GenAI
import time

rate_limit = 3
calls = 0
start_time = time.time()

def call_api():
    global calls, start_time
    if calls >= rate_limit:
        elapsed = time.time() - start_time
        time.sleep(1 - elapsed)
        calls = 0
        start_time = time.time()
    calls += 1
    print('API call made')

for _ in range(5):
    call_api()
AThe variable 'calls' is not declared global inside call_api(), causing UnboundLocalError.
BThe time.sleep() call may receive a negative argument causing ValueError.
CThe start_time is reset too early, causing infinite loop.
DThe loop runs 5 times but only 3 API calls are made.
Attempts:
2 left
💡 Hint

Check the calculation inside time.sleep().