LLMToolSelectorMiddleware Class — langchain Architecture
Architecture documentation for the LLMToolSelectorMiddleware class in tool_selection.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 888c953d_8f6f_ac7f_4274_ca04589e186a["LLMToolSelectorMiddleware"] 2769f2d0_e2a8_5ab6_dc35_d5d0d6406ac4["HumanMessage"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|extends| 2769f2d0_e2a8_5ab6_dc35_d5d0d6406ac4 740a7da1_ca4f_8c85_71e0_4f92696f1237["tool_selection.py"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|defined in| 740a7da1_ca4f_8c85_71e0_4f92696f1237 74e81f5f_ce46_4e5d_763f_ed260d0dbec3["__init__()"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|method| 74e81f5f_ce46_4e5d_763f_ed260d0dbec3 bdf758af_80be_0125_ef7b_855624a8a30a["_prepare_selection_request()"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|method| bdf758af_80be_0125_ef7b_855624a8a30a 963a6e36_74b3_e68c_c642_f81d47a7666b["_process_selection_response()"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|method| 963a6e36_74b3_e68c_c642_f81d47a7666b 79ce050f_9134_368b_2f3c_59dd0f33524c["wrap_model_call()"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|method| 79ce050f_9134_368b_2f3c_59dd0f33524c 914b6a96_704c_ef23_6236_b6d24e64245f["awrap_model_call()"] 888c953d_8f6f_ac7f_4274_ca04589e186a -->|method| 914b6a96_704c_ef23_6236_b6d24e64245f
Relationship Graph
Source Code
libs/langchain_v1/langchain/agents/middleware/tool_selection.py lines 93–358
class LLMToolSelectorMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]):
"""Uses an LLM to select relevant tools before calling the main model.
When an agent has many tools available, this middleware filters them down
to only the most relevant ones for the user's query. This reduces token usage
and helps the main model focus on the right tools.
Examples:
!!! example "Limit to 3 tools"
```python
from langchain.agents.middleware import LLMToolSelectorMiddleware
middleware = LLMToolSelectorMiddleware(max_tools=3)
agent = create_agent(
model="openai:gpt-4o",
tools=[tool1, tool2, tool3, tool4, tool5],
middleware=[middleware],
)
```
!!! example "Use a smaller model for selection"
```python
middleware = LLMToolSelectorMiddleware(model="openai:gpt-4o-mini", max_tools=2)
```
"""
def __init__(
self,
*,
model: str | BaseChatModel | None = None,
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
max_tools: int | None = None,
always_include: list[str] | None = None,
) -> None:
"""Initialize the tool selector.
Args:
model: Model to use for selection.
If not provided, uses the agent's main model.
Can be a model identifier string or `BaseChatModel` instance.
system_prompt: Instructions for the selection model.
max_tools: Maximum number of tools to select.
If the model selects more, only the first `max_tools` will be used.
If not specified, there is no limit.
always_include: Tool names to always include regardless of selection.
These do not count against the `max_tools` limit.
"""
super().__init__()
self.system_prompt = system_prompt
self.max_tools = max_tools
self.always_include = always_include or []
if isinstance(model, (BaseChatModel, type(None))):
self.model: BaseChatModel | None = model
else:
self.model = init_chat_model(model)
def _prepare_selection_request(
self, request: ModelRequest[ContextT]
) -> _SelectionRequest | None:
"""Prepare inputs for tool selection.
Args:
request: the model request.
Returns:
`SelectionRequest` with prepared inputs, or `None` if no selection is
needed.
Raises:
ValueError: If tools in `always_include` are not found in the request.
AssertionError: If no user message is found in the request messages.
"""
Extends
Source
Frequently Asked Questions
What is the LLMToolSelectorMiddleware class?
LLMToolSelectorMiddleware is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/tool_selection.py.
Where is LLMToolSelectorMiddleware defined?
LLMToolSelectorMiddleware is defined in libs/langchain_v1/langchain/agents/middleware/tool_selection.py at line 93.
What does LLMToolSelectorMiddleware extend?
LLMToolSelectorMiddleware extends HumanMessage.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free