Step-by-Step Guide
Learn how to create custom tools in Agency Swarm framework.
In Agency Swarm, tools are Python classes that inherit from 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.
Step-by-step Guide
To create a custom tool, typically you need to follow these steps:
Add Import Statements
On top of your tool file, import the necessary modules and classes.
Define the Tool Class and Docstring
Create a new class that inherits from 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
Use Pydantic fields to define the inputs your tool will accept.
Implement the run Method
Add the functionality that will be executed when the tool is called.
The 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
Test the tool independently to ensure it behaves as expected. We recommend adding a if __name__ == "__main__":
block at the end of the tool file:
Add the Tool to an Agent
After your tool works as expected, simply add it to an agent’s list of tools
.
Full Code Example
Below is the full code example for a calculator tool above.
Next Steps
- Checkout Best Practices & Tips
- Learn why PyDantic is all you need