Taming LangChain DeepAgents: Tool Selector and Todo List Middleware Explained
This article walks through LangChain DeepAgents' Tool Selector and Todo List middleware, detailing why they are needed, how they streamline tool usage and task planning, and provides step‑by‑step Python examples that demonstrate improved performance on complex multi‑tool tasks.
Tool Selector Middleware
When an agent has many tools, only a few are relevant per task; irrelevant tools waste tokens and can introduce noise. The Tool Selector middleware overrides wrap_model_call, pre‑filters the full tool list based on the current conversation and user query, and passes only the most relevant subset to the model.
LangChain DeepAgents provides LLMToolSelectorMiddleware to implement this.
Usage
Install langchain>=1.0.5 and Python ≥ 3.12. Import dependencies and create a DeepSeek model.
Define example tools and a real calculate tool.
Create the agent with the middleware, specifying max_tools=2 and an always_include list.
Invoke a query such as “请计算2+3*4的值”. The agent selects the calculate tool despite the large tool list.
Todo List Middleware
For multi‑step tasks that span several tools, the agent needs to track progress and avoid forgetting completed steps. The Todo List middleware injects a write_todo tool that generates a structured sub‑task list and stores it in the agent’s persistent todos field.
When a user submits a complex request, the model first decides whether decomposition is needed. If so, it calls write_todo, which creates sub‑tasks with statuses pending, progress, or completed. The agent processes the list sequentially, updating each task’s status until all are completed. The final response includes both messages and the enriched todos field.
Usage
Import TodoListMiddleware and create the model.
Create the agent; the middleware automatically injects the write_todo tool.
Invoke a complex query. The response contains a todos field where each entry has content and status. Because invoke is synchronous, all sub‑tasks finish before the result is returned, so their status appears as completed.
The todos field lists sub‑tasks with three possible statuses: pending – not yet executed. progress – currently executing. completed – finished.
When using synchronous invoke, all tasks complete before the response, so statuses are completed. Streaming output can reveal dynamic status changes.
Summary
The Tool Selector middleware reduces token consumption and improves decision accuracy by pre‑filtering tools. The Todo List middleware adds automatic task decomposition and state tracking for complex multi‑step workflows, enabling agents to handle sophisticated tasks efficiently.
Fun with Large Models
Master's graduate from Beijing Institute of Technology, published four top‑journal papers, previously worked as a developer at ByteDance and Alibaba. Currently researching large models at a major state‑owned enterprise. Committed to sharing concise, practical AI large‑model development experience, believing that AI large models will become as essential as PCs in the future. Let's start experimenting now!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
