langchain · difficulty ◆◆◆
ToolRetryMiddleware: Honouring Interrupts Instead of Swallowing Them
Revoke tool calls mid-retry and the agent actually stops
Cancelled tool calls kept running in the background while the agent ignored you.
$ pip install -U langchain>=1.3.14What it does
ToolRetryMiddleware automatically retries tools after transient errors like rate limits or timeouts. The 1.3.12 fix ensures that when a tool invocation is interrupted (for example the user cancels a running agent), the interrupt signal propagates through the retry middleware instead of being swallowed. Previously an interrupt during a retry loop could silently continue instead of halting the agent.
Why it matters
If you have built an agent that calls tools in a loop and noticed it ignoring your cancel signal, this is why. In production it means agents using ToolRetryMiddleware can now be truly interrupted, which is critical for user-facing apps. It also fixes potential resource leaks where cancelled tool calls could keep running in the background.
Example
$ Run a tool-calling agent with a flaky tool and confirm the retry count increments until success.[chain/start] Entering chain run with input: ‘use the flaky tool’\n[tool/error] attempts: 1/5, error: Transient failure - retry me\n[tool/error] attempts: 2/5, error: Transient failure - retry me\n[tool/end] Success after 3 attempts: hello\nResult: The flaky tool returned "Success after 3 attempts: hello"Raise KeyboardInterrupt on attempt 2 to see the interrupt propagate immediately with the fix.
Common flags
- ToolRetryMiddleware
- Wraps tool calls with retry logic and exponential backoff
- create_tool_calling_agent
- Factory building a tool-calling agent
- ToolInterface
- Base class all tools extend; exposes invoke, run, retry_policy
- AgentExecutor
- Runs the agent loop until a final answer
History
Origin
A cancel signal during a retry loop was swallowed, so the agent kept retrying and cancelled tool calls could leak in the background.
The fix
PR #38722, the interrupt propagation fix, made the middleware re-raise interrupts immediately instead of treating them as retryable errors.
Fun facts
Pros & cons
pros
- + True responsiveness on cancel
- + Prevents resource leaks
- + Works with tool-calling agents
cons
- − Only affects retry middleware
- − Needs an upgrade to take effect
Takeaways
- 1Interrupts now propagate through retries
- 2Raise Interrupt to test
- 3Prevent runaway background calls