LangChainLangChain 1.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
46 small wins to finish your pathNext lesson →

More built-in middleware to reach for

Built-in middleware are ready-made wrappers you add to an agent for common needs, such as planning a task list, clearing old tool output, or pre-selecting tools, without writing the hook yourself.

Last updated: 27 Sep, 2026 · LangChain 1.4

You wrote a guardrail and used the retry, fallback, limit and summarization middleware. LangChain ships more, and you add each one the same way: in the agent's middleware list.

The middleware you have not met

MiddlewareWhat it doesReach for it when
TodoListMiddlewareGives the agent a write-todos tool to plan multi-step workA long task it should break into steps
ContextEditingMiddlewareClears old tool outputs as the context nears its limitAn agent that calls many tools in a run
LLMToolSelectorMiddlewareAsks a model to pre-pick the relevant tools each turnAn agent with a long list of tools
SummarizationMiddlewareSummarizes old turns to keep the window smallLong conversations (you used this earlier)

Confirming they are there to import

Each ships in langchain.agents.middleware. Import the ones you want and print their names to see they are available.

Example
from langchain.agents.middleware import (
    TodoListMiddleware,
    ContextEditingMiddleware,
    LLMToolSelectorMiddleware,
    SummarizationMiddleware,
)

for mw in (TodoListMiddleware, ContextEditingMiddleware,
           LLMToolSelectorMiddleware, SummarizationMiddleware):
    print(mw.__name__)

Adding one to an agent

Pass an instance in the middleware list, the same slot the earlier ones used. Here the agent gets a planning tool.

python
from langchain.agents import create_agent
from langchain.agents.middleware import TodoListMiddleware

agent = create_agent(
    model="openai:gpt-4.1",
    tools=[...],
    middleware=[TodoListMiddleware()],   # the agent can now plan with a todo list
)

What each one changes

  • TodoListMiddleware adds a tool, so the agent writes and updates a plan as it works.
  • ContextEditingMiddleware drops stale tool output, so a long run stays inside the window.
  • LLMToolSelectorMiddleware narrows the tool list per turn, so a big toolset stays cheap and accurate.

When to add one

  • The agent loses track of a multi-step job: add the todo list.
  • A run fills the context with tool output: add context editing.
  • The model picks the wrong tool from a long list: add the tool selector.
Watch out. Middleware run in the order you list them, and some, like the tool selector, make their own model call, so they add cost. Add the few you need, not all of them.
Try it yourself
  • Import PIIMiddleware and add it to the print loop.
  • Add TodoListMiddleware to an agent and give it a two-step task.
  • Read the docs' built-in middleware page and pick one more to try.

Little by little, you're building something great.