In a standard AI system, an LLM possesses two core capabilities: Reasoning (the ability to think and break down a problem) and Generation (the ability to speak or write an answer). However, LLMs are inherently limited; they cannot autonomously perform tasks like booking a flight, fetching live weather data, or running complex math reliably.
Tools are the mechanism that provides an LLM with “hands and legs”. They are essentially Python functions packaged in a way that an LLM can understand, decide when to use, and call when needed to execute a specific task.
1. Why are Tools Necessary? #
LLMs are powerful but have specific “blind spots” that tools help cover:
- Real-time Data: LLMs have a knowledge cut-off date. Tools like a Web Search tool allow them to fetch today’s news.
- Reliable Logic: LLMs are language models, not calculators. A Python REPL tool can run actual code to solve a math problem instead of the LLM guessing the answer.
- External Interaction: LLMs cannot naturally call APIs, send emails via Gmail, or interact with a SQL database. Tools provide the interface for these actions.
2. Types of Tools in LangChain #
A. Built-in Tools
LangChain provides production-ready tools for popular use cases so you don’t have to write the logic yourself.
- DuckDuckGo Search: For real-time web searches.
- Wikipedia Query Run: To fetch summarized information from Wikipedia.
- Shell Tool: To execute commands directly on the host machine (use with caution!).
- Python REPL: To execute raw Python code for logic or math.
B. Custom Tools
When you have a unique business case—like interacting with your company’s private API or database—you must build your own tools.
3. How to Create Custom Tools (3 Methods) #
Method 1: The @tool Decorator (Easiest)
This is the most straightforward way. You write a standard Python function and wrap it with a decorator.
from langchain_core.tools import tool
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two numbers together.""" # Highly recommended docstring
return a * b
# Tools are Runnables, so they use .invoke()
result = multiply.invoke({"a": 3, "b": 5})
print(result) # Output: 15
Important: The docstring and type hinting (e.g., a: int) are crucial because they tell the LLM what the tool does and what inputs it expects.
Method 2: StructuredTool with Pydantic
This method is more “strict” and better for production. It uses a Pydantic model to enforce the input schema.
from pydantic import BaseModel, Field
from langchain_core.tools import StructuredTool
class MultiplyInput(BaseModel):
a: int = Field(description="First number")
b: int = Field(description="Second number")
def multiply_logic(a: int, b: int) -> int:
return a * b
calculator_tool = StructuredTool.from_function(
func=multiply_logic,
name="Calculator",
description="Multiplies two numbers",
args_schema=MultiplyInput
)
Method 3: Inheriting from BaseTool
This is the most advanced method, allowing for deep customization and the creation of asynchronous versions of your tools.
4. Understanding Toolkits #
When you have multiple related tools (e.g., several tools for interacting with Google Drive), you can group them into a Toolkit. This promotes reusability across different applications.
class MathToolkit:
def get_tools(self):
return [add_tool, multiply_tool]
toolkit = MathToolkit()
tools = toolkit.get_tools() # Access all related tools at once
5. From Tools to Agents #
The “marriage” of an LLM and Tools is what creates an Agent.
- Reasoning (LLM): “To answer this, I first need to find the weather in Mumbai”.
- Action (Tools): The Agent calls the weather tool, gets the data, and then generates the final response.
Understanding Tools is the fundamental first step toward building autonomous AI agents that can solve real-world problems. Acknowledge that the next steps in this journey are Tool Calling (connecting the tool to the LLM) and finally, Agent construction
6. What is Tool Calling? #
Tool Calling is the mechanism where an LLM, during a conversation, determines that it cannot answer a prompt using its internal knowledge alone and instead generates a structured output. This output contains:
- The name of the specific tool it wants to use.
- The arguments (inputs) required to run that tool.
It is important to understand that the LLM does not actually execute the tool itself. It acts as an advisor, suggesting: “I think this tool with these inputs will give us the answer”. The actual execution is handled by you (the programmer) or a framework like LangChain.
7. The Process: Binding and Calling #
Before an LLM can call a tool, you must Bind the tools to it.
- Tool Binding: This is the registration step where you tell the LLM which tools are available, what they do (via descriptions), and what input format they expect (via schemas).
- The Code: You use the
bind_tools()method to connect your tools to the LLM.
# 1. Define the tool
@tool
def multiply(a: int, b: int) -> int:
"""Multiplies two numbers."""
return a * b
# 2. Bind the tool to the LLM
llm_with_tools = llm.bind_tools([multiply])
3. The Tool Execution Loop
To get a final answer for a user, the system must complete a four-step loop:
- Human Message: The user asks a question (e.g., “What is 3 times 10?”).
- AI Message (Tool Call): The LLM realizes it needs the
multiplytool. It returns anai_messagewhere the text content is empty, but atool_callsattribute contains the tool name and arguments (a=3, b=10). - Tool Execution: The programmer takes those arguments, runs the actual Python function, and receives a Tool Message containing the result (30).
- Final Response: The entire history (Human Message + AI Tool Call + Tool Message) is sent back to the LLM. The LLM then sees the result and generates a final natural language answer: “The product of 3 and 10 is 30”.
4. Advanced Concept: Injected Tool Arguments
Sometimes, you might have a chain of tools where the second tool depends on the first. For example, in a currency converter, the first tool fetches a “conversion rate” from an API, and the second tool multiplies the “amount” by that rate.
To prevent the LLM from “guessing” the conversion rate based on its old training data, you use Injected Tool Arguments. By annotating an argument as InjectedToolArg, you tell the LLM: “Do not try to fill this value; the programmer will provide it after the first tool runs”.
5. Transitioning to AI Agents
While the flow described above allows an LLM to use tools, it is not yet an AI Agent.
- Manual Flow: In the examples above, the programmer manually manages the loop, deciding which tool to run and when to send data back to the LLM.
- AI Agent: An agent is autonomous. It looks at a goal, breaks it down into steps, decides which tools to call, executes them, and continues this cycle until the goal is achieved without manual intervention from the programmer.
Mastering Tool Calling is the final “fundamental topic” required before you can build these fully autonomous agents.
Tools Quiz #
Q.1 According to the source material, what are the two core capabilities that a modern Large Language Model (LLM) possesses?
Code Execution and File Management
Reasoning and Language Generation
Data Storage and Mathematical Calculation
API Interaction and Web Browsing
Explanation
Modern LLMs excel at reasoning over information and generating natural language. However, they cannot directly interact with external systems unless provided with tools.
Q.2 What is the primary role of 'Tools' in the context of LangChain and AI Agents?
To increase the parameter count of the underlying model.
To replace the need for an LLM in an agentic system.
To compress long prompts into shorter embeddings.
To provide the LLM with 'hands and legs' to perform external tasks.
Explanation
Tools extend an LLM’s capabilities by allowing it to interact with external systems, execute code, access APIs, search the web, or manipulate files, making the agent capable of performing real-world tasks.
Q.3 When creating a custom tool using the @tool decorator, why is it highly recommended to include a docstring in the Python function?
The LLM uses the docstring to understand what the tool does and when to use it.
It speeds up the execution time of the function.
It automatically generates the API keys needed for the tool.
It is required for the Python interpreter to compile the code.
Explanation
The docstring becomes part of the tool’s description, helping the LLM understand the tool’s purpose and determine when it should invoke it.
Q.4 Which built-in LangChain tool would be most appropriate for an application that needs to verify the current weather in a specific city?
DuckDuckGo Search.
Python REPL Tool.
Wikipedia Query Run.
Shell Tool.
Explanation
DuckDuckGo Search can retrieve current web information, making it suitable for checking live weather conditions or other up-to-date information.
Q.5 Which capability best demonstrates why tools are essential for building AI agents?
Generating longer responses with more tokens.
Allowing the LLM to interact with external systems such as APIs, databases, and file systems.
Increasing the number of parameters inside the LLM.
Replacing embeddings with keyword matching.
Explanation
An LLM by itself can only generate text. Tools allow it to perform actions such as calling APIs, querying databases, searching the web, reading files, executing Python code, and interacting with external applications, making AI agents capable of solving real-world tasks.
Q.6 What is the primary technical difference between using the @tool decorator and the StructuredTool class for creating custom tools?
The @tool decorator can only be used for mathematical functions.
StructuredTool allows for stricter input validation using a Pydantic model.
StructuredTool does not require a description or name.
The @tool decorator is only compatible with OpenAI models.
Explanation
StructuredTool supports Pydantic schemas, enabling detailed validation, constraints, descriptions, and structured inputs beyond standard Python type hints.
Q.7 When inheriting from the BaseTool class to create a custom tool, which specific method must be defined to contain the tool's core logic?
_run
execute
call_llm
main
Explanation
Custom tools created by extending BaseTool implement the _run() method, which contains the tool’s primary execution logic whenever the tool is invoked.
Q.8 What does an LLM actually 'see' when you connect a tool to it in LangChain?
A screenshot of the function's documentation.
The raw Python source code of the function.
An encrypted binary file.
A JSON Schema describing the tool's name, description, and arguments.
Explanation
The LLM receives a JSON Schema containing the tool’s name, description, input parameters, and required fields, allowing it to decide when and how to call the tool.
Q.9 What is the primary benefit of organizing related tools into a Toolkit?
Reduction in the cost per token of the LLM.
The ability to run tools without an internet connection.
Elimination of the need for type hinting.
Enhanced reusability across different applications.
Explanation
A Toolkit groups related tools into a reusable package, making it easier to integrate the same collection of capabilities into multiple AI applications.
Q.10 If an LLM is asked to find the factorial of a large number, why is it better to use a tool rather than relying on the LLM's own response?
LLMs are strictly prohibited from performing any multiplication.
LLMs predict the next word based on patterns and may not calculate math reliably.
A tool can answer in multiple languages simultaneously.
Tools use less energy than the brain of the LLM.
Explanation
LLMs are probabilistic language models rather than deterministic calculators. A mathematical tool executes the exact algorithm, producing accurate and reliable results even for complex calculations.