Tools
Relevant source files
The following files were used as context for generating this wiki page:
- libs/core/langchain_core/tools/base.py
- libs/core/langchain_core/tools/structured.py
- libs/langchain/langchain/tools/ddg_search/tool.py
- libs/langchain/langchain/tools/python/tool.py
- libs/core/langchain_core/tools/convert.py
- libs/core/langchain_core/tools/render.py
- libs/langchain/langchain/agents/json_chat/base.py
- libs/langchain/langchain/agents/conversational_chat/prompt.py
- SECURITY.md
- libs/core/langchain_core/runnables/base.py
In LangChain, Tools are interfaces that an Agent, Chain, or LLM can use to interact with the external world. They provide a structured way to expose functions and services—such as search engines, APIs, databases, or code execution environments—to the language model. This allows the model to gather information or perform actions beyond its intrinsic knowledge, forming a crucial component of more complex and powerful applications like Agents and Retrieval Augmented Generation (RAG) systems.
Each tool is defined by its name, a description that the LLM uses to determine its purpose, and an args_schema that specifies the required input parameters. This structured definition enables the LLM to intelligently select the appropriate tool and provide the correct arguments to accomplish a given task. The framework provides base classes and helpers to create both simple and complex tools.
Sources: libs/core/langchain_core/tools/init.py:1-9, libs/langchain/README.md:25-32, libs/langchain/langchain/agents/conversational_chat/prompt.py:19-20
Core Concepts & Abstractions
The foundation of the Tool system is built upon a few key classes that define the interface and behavior of all tools.
BaseTool
The BaseTool class is the abstract base class for all tools. It inherits from RunnableSerializable, making it a standard component within the LangChain Expression Language (LCEL). Every tool must implement the _run method for synchronous execution and can optionally implement _arun for asynchronous execution.
Key attributes of BaseTool:
name(str): A unique identifier for the tool.description(str): A detailed explanation of what the tool does, which the LLM uses for selection.args_schema(pydantic.BaseModel): A Pydantic model defining the arguments the tool accepts. This is crucial for structured tools.return_direct(bool): IfTrue, the tool's output is returned directly to the user, bypassing the LLM.
The diagram below shows the class hierarchy for Tools.
This diagram illustrates that Tool and StructuredTool are concrete implementations of the abstract BaseTool, which itself is a RunnableSerializable.
Sources: libs/core/langchain_core/tools/base.py:27-219, libs/core/langchain_core/runnables/base.py:1270-1272
Tool vs. StructuredTool
LangChain provides two primary concrete implementations of BaseTool:
| Class | Description | Input | Use Case |
|---|---|---|---|
Tool | A wrapper for functions that accept a single string argument. It is a simpler tool for straightforward tasks. | A single string. | Quick tool creation for simple functions, like a basic search query. |
StructuredTool | A wrapper for functions that accept multiple, typed arguments. It uses a Pydantic model (args_schema) for robust input validation. | A dictionary of arguments conforming to args_schema. | Complex tools requiring multiple parameters, such as interacting with a detailed API endpoint. |
Sources: libs/core/langchain_core/tools/simple.py, libs/core/langchain_core/tools/structured.py:15-110
Creating Tools
The recommended way to create a tool is by using the @tool decorator, which simplifies the process of converting a Python function into a Tool or StructuredTool.
The @tool Decorator
The @tool decorator, found in langchain_core.tools.convert, automatically infers the tool's name, description, and args_schema from the function's definition, type hints, and docstring.
- If the function has a single string argument, it creates a
Tool. - If the function has multiple arguments or a Pydantic model as an argument, it creates a
StructuredTool.
The following flowchart shows the logic of the @tool decorator.
This flowchart shows how the @tool decorator creates either a Tool or a StructuredTool based on the decorated function's signature.
Here is an example of using the @tool decorator:
from langchain_core.tools import tool
@tool
def search_database(query: str) -> str:
"""Search the database for relevant information.
Args:
query: The search query string.
"""
# Implementation here
return "results"
Sources: libs/core/langchain_core/tools/convert.py:19-100, CLAUDE.md:214-225
Tool Usage in Agents
Tools are a fundamental component of agents. The agent's LLM is provided with a list of available tools and decides which one to use based on the user's input and the tool descriptions.
Tool Rendering
To make tools usable by an LLM, they must be "rendered" into a textual format that the model can understand. The render_text_description function formats a list of tools into a string, typically including each tool's name and description. This rendered string is then inserted into the agent's prompt.
Prompts for agents often contain placeholders like {{tools}} and {tool_names} which are populated with the rendered tool descriptions and a comma-separated list of tool names.
Sources: libs/core/langchain_core/tools/render.py:11-30, libs/langchain/langchain/agents/conversational_chat/prompt.py:19-23
Agent Execution Flow
When an agent receives a request, it follows a sequence to decide on and execute a tool.
The following diagram illustrates the interaction between a user, an agent, and a tool.
This sequence diagram shows how an agent uses an LLM to select a tool, executes it, and uses the result to formulate a final answer.
The create_json_chat_agent function is an example of how tools are integrated into an agent. It takes a list of BaseTool objects and makes them available to the LLM by rendering them into the prompt.
Sources: libs/langchain/langchain/agents/json_chat/base.py:22-25, libs/langchain/langchain/agents/conversational_chat/prompt.py:9-16
Security Considerations
Giving LLMs access to tools introduces security risks, as tools can interact with external systems, execute code, or access sensitive data. The SECURITY.md document outlines several best practices:
- Limit Permissions: Follow the principle of least privilege. Grant tools read-only credentials and restrict access to only necessary resources.
- Anticipate Potential Misuse: Assume that an LLM might use a tool in any way its permissions allow, including destructive actions if not properly restricted.
- Defense in Depth: Use multiple layers of security, such as sandboxing (e.g., running in a container) combined with limited permissions.
For example, tools like PythonREPLTool that execute code are considered high-risk. They have been moved to the langchain-experimental package and are not eligible for bug bounties to signify their experimental and potentially dangerous nature. Developers are expected to understand and mitigate the security implications of the tools they use.
Sources: SECURITY.md:3-28, libs/langchain/langchain/tools/init.py:23-40