Heuristic:666ghj BettaFish API Retry with Graceful Degradation
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Optimization, API_Integration |
| Last Updated | 2026-10-08 21:30 GMT |
Overview
Retry external API calls with exponential backoff on transient failures, returning a safe default value (graceful degradation) when all retries are exhausted.
Description
External search APIs (Tavily, Bocha, Anspire) and LLM APIs can fail transiently due to rate limits (429), server errors (5xx), or network issues. BettaFish uses a `with_graceful_retry` decorator that retries with exponential backoff (initial_delay=1s, backoff_factor=2x, max_delay=60s, max_retries=3). When all retries are exhausted, it returns a `default_return` value (e.g., an empty `TavilyResponse`) instead of raising an exception. This allows the pipeline to continue with partial results rather than crashing.
Usage
Use this heuristic when calling external APIs that may fail transiently and where partial results are acceptable. Apply when a hard failure would terminate an expensive pipeline run.
The Insight (Rule of Thumb)
- Action: Wrap external API calls with `@with_graceful_retry(config, default_return=...)`.
- Value: `RetryConfig(max_retries=3, initial_delay=1.0, backoff_factor=2.0, max_delay=60.0)`. Retryable HTTP status codes: {408, 409, 425, 429} and 500-599.
- Trade-off: Retries add latency but improve success rate. Graceful degradation returns partial/empty results instead of crashing.
- Compatibility: Works with any function that may raise exceptions. The decorator catches and retries.
Reasoning
External API failure rates of 1-5% are common in production. Without retries, a single API failure would crash an entire pipeline run that may have taken minutes to set up. With 3 retries and exponential backoff, the success rate rises to >99% for transient failures. The `default_return` pattern ensures the pipeline continues — e.g., a failed search returns empty results, which the summarization node handles gracefully. This is preferable to a hard crash that wastes all prior work.
Code Evidence
Retry configuration from `utils/retry_helper.py:38-77`:
class RetryConfig:
def __init__(self, max_retries=3, initial_delay=1.0, backoff_factor=2.0, max_delay=60.0):
self.max_retries = max_retries
self.initial_delay = initial_delay
self.backoff_factor = backoff_factor
self.max_delay = max_delay
RETRYABLE_HTTP_STATUS_CODES = {408, 409, 425, 429}
def _should_retry_exception(exc):
status_code = _get_http_status_code(exc)
if status_code is None:
return True
return status_code in RETRYABLE_HTTP_STATUS_CODES or 500 <= status_code <= 599
Graceful degradation in search from `QueryEngine/tools/search.py:95`:
@with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return=TavilyResponse(query="搜索失败"))
def _search_internal(self, **kwargs) -> TavilyResponse:
...