How to Create a Custom Tool for an AI Agent
To create a custom tool for an AI agent, define a class or function that performs a specific task and register it with the agent's tool system using
Tool or similar interfaces. Then, integrate this tool so the agent can call it during its decision process.Syntax
Creating a custom tool involves defining a callable function or class and wrapping it with a Tool interface that the agent recognizes. The key parts are:
- Function or Class: The code that performs the task.
- Tool Wrapper: Registers the function with a name and description.
- Agent Integration: Adds the tool to the agent's tool list.
python
from langchain.agents import Tool def custom_tool_function(input_text: str) -> str: # Your custom logic here return f"Processed: {input_text}" custom_tool = Tool( name="CustomTool", func=custom_tool_function, description="Processes input text and returns a result." ) # Later, add custom_tool to your agent's tools list
Example
This example shows how to create a simple custom tool that reverses a string and integrates it with a basic agent.
python
from langchain.agents import Tool, initialize_agent from langchain.llms import OpenAI # Define the custom tool function def reverse_text(text: str) -> str: return text[::-1] # Wrap the function as a Tool reverse_tool = Tool( name="ReverseText", func=reverse_text, description="Reverses the input text." ) # Initialize a language model llm = OpenAI(temperature=0) # Create an agent with the custom tool agent = initialize_agent( tools=[reverse_tool], llm=llm, agent="zero-shot-react-description", verbose=True ) # Use the agent to call the custom tool response = agent.run("Reverse the word 'hello'") print(response)
Output
olleh
Common Pitfalls
Common mistakes when creating custom tools include:
- Not wrapping the function properly with the
Toolinterface, so the agent cannot recognize it. - Missing or unclear
description, which helps the agent decide when to use the tool. - Using blocking or slow code inside the tool, which can delay agent responses.
- Not handling input/output types correctly, causing errors during execution.
python
from langchain.agents import Tool # Wrong: Missing description and not wrapped as Tool # def bad_tool(text): # return text.upper() # Right: Proper Tool wrapping with description def good_tool(text: str) -> str: return text.upper() good_tool_wrapped = Tool( name="UpperCaseTool", func=good_tool, description="Converts text to uppercase." )
Quick Reference
Summary tips for creating custom tools for agents:
- Define a clear, single-purpose function or class.
- Wrap it with
Toolincluding a descriptive name and description. - Integrate the tool into the agent's tool list before running.
- Test the tool independently to ensure correct input/output.
- Keep tool logic fast and simple for smooth agent operation.
Key Takeaways
Wrap your custom function with a Tool interface including name and description.
Integrate the custom tool into the agent's tool list before running the agent.
Provide clear descriptions so the agent knows when to use your tool.
Keep tool logic simple and fast to avoid slowing down the agent.
Test your tool separately to ensure it handles inputs and outputs correctly.