How to Use Tools with Agent LangChain: Simple Guide
To use
tools with a LangChain agent, you create tool instances and pass them to the agent constructor. The agent then uses these tools to perform specific tasks like searching or calculations automatically during its run.Syntax
Using tools with LangChain agents involves these parts:
- Tools: Objects that perform specific actions (e.g., search, calculator).
- Agent: The main controller that decides when and how to use tools.
- AgentExecutor: Runs the agent with the tools and input.
The basic syntax is to import or define tools, create an agent with those tools, then run the agent with a query.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI from langchain.agents import AgentType # Define tools search_tool = Tool(name="Search", func=lambda x: "search result for " + x, description="Useful for web search") calc_tool = Tool(name="Calculator", func=lambda x: str(eval(x)), description="Useful for math calculations") # Initialize LLM llm = OpenAI(temperature=0) # Create agent with tools agent = initialize_agent( tools=[search_tool, calc_tool], llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) # Run agent response = agent.run("What is 2 + 2?")
Example
This example shows how to create a LangChain agent with a calculator tool and a search tool, then ask it a question that requires calculation.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI from langchain.agents import AgentType # Calculator tool using Python eval safely for demo def calculator(input_str: str) -> str: try: return str(eval(input_str, {"__builtins__": {}})) except Exception: return "Error in calculation" calc_tool = Tool( name="Calculator", func=calculator, description="Useful for math calculations" ) # Dummy search tool def search(input_str: str) -> str: return f"Search results for: {input_str}" search_tool = Tool( name="Search", func=search, description="Useful for web search" ) llm = OpenAI(temperature=0) agent = initialize_agent( tools=[calc_tool, search_tool], llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False ) result = agent.run("Calculate 12 * 8 and then search for LangChain") print(result)
Output
96
Common Pitfalls
Common mistakes when using tools with LangChain agents include:
- Not passing tools as a list to
initialize_agent. - Using tools with incompatible function signatures.
- Forgetting to set
agenttype correctly (e.g.,ZERO_SHOT_REACT_DESCRIPTION). - Not handling exceptions inside tool functions, causing agent crashes.
Always test each tool function independently before integrating.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI from langchain.agents import AgentType # Wrong: tools passed as single tool, not list calc_tool = Tool(name="Calculator", func=lambda x: str(eval(x)), description="Math tool") llm = OpenAI(temperature=0) # This will raise an error # agent = initialize_agent(tools=calc_tool, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION) # Right way: agent = initialize_agent( tools=[calc_tool], llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION )
Quick Reference
Tips for using tools with LangChain agents:
- Always define tools with a
name,func, anddescription. - Pass tools as a list to
initialize_agent. - Choose the right
AgentTypefor your use case. - Test tools separately before combining.
- Use
verbose=Trueduring development to see agent decisions.
Key Takeaways
Create tool objects with a name, function, and description before passing to the agent.
Pass all tools as a list to the agent initializer along with the LLM and agent type.
Use agent types like ZERO_SHOT_REACT_DESCRIPTION to enable tool usage during runs.
Test tool functions independently to avoid runtime errors in the agent.
Enable verbose mode to debug how the agent chooses and uses tools.