Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 Case | 💡 Description |
---|---|
🌐 Real-Time Web Search | Fetch up-to-date information using tools like DuckDuckGoSearchRun or WikipediaQueryRun . |
📊 Financial Data Fetching | Query stock prices, cryptocurrency values, or exchange rates via finance APIs. |
🌦️ Weather Forecasting | Use a custom API tool to get current and forecasted weather for any location. |
🧮 Scientific/Technical Calculations | Run dynamic calculations using PythonREPLTool for science, engineering, or data analysis. |
📁 Document-Based Q&A | Load and search through local or cloud-based documents to answer user queries (RAG pipelines). |
💬 Customer Support Integration | Fetch/update support tickets by connecting to platforms like Zendesk or Salesforce. |
🗃️ Internal Database Access | Query internal SQL/NoSQL databases for business metrics, logs, or customer info. |
🔄 Workflow Automation | Chain multiple tools (e.g., fetch data → summarize → send email) to automate tasks. |
📱 API Orchestration | Let agents interact with multiple APIs (e.g., Google Calendar + Notion + Slack). |
👩💼 Personal AI Assistant | Combine tools for news, reminders, weather, and more to build an intelligent assistant. |
New to LangChain?
Don’t worry — I’ve got you covered! Start with these beginner-friendly blogs before diving into tools:
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.
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
# Initialize the Wikipedia API wrapper
wiki_api = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=500)
# Wrap it in the tool interface
wikipedia_tool = WikipediaQueryRun(api_wrapper=wiki_api)
# Run the tool with a query
print(wikipedia_tool.run("LangChain"))
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).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:
from langchain_community.tools import YouTubeSearchTool
# Initialize the YouTube search tool
tool = YouTubeSearchTool()
# View tool metadata
print(tool.name) # Outputs: YouTube Search
print(tool.description) # Outputs: Use this tool to search for YouTube videos.
# Run a search query
results = tool.run("ThemesOfBharat")
print(results)
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.
Custom tools turn ideas into actions — empowering your agents to do exactly what you need.
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:
def greet(name: str) -> str:
return f"Hello, {name}! Welcome to LangChain custom tools."
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:
from langchain.agents import tool
@tool
def greet(name: str) -> str:
""" Returns a friendly greeting message for the given name."""
return f"Hello, {name}! Welcome to LangChain custom tools."
Invoke Your Custom Tool
print(greet.run("Prem"))
# Output: Hello, Prem! Welcome to LangChain custom tools.
@tool
decorator?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.
from langchain.agents import tool
@tool
def multiply(a: int, b: int) -> int:
'''This tool performs multiplication of two integers.'''
return a * b
You can invoke this tool using the .run()
method with your desired input values:
result = multiply.run(6, 7)
print(result) # Output: 42
This simple example demonstrates how easy it is to extend LangChain’s capabilities by building your own custom tools tailored to your specific needs.
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.