LangChain · classic handle_* flags + v1 middleware
ToolRetryMiddleware, ModelRetryMiddleware, SummarizationMiddleware, etc.How it works
Classic AgentExecutor. Three flags do most of the work.
handle_parsing_errors: bool | str | Callable (line 1042 of
agent.py) controls what happens when the output parser blows up —
set to True and the framework re-prompts the LLM with the parse
error attached. Each tool has Tool.handle_tool_error with the same
three-shape signature; on a tool exception the value (or the result of the
callable) is fed back to the LLM as the tool's "result" string. Outer guards
max_iterations (default 15) and max_execution_time
stop runaway loops.
Retry-on-bad-output parsers.
OutputFixingParser wraps a base parser and, on failure, calls a
"fixer" LLM with the original output and the parser error.
RetryOutputParser goes further, also passing the original prompt so
the LLM can correct itself in context.
v1 middleware stack. The new
langchain.agents.middleware package promotes each concern to its own
AgentMiddleware subclass — ToolRetryMiddleware (retries
with exponential backoff, default 2 retries, configurable
should_retry), ModelRetryMiddleware,
ModelFallbackMiddleware, SummarizationMiddleware,
ContextEditingMiddleware (with edit policies like
ClearToolUsesEdit).
Mode coverage
1 · Tool fails
Native. handle_tool_error on each Tool, plus ToolRetryMiddleware with backoff in v1.
2 · LLM incorrect
Native. handle_parsing_errors, OutputFixingParser, RetryOutputParser, ModelRetryMiddleware.
3 · Hallucination
No built-in detector. Schema validation in tool args is your best lever.
4 · Large context
trim_messages (in langchain_core.messages.utils), ConversationSummaryBufferMemory, v1 SummarizationMiddleware.
Auto-retry defaults
None at the executor level — you opt in by setting flags or adding middlewares. v1 ToolRetryMiddleware defaults: 2 retries · exponential backoff. OutputFixingParser retries once with the fixer LLM.
Code anchor
libs/langchain/langchain_classic/agents/agent.py · handle_parsing_errors (line 1042), max_iterations (line 1023)
libs/langchain/langchain_classic/output_parsers/{fix,retry}.py
libs/langchain_v1/langchain/agents/middleware/{tool_retry,model_retry,model_fallback,summarization,context_editing}.py