feat: add retry with exponential backoff for transient API failures - #63
Merged
Merged
Conversation
Wrap LLM and Niutrans HTTP calls with retry_with_backoff() that retries on 429503/503,). Client errors (401, 400 422) fail immediately without retry. Saves LLM API credits when a later pipeline step fails transiently —transiently the—spend on Steps 1-2 is—.
test_llm_client.py loaded llm_client.py standalone via spec_from_file_location, which breaks now that llm_client imports `from .retry`. Import through the package instead (consistent with test_smoke.py). Fixes test collection. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The pipeline calls each API exactly once per step. If DeepSeek returns a 429 (rate limit) or Google Translate has a momentary network blip, the entire pipeline fails — even though the error is transient and would likely succeed on retry. This is especially painful because Steps 1-2 consume LLM API credits. If Step 3 or 4 fails transiently, the LLM spend is wasted.
Solution
Add a
retry_with_backoff()helper in a newsrc/standard/retry.pymodule that retries on transient errors (429, 502, 503, 504, timeout) with exponential backoff (1s → 2s → 4s by default).chat_completions()inllm_client.pyandniutrans_translate()intranslators.pytests/test_retry.pyFiles changed
src/standard/retry.py(new) —retry_with_backoff()helper +TRANSIENT_STATUSsetsrc/standard/llm_client.py— wraphttpx.post()callsrc/standard/translators.py— wrap Niutranshttpx.post()calltests/test_retry.py(new) — 6 unit tests covering success, transient retry, client error, timeout, and exhaustion