# Recommended configuration for ModelRetryMiddleware: rationale and trade-offs

**URL:** <https://forum.langchain.com/t/recommended-configuration-for-modelretrymiddleware-rationale-and-trade-offs/4972>\
**Category:** LangChain\
**Tags:** python-help\
**Created:** [October 29, 2026, 12:12pm UTC](https://forum.langchain.com/t/recommended-configuration-for-modelretrymiddleware-rationale-and-trade-offs/4972 "2026-10-29T12:12:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdullah](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Abdullah](https://forum.langchain.com/u/Abdullah)\
**Post date:** [October 29, 2026, 12:12pm UTC](https://forum.langchain.com/t/recommended-configuration-for-modelretrymiddleware-rationale-and-trade-offs/4972/1 "2026-10-29T12:12:12Z")

</div>

**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**

1. How many retries (max\_retries) is reasonable, and what drove your choice: provider reliability, user latency, or cost?
2. initial\_delay, backoff\_factor, max\_delay and jitter: should keep the defaults, or tune them?
3. retry\_on: which exceptions do we include or exclude?
4. on\_failure: should we let the error propagate, or return it to the model? Why?
5. How to test your retry settings before production?

Any recommendations, links to docs or examples of settings that worked or failed would be appreciated.

---

<div class="post-metadata">

**Author:** ![Steve](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/steve/32/2619_2.png) [@Steve](https://forum.langchain.com/u/Steve)\
**Post date:** [October 30, 2026, 3:24am UTC](https://forum.langchain.com/t/recommended-configuration-for-modelretrymiddleware-rationale-and-trade-offs/4972/2 "2026-10-30T03:24:20Z")

</div>

Hi @Abdullah,

Here’s a quick rundown:

1. **`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.
2. **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.
3. **`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](https://docs.langchain.com/oss/python/langchain/models#model-exceptions) for the full list.
4. **`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.

```python
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"),
    ],
)

```

1. **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](https://docs.langchain.com/oss/python/langchain/test/unit-testing).

Docs: [Model retry middleware](https://docs.langchain.com/oss/python/langchain/middleware/built-in#model-retry)
