Agents are only as powerful as the tools they can access. In this lesson, we learn how to build and expose Custom Tools (Python functions) to your agent swarms, allowing them to perform real-world actions like database lookups, API calls, and local file manipulation.
A custom tool requires 3 components:
from crewai_tools import tool
import requests
@tool("psi_auditor")
def psi_auditor(url: str) -> str:
"""Performs a Google PageSpeed Insights audit and returns the LCP score."""
api_url = f"https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={url}&category=PERFORMANCE"
response = requests.get(api_url).json()
lcp = response['lighthouseResult']['audits']['largest-contentful-paint']['displayValue']
return f"Technical Audit for {url}: LCP is {lcp}."
The agent doesn't see your code; it only sees the Docstring. If your description is vague (e.g., "Fetches data"), the agent will use it incorrectly. A high-status description is precise: "Use this tool ONLY when you need to identify the Largest Contentful Paint (LCP) for a specific website URL."
Build a custom tool that takes a {lead_email, score} object and appends it to a local CSV file. Register this tool with a CrewAI agent and verify the agent can write to the CSV autonomously.