fix(compiler): retry transient LLM API timeouts with bounded backoff - #230
Open
sebastianbraun25 wants to merge 2 commits into
Open
fix(compiler): retry transient LLM API timeouts with bounded backoff#230sebastianbraun25 wants to merge 2 commits into
sebastianbraun25 wants to merge 2 commits into
Conversation
added 2 commits
August 27, 2026 15:27
- Added retryable exception classifier (_should_retry_exception) - Modified _llm_call() to pass retries=2 to litellm.completion() - Modified _llm_call_async() to pass retries=2 to litellm.acompletion() - LiteLLM handles exponential backoff (base 2) internally - Retries transient errors (Timeout, RateLimitError, ConnectionError) - Skips retry for permanent errors (ValueError, Auth, BadRequest) - Added comprehensive unit tests for exception filtering logic Fixes VectifyAI#229
The retry mechanism used kwargs.setdefault('retries', 2), but LiteLLM only
recognizes 'num_retries'/'max_retries' as internal retry-control parameters.
An unrecognized 'retries' kwarg falls through as a provider request-body
field, which strict-mode proxies (e.g. custom Anthropic gateways) reject
with 'retries: Extra inputs are not permitted'.
Refines VectifyAI#229
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
During batch document ingestion (
openkb add), ~15-20% of planned concepts and entities fail to generate with transient timeout errors:This occurs under sustained high-concurrency load (5 parallel requests over ~150 seconds) when multiple documents trigger concept/entity generation in the same batch run. Despite the error, the document is marked
[OK]and added with incomplete concept/entity coverage, reducing knowledge base quality.Root Cause
The Anthropic API gateway experiences overload after ~6-7 concurrent tasks sustained for 150+ seconds. This is a transient, recoverable server-side phenomenon (not a client-side timeout misconfiguration). The same request succeeds on retry because the gateway recovers within exponential backoff windows (2-4 seconds).
Solution
Implemented hybrid retry mechanism combining LiteLLM's built-in retry capability with selective exception filtering:
Timeout/Gateway TimeoutRateLimitError/429 Too Many RequestsConnectionErrorServiceUnavailableError/5xxAPI errorsValueError,TypeError(client-side bugs)AuthenticationError,BadRequestError(permanent auth/validation failures)TruncatedResponseError(output already truncated; retry won't fix it)Changes
Added
_should_retry_exception()function (~60 LOC)Modified
_llm_call()function (~30 LOC changes)num_retries=2kwarg tolitellm.completion()Modified
_llm_call_async()function (~30 LOC changes)num_retries=2kwarg tolitellm.acompletion()Added comprehensive unit tests (17 test cases in
tests/test_compiler_retry.py)num_retries(notretries) is forwarded tolitellm.completion/acompletion, and that an explicit caller-suppliednum_retriesisn't overriddenBehavior
"LLM [X] failed with transient error (retries applied by litellm)""LLM [X] failed with permanent error (no retry)"+ tracebackConfiguration
No configuration changes required. Retry behavior is fixed:
Testing
Expected Impact
Update: fixed wrong LiteLLM kwarg name (
retries→num_retries)The initial implementation passed
retries=2tolitellm.completion()/acompletion(). LiteLLM does not recognize a bareretrieskwarg as an internal control parameter (onlynum_retries/max_retries) — it silently falls through as an unrecognized provider request-body field. Against most providers this is harmlessly dropped, but strict-mode proxies (e.g. custom enterprise Anthropic gateways) reject the request outright:This effectively broke every LLM call (and therefore every
add) for anyone behind such a proxy — a regression more severe than the original timeout issue this PR set out to fix. Renamed tonum_retriesin both_llm_call()and_llm_call_async(), and added regression tests asserting the exact kwarg forwarded tolitellm.completion/acompletion.Issues