Mastering LangChain Tools: Built-in & Custom Made Easy

What Are LangChain Tools?

In LangChain, Tools are modular components that allow language models (LLMs) to interact with the outside world.

Think of them as functions that agents can call to perform specific tasks — like searching the web, making API calls, executing code, or querying a database.

In short, LangChain Tools turn passive LLMs into action-taking agents — capable of solving real-world problems dynamically.

Agents = LLM (the brain) + Tools (the hands)

LLM is the brain. Tools are the hands. Agents make them work together.

Use Cases of LangChain Tools

New to LangChain?
Don’t worry — I’ve got you covered! Start with these beginner-friendly blogs before diving into tools:


LangChain Tool Spotlight: WikipediaQueryRun

One of the most popular and beginner-friendly tools in LangChain is the WikipediaQueryRun tool.

WikipediaQueryRun allows LangChain agents to search and fetch content directly from Wikipedia — a powerful capability when the LLM needs factual or encyclopedic information.

It works by querying the Wikipedia API and returning a summary of the topic the agent is asking about.

  • top_k_results: Limits the number of results returned (useful for precision).
  • doc_content_chars_max: Restricts the length of the returned content (good for short summaries).
LangChain Tool Spotlight: YouTubeSearchTool

LangChain also supports searching YouTube directly through the YouTubeSearchTool, which allows agents to find relevant YouTube videos based on a search query.

The YouTubeSearchTool connects to YouTube’s search functionality (via unofficial APIs or scraping methods) and returns video results relevant to your query. This tool is great for:

  • Finding tutorials, lectures, or entertainment content
  • Gathering video content summaries
  • Enhancing multimedia AI assistants that can recommend videos
  • It returns relevant video titles, descriptions, and links, which can be fed back to your agent or application.

Building Custom LangChain Tools

While LangChain offers many powerful built-in tools, sometimes you need something tailored to your specific needs — whether it’s integrating with your company’s APIs, accessing internal databases, or handling unique workflows.

Custom tools let you extend LangChain’s functionality by defining your own “actions” that an agent can call.

Building a Custom LangChain Tool: Step-by-Step

Creating a custom tool in LangChain involves two simple steps:

Write a Regular Python Function

First, you write a standard Python function that performs the action you want. For example:

This function simply takes a name and returns a greeting message.

Add the @tool Decorator

Next, to make this function usable as a LangChain tool, you add the @tool decorator from langchain.tools. Also u need to add docstring. This wraps your function with additional functionality so it can be called by LangChain agents:

Invoke Your Custom Tool

Why use the @tool decorator?
  • It registers your function as a LangChain tool, enabling integration with agents.
  • It handles input parsing and output formatting for your function.
  • Makes your tool discoverable and callable in LangChain workflows.
Why Use a Docstring?
  • The docstring serves as a description for your tool.
  • It helps the agent understand when to use this tool.
  • Improves readability and maintenance of your code.
Another Example: Multiplication Tool

Here’s a practical example of a custom LangChain tool that performs multiplication of two numbers.

First, import the tool decorator from LangChain’s agents module. Then, define a function multiply that takes two integers as input and returns their product. Don’t forget to include a docstring describing the tool’s purpose — this description helps the LangChain agent understand when to use it.

You can invoke this tool using the .run() method with your desired input values:

This simple example demonstrates how easy it is to extend LangChain’s capabilities by building your own custom tools tailored to your specific needs.

Conclusion

LangChain tools empower developers to seamlessly integrate powerful language models with external functionalities, enhancing the capabilities of AI applications. By leveraging custom tools, decorators, and built-in utilities, you can create intelligent agents that perform complex tasks beyond simple text generation. Whether it’s automating workflows, retrieving information, or building interactive assistants, LangChain’s flexible tool framework makes it easier to build robust, scalable, and context-aware applications. Exploring and mastering these tools will unlock new possibilities in AI-driven development and enable you to deliver more dynamic and user-centric solutions.

Prem Kumar
Prem Kumar
Articles: 9

Leave a Reply

Your email address will not be published. Required fields are marked *