fix(llms): don't stack the sdk's retries on rate-limit replays #8333
All Vouch review confidence checks passed
Residual risk 0.35 = Change Risk (0.64) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.64 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #8333"
general
weight: 0.35
simin-yuan
Prepared with an AI agent. Per CONTRIBUTING this should carry the
llm-generatedlabel, which I don't have permission to apply.Related issue
Closes #8332
Summary
run_with_rate_limit_retry/arun_with_rate_limit_retrynow record whether the attempt they are aboutto run is a replay of a call that already failed (
_llm_retry_replaycontextvar, exposed asin_rate_limit_retry_replay()), and the OpenAI provider uses that to build its client:All nine provider call sites in
lib/crewai/src/crewai/llms/providers/openai/completion.pygo through_client_for_attempt(...): 4responses.create, 3chat.completions.create, and 2chat.completions.with_raw_response.create(in_handle_completionand_ahandle_completion, i.e. thenon-streaming path).
Why: the two layers multiply today.
max_retriesdefaults to2atcompletion.py:376, so the SDK makesup to 3 HTTP attempts per call; CrewAI then retries the same call up to 3 times (
retry.py:35, wrapperinstalled at
base_llm.py:201/:215). Measured oncrewai==1.15.28against a server that always answers429: 27 byte-identical requests for one call(https://github.com/simin-yuan/agent-double-exec-bench,
results/e1/crewai/429/server.jsonl; the SDK'sx-stainless-retry-countcycles 0→1→2 nine times).Measured, before and after
Same harness, same fault server, CrewAI default configuration,
Retry-After: 1on the 429 cell. "Before"is base commit
dec075b5, "after" is this branch head:| cell | before | after |
|---|---|---|
| 429 (
Retry-After: 1) | 27 chat requests (x-stainless-retry-count0 ×9, 1 ×9, 2 ×9) | 15 (0 ×9, 1 ×3, 2 ×3) || 500-neutral | 9 chat requests (0 ×3, 1 ×3, 2 ×3) | 9 (unchanged) |
The 15 is counted in the server log, not derived: the first attempt still spends the SDK's three attempts
(that is the cheap way to ride out a one-off blip), and the two replays CrewAI makes now spend one request
each — 3 rounds × (3 + 1 + 1). The 500 cell is unchanged because CrewAI never replays a 500 at the model
layer, so there is no replay for the SDK's retries to stack on.
Logs:
results/e3/crewai-before/andresults/e3/crewai-after/inhttps://github.com/simin-yuan/agent-double-exec-bench (
429and500-neutralcells,server.jsonl;chat/completionslines only).Verification
lib/crewai/tests/llms/test_retry.py, six new tests (eight cases):| test | before the change | after |
|---|---|---|
|
test_replays_of_a_throttled_call_are_marked_as_replays| fails — sees[False, False]| passes ||
test_async_replays_of_a_throttled_call_are_marked_as_replays| fails — sees[False, False]| passes ||
test_the_replay_flag_is_off_outside_a_retry| fails at import | passes ||
test_openai_provider_turns_sdk_retries_off_on_replay| fails at import | passes ||
test_non_streaming_chat_keeps_sdk_retries_for_the_first_attempt_only[True]| fails — the replay still carriesmax_retries=2| passes ||
test_async_non_streaming_chat_keeps_sdk_retries_for_the_first_attempt_only[True]| fails — the replay still carriesmax_retries=2| passes |The non-streaming pair also runs with
[False](first-attempt case) and passes in both directions — thathalf is the control: the patch must not touch the first attempt. With only
completion.pyreverted to itsparent version, the run is
2 failed, 2 passed: the two replay cases fail, the two controls pass.The replay tests drive the real code path (
BaseLLM.__init_subclass__wrapper → retry loop) and assert thesequence
[False, True]; the provider test asserts the client is asked formax_retries=0on a replay andleft untouched otherwise.
Local runs (Windows, Python 3.12, venv with
uv sync --all-groupsplus the provider extras):The 18 and the 138 are environmental, and I checked that rather than assuming it: running those exact test
ids with
lib/checked out atdec075b5reproduces them 1:1 (18 failed, 14.95 s / 138 failed). The 11errors are collection errors for optional dependencies that are not installed locally (
a2a,qdrant).The whole-tree mypy run also needs the full dev extras; on the two files this PR touches it is clean under
the same strict configuration, unchanged from
dec075b5.On Windows the repo's
--block-networkaddopts blocks the local socket asyncio needs, which makes everyasync test — pre-existing ones included — error out at setup; the runs above pass
--allowed-hosts=127.0.0.1for that reason only. CI on Linux does not have this problem and is the number to trust.
Additional context
Deliberately not touched here:
Agent._times_executednever being reset (agent/core.py:242,:886-887) — real, but it is asemantics change for
max_retry_limit, so it is reported in the linked issue instead;_handle_execution_error→execute_task), which produces the 0.02 sre-sends;
generic (
in_rate_limit_retry_replay()), so they can opt in one at a time.---
Disclosure: I directed this work. The source reading, the patch, its tests and the measurements were
produced by AI assistants I operate. Every number above comes from the server logs in
agent-double-exec-bench, and I will answer review comments on it.
Per CONTRIBUTING this should carry the
llm-generatedlabel. Contributors can't set labels, so I asked forit in a comment below.
Related: #7771 handles Retry-After on SDK response objects; this PR changes a different part of the same
files (the SDK's own retries under CrewAI's rate-limit replays). The two do not touch the same lines.
<!-- CURSOR_SUMMARY -->
---
<!-- /CURSOR_SUMMARY -->