Home / File/ tool_retry.py — langchain Source File

tool_retry.py — langchain Source File

Architecture documentation for tool_retry.py, a python file in the langchain codebase. 10 imports, 0 dependents.

File python CoreAbstractions RunnableInterface 10 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  103f7b48_ea3a_04f0_2d36_102935615884["tool_retry.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> a327e534_84f6_5308_58ca_5727d5eda0cf
  0c1d9a1b_c553_0388_dbc1_58af49567aa2["time"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 0c1d9a1b_c553_0388_dbc1_58af49567aa2
  0c635125_6987_b8b3_7ff7_d60249aecde7["warnings"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 0c635125_6987_b8b3_7ff7_d60249aecde7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> d758344f_537f_649e_f467_b9d7442e86df
  7d6aee3c_3a42_328c_cd7f_a72407a5159d["langchain.agents.middleware._retry"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 7d6aee3c_3a42_328c_cd7f_a72407a5159d
  50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 50acc543_e5f0_2162_cf07_c2bf50723e0c
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  0e93d2ee_1fb2_3a0f_5a76_8aba32d0d4ed["langgraph.types"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> 0e93d2ee_1fb2_3a0f_5a76_8aba32d0d4ed
  e85266a5_b172_4ddd_2fa6_e91b38ae8c0e["langchain.tools"]
  103f7b48_ea3a_04f0_2d36_102935615884 --> e85266a5_b172_4ddd_2fa6_e91b38ae8c0e
  style 103f7b48_ea3a_04f0_2d36_102935615884 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Tool retry middleware for agents."""

from __future__ import annotations

import asyncio
import time
import warnings
from typing import TYPE_CHECKING, Any

from langchain_core.messages import ToolMessage

from langchain.agents.middleware._retry import (
    OnFailure,
    RetryOn,
    calculate_delay,
    should_retry_exception,
    validate_retry_params,
)
from langchain.agents.middleware.types import AgentMiddleware, AgentState, ContextT, ResponseT

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable

    from langgraph.types import Command

    from langchain.agents.middleware.types import ToolCallRequest
    from langchain.tools import BaseTool


class ToolRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]):
    """Middleware that automatically retries failed tool calls with configurable backoff.

    Supports retrying on specific exceptions and exponential backoff.

    Examples:
        !!! example "Basic usage with default settings (2 retries, exponential backoff)"

            ```python
            from langchain.agents import create_agent
            from langchain.agents.middleware import ToolRetryMiddleware

            agent = create_agent(model, tools=[search_tool], middleware=[ToolRetryMiddleware()])
            ```

        !!! example "Retry specific exceptions only"

            ```python
            from requests.exceptions import RequestException, Timeout

            retry = ToolRetryMiddleware(
                max_retries=4,
                retry_on=(RequestException, Timeout),
                backoff_factor=1.5,
            )
            ```

        !!! example "Custom exception filtering"

            ```python
            from requests.exceptions import HTTPError
// ... (344 more lines)

Subdomains

Functions

Dependencies

  • asyncio
  • collections.abc
  • langchain.agents.middleware._retry
  • langchain.agents.middleware.types
  • langchain.tools
  • langchain_core.messages
  • langgraph.types
  • time
  • typing
  • warnings

Frequently Asked Questions

What does tool_retry.py do?
tool_retry.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in tool_retry.py?
tool_retry.py defines 1 function(s): collections.
What does tool_retry.py depend on?
tool_retry.py imports 10 module(s): asyncio, collections.abc, langchain.agents.middleware._retry, langchain.agents.middleware.types, langchain.tools, langchain_core.messages, langgraph.types, time, and 2 more.
Where is tool_retry.py in the architecture?
tool_retry.py is located at libs/langchain_v1/langchain/agents/middleware/tool_retry.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain_v1/langchain/agents/middleware).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free