Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(rate-limiting): use the median index for the adaptive base wait (#5201)

Ehsan1975fe247a93
diff --git a/changelog.d/+adaptive-ratelimit-median-off-by-one.bugfix.md b/changelog.d/+adaptive-ratelimit-median-off-by-one.bugfix.md
new file mode 100644
index 0000000000..bbbe39d395
--- /dev/null
+++ b/changelog.d/+adaptive-ratelimit-median-off-by-one.bugfix.md
@@ -0,0 +1 @@
+**Adaptive rate limiter learned a below-median base wait.** `AdaptiveRateLimitTracker._update_estimate` documents its base estimate as the "50th percentile (median) of successful waits", but the index carried a trailing `- 1` left over from an earlier 25th-percentile implementation: `max(0, int(len(successful_waits) * 0.50) - 1)`. For an odd number of successful waits that index falls below the median, and at `n == 3` (the minimum qualifying sample, since the method returns early below three attempts, so the first estimate update typically runs on exactly three) it selected index 0, the fastest observed wait, not the median. The learned base was therefore systematically shorter than the stated intent on odd samples, so the limiter backed off less than designed and hit rate limits sooner. The index is now `(len(successful_waits) - 1) // 2`, the median for odd n and the lower of the two middle waits for even n (matching the prior behavior for even samples).
diff --git a/src/local_deep_research/web_search_engines/rate_limiting/tracker.py b/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
index 0390da3f7e..067cd38504 100644
--- a/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
+++ b/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
@@ -418,10 +418,13 @@ class AdaptiveRateLimitTracker:
# Use 50th percentile (median) of successful waits for more stability
# This provides a balanced approach between speed and reliability
successful_waits.sort()
- percentile_50 = successful_waits[
- max(0, int(len(successful_waits) * 0.50) - 1)
- ]
- new_base = percentile_50
+ # Lower-median index: `(n - 1) // 2` is the middle element for odd n
+ # and the lower of the two middles for even n. The prior
+ # `int(n * 0.50) - 1` (left from a 25th-percentile version) sat below
+ # the median for odd n: at n == 3, the minimum qualifying sample, it
+ # picked index 0, the fastest wait, not the median.
+ median_index = (len(successful_waits) - 1) // 2
+ new_base = successful_waits[median_index]
# Update estimate with learning rate (exponential moving average)
if old_estimate is not None:
diff --git a/tests/web_search_engines/rate_limiting/test_tracker_high_value.py b/tests/web_search_engines/rate_limiting/test_tracker_high_value.py
index f15bb1e50b..5448b2d835 100644
--- a/tests/web_search_engines/rate_limiting/test_tracker_high_value.py
+++ b/tests/web_search_engines/rate_limiting/test_tracker_high_value.py
@@ -297,8 +297,37 @@ class TestUpdateEstimate:
tracker._update_estimate("Eng")
est = tracker.current_estimates["Eng"]
- # Sorted successes: [0.5, 1.0, 1.5], index = max(0, int(3*0.5)-1) = 0 -> 0.5
- assert est["base"] == 0.5
+ # Sorted successes: [0.5, 1.0, 1.5]; the failed 5.0 is excluded.
+ # Nearest-rank 50th percentile index int(3*0.5) == 1 -> 1.0 (the median).
+ assert est["base"] == 1.0
+
+ def test_first_estimate_uses_median_not_minimum(self):
+ """The first estimate (old_estimate is None, so no EMA blend) must be the
+ median of the successful waits, not a below-median element.
+
+ Regression for the off-by-one in the percentile index: the trailing
+ `- 1` (left over from a 25th-percentile version) biased the index below
+ the median for every n >= 3, and at n == 3 (the minimum qualifying
+ sample) it returned index 0, the fastest wait, making the learned base
+ systematically shorter than the code's stated 50th-percentile intent.
+ """
+ # (waits, expected nearest-rank 50th-percentile element)
+ cases = [
+ ([0.5, 1.0, 1.5], 1.0), # n=3: median, not the 0.5 minimum
+ ([1.0, 2.0, 3.0, 4.0, 5.0], 3.0), # n=5: median
+ ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 4.0), # n=7: median
+ ([1.0, 2.0, 3.0, 4.0], 2.0), # even n: lower of the two middles
+ ]
+ for waits, expected in cases:
+ tracker = _make_tracker()
+ tracker.recent_attempts["Eng"] = deque(
+ [_attempt(w, True) for w in waits], maxlen=100
+ )
+ with patch(f"{MODULE}.get_settings_context", return_value=None):
+ tracker._update_estimate("Eng")
+ # old_estimate is None on the first update, so base is the raw
+ # percentile element with no EMA smoothing.
+ assert tracker.current_estimates["Eng"]["base"] == expected, waits
def test_ema_blends_old_and_new(self):
"""EMA formula: new = (1-lr) * old + lr * computed."""