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%Langchainlangchain-coreAgentsChat ModelsAffected 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

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state