AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Retry Jitter Can Cause Delays To Exceed Max_delay

In LangChain's retry middleware, calculate_delay applies the max_delay cap before adding ±25% jitter, allowing the final delay to reach up to max_delay * 1.25. This causes unexpected longer waits than configured.

highConfidence 98%LangchainAffected V1.4.0

Origin Analysis

calculate_delay clamps the base delay to max_delay before applying jitter, but after jitter it only enforces the lower bound (non-negative) and does not re-clamp to max_delay.
1. Patch random.uniform to return the upper bound of its interval. 2. Call calculate_delay with backoff_factor=0.0, initial_delay=10.0, max_delay=10.0, jitter=True. 3. The returned delay is 12.5, exceeding max_delay. Example: from unittest.mock import patch from langchain.agents.middleware._retry import calculate_delay with patch("langchain.agents.middleware._retry.random.uniform", side_effect=lambda _lower, upper: upper): delay = calculate_delay(0, backoff_factor=0.0, initial_delay=10.0, max_delay=10.0, jitter=True) print(delay) # 12.5 assert delay <= 10.0

Fixing Code Block

Edge Case Audit

The fix is backward-compatible and safe for typical use. It may change behavior for users who unintentionally relied on delays exceeding max_delay, but such reliance is unlikely and contradicts documentation. No rollback needed; ensure tests cover boundary cases.

Ecosystem Topology