Build a custom Toolkit in LangChain by subclassing BaseToolkit and implementing the abstract get_tools method to return a list of related BaseTool instances, providing a cohesive interface for grouping tools by functionality.
In LangChain, a Toolkit is a powerful abstraction for grouping a cohesive set of related tools under a single interface. This is particularly useful when you have multiple tools that operate on the same domain (e.g., a database, a CRM, or a file system) and you want to present them as a unified package to an agent. The BaseToolkit class provides the blueprint for this pattern, and building one involves creating your own subclass that implements the required get_tools method.
Subclass BaseToolkit: Create a new class that inherits from langchain_community.agent_toolkits.base.BaseToolkit. This gives your toolkit the foundational structure and integrates with LangChain's tooling ecosystem. BaseToolkit itself is an abstract base class (ABC), so you are required to implement its abstract methods.
Implement get_tools: Override the abstract get_tools method. This method is the core of your toolkit and must return a list of BaseTool objects. These are the individual tools that your toolkit groups together (e.g., ReadFileTool, WriteFileTool, ListDirectoryTool).
Initialize Tools: Inside get_tools, instantiate and configure your specific tools. This is where you can pass runtime dependencies like API keys, database connections, or configuration objects to the tools, keeping them decoupled from the toolkit's creation logic.
Creating the Agent: Once you have your toolkit, you create an instance and call its get_tools() method to retrieve the list of tools. This list can be passed directly to agent constructors like create_react_agent or create_agent.
Modular Architecture: The toolkit pattern promotes clean code organization. It allows you to encapsulate the logic for creating a set of related tools, making it easy to manage, test, and reuse across different agents or projects. It also provides a single import path for consumers of your toolkit.
Configuration and Dependencies: The __init__ method of your toolkit is the ideal place to accept configuration (like API keys, endpoints, or client objects). This config can then be passed down to individual tools during the get_tools() call, ensuring all tools are correctly set up without each consumer having to configure them individually.