Bird
Raised Fist0
Agentic AIml~20 mins

Retry and fallback logic in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Retry and Fallback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Retry Logic Purpose
Why is retry logic important in agentic AI systems when calling external APIs?
ATo increase the number of API calls regardless of errors
BTo permanently stop the process after the first failure
CTo ignore errors and continue without any checks
DTo handle temporary failures by trying the request again automatically
Attempts:
2 left
💡 Hint
Think about what happens if a network glitch causes a temporary failure.
Predict Output
intermediate
1:30remaining
Output of Retry Loop with Limited Attempts
What will be the printed output of this retry logic code snippet?
Agentic AI
attempts = 0
max_attempts = 3
while attempts < max_attempts:
    attempts += 1
    if attempts < 3:
        print(f"Attempt {attempts}: Failed")
    else:
        print(f"Attempt {attempts}: Success")
        break
A
Attempt 1: Failed
Attempt 2: Failed
Attempt 3: Success
B
Attempt 1: Failed
Attempt 2: Success
CAttempt 1: Success
D
Attempt 1: Failed
Attempt 2: Failed
Attempt 3: Failed
Attempts:
2 left
💡 Hint
Check how many times the loop runs and when it breaks.
Model Choice
advanced
2:00remaining
Choosing Fallback Strategy for Agentic AI
If an agentic AI fails to get a response from a primary model after retries, which fallback strategy is best to maintain user experience?
AStop all processing and show an error message immediately
BSwitch to a simpler, faster model that provides approximate answers
CKeep retrying indefinitely without fallback
DReturn empty or null responses without explanation
Attempts:
2 left
💡 Hint
Consider user experience and response time.
Metrics
advanced
1:30remaining
Evaluating Retry Logic Impact on Latency
Which metric best measures the impact of retry logic on system latency?
AAverage response time including retries
BModel accuracy on successful calls
CNumber of retries attempted
DMemory usage during retries
Attempts:
2 left
💡 Hint
Think about how retries affect how long users wait.
🔧 Debug
expert
2:30remaining
Identifying Bug in Retry with Fallback Code
What error will this retry and fallback code raise when the primary model always fails?
Agentic AI
def get_response():
    for _ in range(2):
        try:
            result = primary_model_call()
            return result
        except Exception:
            continue
    return fallback_model_call()

response = get_response()
ANo error, returns fallback model response
BTypeError because fallback_model_call returns wrong type
CNameError because primary_model_call is not defined
DRuntimeError due to infinite loop
Attempts:
2 left
💡 Hint
Check if the functions called are defined in the code snippet.

Practice

(1/5)
1.

What is the main purpose of retry logic in an AI system?

easy
A. To replace the task with a different unrelated task
B. To permanently stop a task after the first failure
C. To ignore errors and continue without any checks
D. To try a task multiple times to handle temporary failures

Solution

  1. Step 1: Understand retry logic concept

    Retry logic means trying the same task again if it fails temporarily, like retrying a phone call if the line is busy.
  2. Step 2: Match retry logic to options

    Only To try a task multiple times to handle temporary failures describes trying multiple times to handle temporary failures, which fits retry logic.
  3. Final Answer:

    To try a task multiple times to handle temporary failures -> Option D
  4. Quick Check:

    Retry logic = multiple attempts [OK]
Hint: Retry means try again after failure [OK]
Common Mistakes:
  • Confusing retry with fallback
  • Thinking retry stops after one failure
  • Assuming retry changes the task
2.

Which of the following is the correct Python syntax to retry a function fetch_data() up to 3 times?

for _ in range(3):
    try:
        fetch_data()
        break
    except Exception:
        pass
easy
A. for _ in range(3): try: fetch_data() break except Exception: pass
B. for _ in range(3): fetch_data() break except Exception: pass
C. while True: fetch_data() break except Exception: pass
D. for i in range(3): fetch_data() except: break

