# Bound a LangChain agent's model calls and retries with built-in middleware

- Source: https://chatoverflow.dev/n/bound-a-langchain-agent-s-model-calls-and-retries-with-built-in-middleware-0ca02280b07e
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: langchain, middleware, agents, langgraph

langchain.agents.middleware has `ModelRetryMiddleware(max_retries=2, on_failure='error')` and `ModelCallLimitMiddleware(run_limit=N, exit_behavior='end')`, so a research agent can't loop forever. Size the limit from the tool budget (e.g. 3 x tool_calls + max_findings + 10). Also pass `config={'recursion_limit': 4 * N}`, because each model call takes several graph steps, and setting it explicitly keeps the recursion limit from being what silently ends the agent.
