Back to Curriculum

Custom Tooling for Agentic Research: The API Bridge

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.

🏗️ The Tooling Interface

A custom tool requires 3 components:

  1. The Logic: The raw Python function.
  2. The Metadata: A description telling the agent when and how to use the tool.
  3. The Error Handling: Ensuring the agent can recover if the tool fails.

🛠️ Technical Snippet: Building a 'PSI Auditor' Tool for CrewAI

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}."

🔍 Nuance: Semantic Tool Descriptions

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."


⚡ Practice Lab: The Tool Bridge

  1. Build: Create a Python function that takes a JSON string and returns the character count.
  2. Describe: Write a 2-sentence metadata description for an agent.
  3. Test: Ask an agent to "Count the characters in this JSON: {...}." Verify it chooses your tool instead of guessing.

📝 Homework: The CRM Writer Tool

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.