|
|
CrewAI Custom Tools
Author: Venkata Sudhakar
A CrewAI tool is a function that an agent can choose to call when it needs information or wants to take an action. Out of the box CrewAI comes with built-in tools for web search and file reading, but for real-world applications you need custom tools that connect to your own APIs, databases, or internal services. A custom tool is just a Python class decorated with @tool or inheriting from BaseTool - you write the logic, and the agent decides when and how to call it based on its description. The tool description is critical. The LLM reads the description to understand what the tool does and when to use it. A clear, specific description like "Look up the current row count and last sync time for a database table by name" will be used correctly. A vague description like "gets data" will be misused or ignored. The tool returns a string that the agent incorporates into its reasoning before deciding the next step. The below example shows two custom tools for a data migration agent: one that checks table row counts and one that checks CDC lag, then wires them into an agent.
Wiring the tools into a CrewAI agent and running it,
It gives the following output,
Agent calling tool: check_table_row_count(table_name="customers")
Tool result: Table customers: 125000 rows migrated.
Agent calling tool: check_table_row_count(table_name="orders")
Tool result: Table orders: 890000 rows migrated.
Agent calling tool: check_cdc_lag(consumer_group="migration-consumer")
Tool result: Consumer group migration-consumer: lag=2s, pending_messages=200
Final answer:
Migration status: customers (125,000 rows) and orders (890,000 rows) are migrated.
CDC lag is 2 seconds with 200 pending messages - nearly caught up but not zero.
Recommendation: Wait 1-2 minutes for CDC lag to reach zero before cutting over.
Keep tool descriptions precise and action-oriented. Start with a verb: "Check...", "Look up...", "Fetch...", "Calculate...". Include what the input should be and what the output contains. The agent has no other way to understand your tool except through this description, so treat it like documentation that another developer will read.
|
|