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

Use middleware inside a LangGraph workflow
将整个agent作为更大的StateGraph的子图或者节点 适用场景: 当拓扑结构不再是简单的循环直到结束时 例如:路由到多个agents之一并且对输入进行分类,并发工作,或者通过确定性步骤将agent调用串联起来
HumanInTheLoopMiddleware通过 tool’s name匹配
@tool修饰符从函数名获取器名称