model_retry.py — langchain Source File
Architecture documentation for model_retry.py, a python file in the langchain codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8["model_retry.py"] e6310202_b39e_ec9f_8b57_921c9c39c97d["asyncio"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> e6310202_b39e_ec9f_8b57_921c9c39c97d 996b2db9_46dd_901f_f7eb_068bafab4b12["time"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> 996b2db9_46dd_901f_f7eb_068bafab4b12 feec1ec4_6917_867b_d228_b134d0ff8099["typing"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> feec1ec4_6917_867b_d228_b134d0ff8099 9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> 9444498b_8066_55c7_b3a2_1d90c4162a32 1d2dc1c1_d133_ba97_472a_386346d9e12e["langchain.agents.middleware._retry"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> 1d2dc1c1_d133_ba97_472a_386346d9e12e a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> a681398d_ed44_c914_1a44_5d174223b069 2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"] 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 --> 2bf6d401_816d_d011_3b05_a6114f55ff58 style 2343fe4b_5b36_43a8_e17c_329c9cc2cbd8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Model retry middleware for agents."""
from __future__ import annotations
import asyncio
import time
from typing import TYPE_CHECKING
from langchain_core.messages import AIMessage
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,
ModelRequest,
ModelResponse,
ResponseT,
)
if TYPE_CHECKING:
from collections.abc import Awaitable, Callable
class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]):
"""Middleware that automatically retries failed model 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 ModelRetryMiddleware
agent = create_agent(model, tools=[search_tool], middleware=[ModelRetryMiddleware()])
```
!!! example "Retry specific exceptions only"
```python
from anthropic import RateLimitError
from openai import APITimeoutError
retry = ModelRetryMiddleware(
max_retries=4,
retry_on=(APITimeoutError, RateLimitError),
backoff_factor=1.5,
)
```
!!! example "Custom exception filtering"
// ... (253 more lines)
Domain
Subdomains
Functions
Classes
Dependencies
- asyncio
- collections.abc
- langchain.agents.middleware._retry
- langchain.agents.middleware.types
- langchain_core.messages
- time
- typing
Source
Frequently Asked Questions
What does model_retry.py do?
model_retry.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, LanguageModelBase subdomain.
What functions are defined in model_retry.py?
model_retry.py defines 1 function(s): collections.
What does model_retry.py depend on?
model_retry.py imports 7 module(s): asyncio, collections.abc, langchain.agents.middleware._retry, langchain.agents.middleware.types, langchain_core.messages, time, typing.
Where is model_retry.py in the architecture?
model_retry.py is located at libs/langchain_v1/langchain/agents/middleware/model_retry.py (domain: LangChainCore, subdomain: LanguageModelBase, 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