[Bug] LLM retry loop retries non-retryable errors and defers fallback to the final attempt
Summary
The LLM retry loop in src/llm/api.py treats every exception as retryable and only reaches the configured fallback provider on the final attempt. Together these mean a permanently-failing request (bad API key, malformed request, context-length overflow) costs three provider round-trips plus ~8s of backoff before it surfaces, and a genuinely transient failure waits out two doomed primary attempts before the fallback is tried.
Both are pre-existing; neither is a regression.
Defect 1 — no retry predicate
src/llm/api.py:281-286:
if enable_retry:
decorated = retry(
stop=stop_after_attempt(retry_attempts),
wait=wait_exponential(multiplier=1, min=4, max=10),
before_sleep=before_retry_callback,
)(decorated)There is no retry= argument, so tenacity falls back to retry_if_exception_type(Exception) — everything is retried. Nothing upstream filters: src/llm/executor.py wraps provider calls in except BaseException blocks that emit telemetry and re-raise (lines 516, 537, 582), and the backends don't classify either. A provider SDK's AuthenticationError or BadRequestError reaches tenacity looking exactly like a RateLimitError.
With stop_after_attempt(3) there are two backoff sleeps, each clamped to the 4s floor, so a 401 takes ~8s of pure sleep plus 3 HTTP round-trips before the caller sees it. On the Dialectic path that is added latency on a synchronous request.
(asyncio.CancelledError is a BaseException, so client disconnects are already excluded. That part is fine.)
Defect 2 — fallback deferred to the final attempt
select_model_config_for_attempt in src/llm/runtime.py:
if attempt != retry_attempts or model_config.fallback is None:
return model_configThe fallback provider is only selected when attempt == retry_attempts. If the primary is down, we spend attempts 1 and 2 plus ~8s of backoff hitting it before trying the provider we configured precisely for this case.
Suggested fix
- Add a
retry=predicate to the tenacity decorator: retry on 429, 5xx, and timeout/connection errors; fail fast on everything else. This needs a small provider-agnostic classifier, since each backend SDK raises its own types —src/llm/executor.pyalready has_outcome_from_erroras a precedent for that kind of helper, though it only distinguishes success/error/cancelled and isn't reusable as-is. - Switch to the fallback on the first retryable failure rather than at
attempt == retry_attempts.
Both changes are confined to src/llm/api.py and src/llm/runtime.py.
Prior art
PR #736 (closed) implemented both, plus a retryable-error classifier and 26 tests. It was split out of #692 at our request, and closed unmerged alongside its siblings #737/#738 — 163 commits behind with conflicts, so not rebasable, but the diff is a reasonable starting reference. Credit to @phuongvm for the original diagnosis.
Note that #736's description claims to introduce per-agent cross-provider fallback; that part already landed separately in #459 (AttemptPlan, FallbackModelSettings, is_fallback telemetry). Only the two items above are actually missing.
Source: plastic-labs/honcho