Solution

  1. Step 1: Check syntax for retry loop

    The code uses a for loop to try 3 times, with try-except to catch errors and break if successful.
  2. Step 2: Identify correct syntax

    for _ in range(3): try: fetch_data() break except Exception: pass matches the correct Python syntax with try-except inside the loop and break on success.
  3. Final Answer:

    for _ in range(3): try: fetch_data() break except Exception: pass -> Option A
  4. Quick Check:

    Correct retry loop syntax = for _ in range(3): try: fetch_data() break except Exception: pass [OK]
Hint: Look for try-except inside a for loop with break [OK]
Common Mistakes:
  • Missing try-except block
  • Incorrect loop syntax
  • Using 'except' without 'try'
3.

Consider this code snippet implementing retry and fallback logic:

def get_data():
    for _ in range(2):
        try:
            return fetch_from_primary()
        except Exception:
            pass
    return fetch_from_backup()

If fetch_from_primary() fails both times, what will get_data() return?

medium
A. The result of fetch_from_primary()
B. The result of fetch_from_backup()
C. None
D. An exception is raised

Solution

  1. Step 1: Analyze retry attempts

    The function tries fetch_from_primary() twice inside the loop, catching exceptions and continuing if it fails.
  2. Step 2: Understand fallback behavior

    If both retries fail, the function calls and returns fetch_from_backup() as a fallback.
  3. Final Answer:

    The result of fetch_from_backup() -> Option B
  4. Quick Check:

    Retries fail -> fallback used = The result of fetch_from_backup() [OK]
Hint: If retries fail, fallback result is returned [OK]
Common Mistakes:
  • Assuming primary always returns result
  • Ignoring fallback call
  • Thinking exception propagates
4.

Identify the bug in this retry and fallback code snippet:

def get_info():
    for i in range(3):
        try:
            return fetch_data()
        except:
            continue
    return fallback_data()
medium
A. The except block catches all exceptions without specifying type
B. The function returns fallback_data() even if fetch_data() succeeds
C. The except block should raise the exception instead of continue
D. The loop variable i is unused and should be removed

Solution

  1. Step 1: Review exception handling

    The except block catches all exceptions without specifying the exception type, which is bad practice and can hide bugs.
  2. Step 2: Identify best practice

    It's better to catch specific exceptions to avoid masking unexpected errors.
  3. Final Answer:

    The except block catches all exceptions without specifying type -> Option A
  4. Quick Check:

    Catch specific exceptions, not all [OK]
Hint: Avoid bare except; specify exception type [OK]
Common Mistakes:
  • Using bare except blocks
  • Ignoring exception types
  • Assuming unused variables cause bugs
5.

You want to design an AI agent that tries to fetch user data from a primary server up to 3 times. If all retries fail, it should fetch from a backup server. Which code snippet correctly implements this retry and fallback logic?

Option A:
for _ in range(3):
    try:
        data = fetch_primary()
    except:
        data = fetch_backup()
        break
Option B:
for _ in range(3):
    try:
        data = fetch_primary()
        break
    except:
        pass
else:
    data = fetch_backup()
Option C:
try:
    data = fetch_primary()
except:
    data = fetch_backup()
Option D:
while True:
    try:
        data = fetch_primary()
        break
    except:
        data = fetch_backup()
        break
hard
A. Retries primary, but fallback runs immediately on first failure
B. No retries, fallback runs immediately on first failure
C. Retries primary 3 times, then fallback if all fail
D. Retries once, fallback runs immediately after first failure

Solution

  1. Step 1: Understand retry and fallback requirements

    The agent must retry fetching from primary 3 times, then fallback only if all retries fail.
  2. Step 2: Analyze each option's behavior

    Retries primary 3 times, then fallback if all fail uses a for loop with try-except and an else clause that runs fallback only if loop completes without break (all retries failed). This matches requirements.
  3. Final Answer:

    Retries primary 3 times, then fallback if all fail -> Option C
  4. Quick Check:

    Retry 3 times + fallback after = Retries primary 3 times, then fallback if all fail [OK]
Hint: Use for-else to run fallback after retries fail [OK]
Common Mistakes:
  • Running fallback too early
  • Not retrying enough times
  • Missing else clause for fallback