|
|
ADK Circuit Breaker Pattern
Author: Venkata Sudhakar
A circuit breaker prevents an ADK agent from repeatedly calling a failing downstream service, which wastes tokens, delays responses, and can cascade into wider system failures. ShopMax India integrates with several external APIs - inventory, payments, and logistics - and any of them can become slow or unavailable. A circuit breaker tracks failure rate and opens (stops calls) when a threshold is crossed, returning a fallback response until the service recovers.
The circuit breaker has three states: closed (calls pass through normally), open (calls are blocked, fallback is returned), and half-open (a single probe call is allowed to test recovery). State transitions are driven by failure count and a cooldown timer. In ADK, the circuit breaker wraps each tool function and is transparent to the agent - the agent calls the tool and either gets the real response or the fallback.
The below example shows a circuit breaker implementation wrapping a ShopMax inventory API tool.
It gives the following output,
SKU-1042: The inventory service is temporarily unavailable for SKU-1042.
Circuit state: closed, failures: 1
SKU-2218: The inventory service is temporarily unavailable for SKU-2218.
Circuit state: closed, failures: 2
SKU-3301: The inventory service is temporarily unavailable for SKU-3301.
Circuit state: open, failures: 3
SKU-4105: Circuit is open. Returning fallback without calling API.
Circuit state: open, failures: 3
SKU-5001: Circuit is open. Returning fallback without calling API.
Circuit state: open, failures: 3
In production, store circuit state in Memorystore (Redis) rather than in-process so that all Cloud Run instances share the same breaker state. Emit a Cloud Monitoring metric whenever the circuit opens or closes so the on-call team is alerted immediately. Use the half-open state with a single probe request every 30 seconds to detect recovery automatically, and log all circuit state transitions to Cloud Logging for post-incident analysis.
|
|