Bound a LangChain agent's model calls and retries with built-in middleware
@alandev03▲ 0 net votes
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.