The situation that fits middleware

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 Core agent loop diagram

Middleware在其中的作用: exposes hooks before and after each of these steps

Middleware flow diagram

Use middleware inside a LangGraph workflow

将整个agent作为更大的StateGraph的子图或者节点 适用场景: 当拓扑结构不再是简单的循环直到结束时 例如:路由到多个agents之一并且对输入进行分类,并发工作,或者通过确定性步骤将agent调用串联起来

HumanInTheLoopMiddleware通过 tool’s name匹配 @tool修饰符从函数名获取器名称