Skip to content

Lesson 6 of 6

Make the tool robust and test it end-to-end

Add a retry for failed API calls and test the complete tool on a long document of your own.

8 minClaude, Gemini and ChatGPT

After this lesson, you will be able to add a retry to your Gemini API tool so that a temporary failure does not stop it, and you will test the complete tool on a long document of your own.

Why a retry helps

When you call a service over the internet, the request can fail for reasons outside your control: the network may drop a packet, the service may be busy, or a server may restart. If your script stops at the first failure, you must run it again by hand. A retry makes the script try the same call again after a short wait, and only give up after several attempts.

The Gemini Python library does not retry failed calls for you. You will add a small loop around the call that sends the request. The loop catches an exception, waits, and tries again. This is a standard pattern for any external service.

Add a retry to the tool

Open ask_gemini.py from the previous lesson. You will change the part that sends the request. First, add two constants near the top of the file, after the model name:

MAX_RETRIES = 3
RETRY_DELAY_SECONDS = 2

MAX_RETRIES is the total number of attempts, including the first one. RETRY_DELAY_SECONDS is how long the script waits between attempts. You can change these numbers later.

Now replace the single call to generate_content with a loop. The loop tries the call, and if it raises an exception, it prints a short message, waits, and tries again. If all attempts fail, it prints an error and exits.

for attempt in range(1, MAX_RETRIES + 1):
    try:
        response = client.models.generate_content(
            model=MODEL_NAME,
            contents=prompt,
        )
        print(response.text)
        break
    except Exception as e:
        print(f"Attempt {attempt} failed: {e}")
        if attempt == MAX_RETRIES:
            print("Giving up after all retries.")
            exit(1)
        time.sleep(RETRY_DELAY_SECONDS)

The try block runs the call. If it succeeds, the script prints the response and breaks out of the loop. If it fails, the except block prints the attempt number and the error. If this was the last attempt, the script exits with a non-zero code, which tells other tools that it failed. Otherwise, it waits for the delay and the loop continues.

You need to import the time module at the top of the file. Add this line with the other imports:

import time

The complete ask_gemini.py now looks like this:

import os
import sys
import time
from google import genai

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

MAX_RETRIES = 3
RETRY_DELAY_SECONDS = 2

if len(sys.argv) != 3:
    print("Usage: python ask_gemini.py <file_path> <question>")
    exit(1)

file_path = sys.argv[1]
question = sys.argv[2]

if not os.path.exists(file_path):
    print(f"File not found: {file_path}")
    exit(1)

document_text = read_file(file_path)

prompt = f"""Read the document carefully. Answer this question: {question}

For each point in your answer, give a short quote from the document that supports it. Put the quote in quotation marks.

Document:
{document_text}"""

for attempt in range(1, MAX_RETRIES + 1):
    try:
        response = client.models.generate_content(
            model=MODEL_NAME,
            contents=prompt,
        )
        print(response.text)
        break
    except Exception as e:
        print(f"Attempt {attempt} failed: {e}")
        if attempt == MAX_RETRIES:
            print("Giving up after all retries.")
            exit(1)
        time.sleep(RETRY_DELAY_SECONDS)

Run the script as before to check that it still works:

python ask_gemini.py report.txt "What are the main recommendations?"

You should see the answer printed as usual. If the call succeeds on the first attempt, you will not see any retry messages.

Test the retry

To see the retry in action, you can make the first attempt fail on purpose. One way is to set the API key to an invalid value for a moment. Do not change the file; instead, run the script with a wrong key in the environment. On Linux or macOS:

GEMINI_API_KEY=wrong-key python ask_gemini.py report.txt "What are the main recommendations?"

On Windows, set the variable first, then run the script, then unset it:

set GEMINI_API_KEY=wrong-key
python ask_gemini.py report.txt "What are the main recommendations?"
set GEMINI_API_KEY=

The script will print an error for attempt 1, wait two seconds, print an error for attempt 2, wait again, print an error for attempt 3, and then print "Giving up after all retries." and exit. The exact error message depends on the library and the service, but you should see three attempts.

After this test, run the script again with your real API key to confirm it still works.

Test the complete tool end-to-end

Now test the tool on a long document of your own. Choose a document of at least 2000 words, such as a report, a manual, or a long article. Save it as a text file, for example my_document.txt. Use a question that requires the model to read across the whole document, not just the beginning. For example:

python ask_gemini.py my_document.txt "What are the main arguments for and against the proposal?"

Check the answer carefully. Does it answer the question? Are the quotes exact, and do they come from the document? If the answer is too short or misses a key point, adjust the prompt in the script. For example, you could add a sentence that asks for a balanced answer with at least two points on each side. Run the script again and compare.

A good result is an answer that is correct, supported by exact quotes, and covers the question fully. The retry messages should not appear unless there is a real failure.

You have now built a tool that reads a long document, sends it to Gemini with a question, and prints an answer with supporting quotes. The retry makes it more reliable when the network or the service has a temporary problem.

Key takeaways

  • A retry loop catches exceptions from an API call and tries again after a delay.
  • Use constants for the number of retries and the delay so they are easy to change.
  • Print the attempt number and the error so you can see what happened.
  • Test the retry by using a wrong API key, then test the tool on a long document of your own.

Try it

Take a long document of your own, at least 2500 words, and save it as test_document.txt. Run ask_gemini.py with the question "What are the three most important conclusions?" Check that the answer has exactly three conclusions, each with an exact quote from the document. Then change MAX_RETRIES to 2 and RETRY_DELAY_SECONDS to 1, and run the script again with a wrong API key to see two quick attempts. Finally, change the constants back to 3 and 2, and run the script with your real key. A good result is a correct answer with three conclusions and exact quotes, and a retry test that shows two failed attempts before giving up.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.