Learn how to create custom tools in Agency Swarm framework.
BaseTool
. They are defined using Pydantic, a data validation library. Each BaseTool must implement the run
method, which is the main method that will be called when the tool is invoked by an agent.
Add Import Statements
Define the Tool Class and Docstring
BaseTool
. Write a clear docstring describing the tool’s purpose. This docstring is crucial as it helps agents understand how to use the tool.Define Input Fields
Custom Validation Logic (Optional)
Implement the run Method
run
method should return a string, which is the tool’s output that the agent will see and use in its response.Test the Tool Independently
if __name__ == "__main__":
block at the end of the tool file:Add the Tool to an Agent
tools
.Using tools folder
tools_folder
directory and it will be automatically added to the agent.tools_folder
should contain a class that is named exactly the same as the file name. For example, Calculator.py
should contain a Calculator
class.