tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > ADK Agent Testing > Testing Retry and Exponential Backoff in ADK Agents

Testing Retry and Exponential Backoff in ADK Agents

Author: Venkata Sudhakar

ShopMax India's ADK agents call external APIs for order lookups, inventory checks, and payment processing. These APIs can be temporarily unavailable due to rate limits, network blips, or maintenance windows. Without retry logic, a single transient failure causes the agent to return an error to the customer. Testing the retry and exponential backoff implementation ensures the agent recovers automatically from short outages without overwhelming the upstream service.

Exponential backoff retries a failed call after increasingly long delays: attempt 1 after 1 second, attempt 2 after 2 seconds, attempt 3 after 4 seconds, and so on up to a maximum wait. The test uses a mock that fails a configurable number of times before succeeding, then asserts that the correct number of attempts were made, the total elapsed time matches the expected backoff schedule, and the final result is correct.

The example below implements a retry wrapper with exponential backoff and tests it against a mock that simulates two transient failures before succeeding on the third attempt.


It gives the following output,

Total attempts: 3
Total elapsed: 0.152s
Result: Order ORD-7821 is shipped.
.. (2 passed in 0.30s)

In production, set BASE_DELAY_S to 1.0 and MAX_RETRIES to 4 for real API calls to give upstream services enough time to recover. Add jitter to the backoff delay (multiply by a random factor between 0.5 and 1.5) to prevent retry storms when many concurrent agents fail at the same moment. Log each retry attempt with the attempt number, error message, and wait time so the on-call team in Bangalore or Chennai can diagnose persistent failures quickly.


 
  


  
bl  br