Custom Middleware

通过在agent工作流中实现hook来构建自定义的midddleware Hooks two types: Node-style hook 在特定的执行点顺序执行.Use for validation,logging and state updates. Hook When it runs before_agent Before agent starts (once per invocation) before_model Before each model call after_model After each model response after_agent After agent completes (once per invocation) Wrap-style hooks run around each call, giving you control over execution: ...

FrameworksLangChain
7 分钟3256 字chenghaoning51

Overview

The situation that fits middleware Tracking agent behavior with logging, analytics, and debugging. Transforming prompts, tool selection, and output formatting. Adding retries, fallbacks, and early termination logic. Applying rate limits, guardrails, and PII detection. The agent loop the core agent loop involves calling a model ,letting it choose tools to execute,and then finishing when it calls no more tools Middleware在其中的作用: exposes hooks before and after each of these steps ...

FrameworksLangChain
1 分钟246 字chenghaoning51

Prebuilt Middleware

Tool error 捕获工具异常,并且转化为ToolMessage,模型收到信息并且从中修复 API reference: ToolErrorMiddleware 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from langchain.agents import create_agent from langchain.agents.middleware import ToolErrorMiddleware, ToolRetryMiddleware def on_error(exc: Exception, request: ToolCallRequest) -> str | None: # Surface ValueError to the model so it can correct the input if isinstance(exc, ValueError): return f"`{request.tool_call['name']}` failed: {type(exc).__name__}. Fix the input and retry." # Let all other exceptions propagate (halts the run) return None # Async-only usage async def aon_error(exc: Exception, request: ToolCallRequest) -> str | None: if isinstance(exc, ConnectionError): return f"Tool `{request.tool_call['name']}` encountered a connection error." return None agent = create_agent( model="gpt-5.5", tools=[search_tool, database_tool], middleware=[ # Place retry inner so exceptions reach ToolErrorMiddleware after retries are exhausted ToolRetryMiddleware(max_retries=3, on_failure="error"), ToolErrorMiddleware(on_error=on_error, tools=["search_tool"]), ], ) # Async-only: pass aon_error alone (do not pass on_error) async_agent = create_agent( model="gpt-5.5", tools=[api_tool], middleware=[ToolErrorMiddleware(aon_error=aon_error)], ) Tool retry 通过可配置的指数避退机制来自动重试失败tool calls ...

FrameworksLangChain
3 分钟1373 字chenghaoning51