Context. We’re building an agent with tool calls on a hosted model. We want to use ModelRetryMiddleware rather than hand written loops, but there are many parameters and the defaults aren’t explained in terms of cost or latency.
What we’re trying to decide
How many retries (max_retries) is reasonable, and what drove your choice: provider reliability, user latency, or cost?
initial_delay, backoff_factor, max_delay and jitter: should keep the defaults, or tune them?
retry_on: which exceptions do we include or exclude?
on_failure: should we let the error propagate, or return it to the model? Why?
How to test your retry settings before production?
Any recommendations, links to docs or examples of settings that worked or failed would be appreciated.
max_retries: The default is 2, which means 3 attempts in total. For interactive agents, 2–3 is a good starting point. User latency is usually the deciding factor, because every retry adds wait time and resends the full prompt. Chat models also have their own max_retries (provider SDK retries), and the two layers multiply. I’d keep one of them low.
Delays: The defaults are fine for most cases. With them you wait about 1s, then 2s, with ±25% jitter. If you mostly hit 429s, a larger initial_delay helps. Also set an explicit timeout on the model so failed attempts don’t hang.
retry_on: On langchain>=1.3.16 the default is a good baseline. It retries rate limits, timeouts, connection errors and 5xx, and skips non-retryable errors like auth failures, invalid requests and context overflow. See Model exceptions for the full list.
on_failure: With "continue", the error becomes an AIMessage that the agent returns as its final answer. The model doesn’t get a chance to recover. Use "error" if your app handles failures upstream or if you use ModelFallbackMiddleware, since fallback only triggers on an exception.
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRetryMiddleware
agent = create_agent(
model="gpt-5.5",
tools=[...],
middleware=[
ModelRetryMiddleware(max_retries=3, max_delay=20.0, on_failure="error"),
],
)
Testing: Put a small custom wrap_model_call middleware after the retry middleware that raises an exception for the first N calls. Run it against a fake model with initial_delay=0. That lets you check both the retry path and the on_failure path without real API calls. See unit testing.