While agents provide the reasoning capability, allowing Large Language Models (LLMs) to decide on a sequence of actions, they often need mechanisms to interact with the outside world or perform tasks beyond pure text generation. This is where tools come into play. Think of tools as specialized instruments that an agent can employ to gather information or execute specific operations, extending its reach and capabilities significantly.
In the context of LLM frameworks like LangChain, a tool is essentially a function or service that an agent can call upon. These tools encapsulate specific functionalities, such as:
By providing an agent with a curated set of tools, you empower it to overcome the inherent limitations of the LLM itself, grounding its responses in external data or actions.
How does an agent actually decide to use a tool and incorporate its output? The process typically follows an iterative loop, often based on patterns like ReAct (Reasoning and Acting):
web_search
) and formulates the necessary input for that tool (e.g., the search query current weather in London
).7 degrees Celsius, cloudy
).This loop continues until the agent determines it has enough information to fulfill the original request.
The agent reasoning loop. The LLM decides whether to reason internally, take an action using a tool, or generate the final response.
Frameworks like LangChain provide convenient ways to define and manage tools. A tool is typically represented by an object or function that has at least:
calculator
, web_search
).Here's a conceptual example using LangChain's @tool
decorator in Python to define a simple calculator tool:
# Assuming necessary LangChain imports
from langchain.tools import tool
@tool
def simple_calculator(expression: str) -> str:
"""
Use this tool to evaluate simple arithmetic expressions.
Input should be a valid mathematical expression string (e.g., '2 + 2', '10 * (4 / 2)').
Only handles basic +, -, *, / operators and parentheses.
"""
try:
# WARNING: eval() can be dangerous with untrusted input.
# In production, use a safer parsing library like numexpr or ast.literal_eval.
result = eval(expression)
return str(result)
except Exception as e:
return f"Error evaluating expression: {str(e)}"
# Example usage (outside the agent context)
# print(simple_calculator.name) # -> simple_calculator
# print(simple_calculator.description) # -> The docstring content
# print(simple_calculator.run("5 * (3 + 1)")) # -> 20
Notice how the docstring serves as the tool's description. A well-written description guides the LLM effectively. For instance, the description explicitly states when to use the tool ("evaluate simple arithmetic expressions"), the expected input format ("valid mathematical expression string"), and examples.
Once defined, you provide a list of these tool objects to the agent during its initialization. The framework handles the logic of presenting these tools (specifically their names and descriptions) to the LLM within the prompt when it needs to decide on an action.
# Conceptual LangChain agent initialization with tools
from langchain_openai import ChatOpenAI
# Assume 'web_search_tool' is another defined tool instance
# from langchain.agents import AgentExecutor, create_react_agent # (Example imports)
# from langchain import hub # (Example imports)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [simple_calculator, web_search_tool] # List of available tools
# Example: Using a predefined ReAct prompt template
# react_prompt = hub.pull("hwchase17/react")
# agent = create_react_agent(llm, tools, react_prompt)
# agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# result = agent_executor.invoke({"input": "What is the result of 99 divided by 3, and what's the weather in Paris?"})
In this setup, when the agent receives the input, the underlying LLM will see descriptions for both simple_calculator
and web_search_tool
and use its reasoning capabilities to determine which tool (or tools, sequentially) to call with appropriate inputs to answer the combined question.
Agents can be equipped with a wide variety of tools:
When incorporating tools, keep these points in mind:
Tools transform agents from pure reasoners into active participants capable of interacting with and retrieving information from diverse sources. By carefully selecting, designing, and integrating tools, you can build significantly more powerful and grounded LLM applications. The hands-on practice later in this chapter will guide you through building an agent that leverages a specific tool.
© 2025 ApX Machine Learning