Best practices and real-world examples for Agency Swarm tools.
Although the tool interface is straightforward and simple to use, there are actually quite a few practices and tricks that you can use to get significantly better results.
Based on your tool’s logic, you can provide hints for the agent in tool output on what to do next.
Copy
Ask AI
class QueryDatabase(BaseTool): question: str = Field(...) def run(self): # query your database here context = self.query_database(self.question) # context not found if context is None: # tell agent what to do next raise ValueError("No context found. Please propose to the user to change the topic.") else: # return the context to the agent return context
Use shared_state to validate previous actions taken by this or other agents, before allowing it to proceed with the next action.
Copy
Ask AI
class Action2(BaseTool): input: str = Field(...) def run(self): if self._shared_state.get("action_1_result", None) is "failure": raise ValueError("Please proceed with the Action1 tool first.") else: return "Success. The action has been taken."
Include test cases at the bottom of each tool file.
Copy
Ask AI
if __name__ == "__main__": # Test the EmailSender tool email_sender = EmailSender( chain_of_thought="Plan to inform the team about the update.", recipient="user@example.com", subject="Project Update", body="The project is on track." ) assert email_sender.run() == "Email sent successfully."