10. Custom Function Tools

This blog is part of the ADK Masterclass - Hands-On Series. Sometimes, pre-built tools aren't enough. We might need our agent to interact with an internal database, call a proprietary API, or execute specific business logic.

In ADK, we can easily turn any Python function into a tool that an agent can understand and use. ADK automatically wraps functions as tools when passed to an agent.

View Code on GitHub

Table of Contents

1. What are Function Tools?

Function Tools act as a bridge between our Python code and the agent's reasoning capabilities. When we pass a Python function to an agent, ADK automatically creates a tool that provides the agent with:

  • The Function Itself: The actual code to execute.
  • Instructions: The function's docstring becomes the instruction manual for the agent, explaining what the tool does and how to use it.
  • Schema: The type hints in our function definition tell the agent what arguments to provide.

This allows the agent to "call" our code just as it would call a built-in tool like Google Search.

graph TD User[User Prompt] -->|User Input| LLM[LLM] LLM -->|Analyzes prompt| Decision{Decision} Decision -->|Function call| ReturnJSON[Returns JSON
function call] ReturnJSON -->|Function call| Backend[Backend] Backend -->|Executes Search/CRUD operation| API[API] API -->|Returns result| GenFinal[Generates final
response] Decision -->|Generates response| GenResponse[Generates response] GenResponse -->|Final response| GenFinal GenFinal -->|Final response| Output[Final Output to User] style User fill:#f8bbd0,stroke:#c2185b,stroke-width:2px style LLM fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style Decision fill:#b9f6ca,stroke:#00c853,stroke-width:2px style ReturnJSON fill:#dcedc8,stroke:#689f38,stroke-width:2px style Backend fill:#fff9c4,stroke:#fbc02d,stroke-width:2px style API fill:#ffe0b2,stroke:#f57c00,stroke-width:2px style GenResponse fill:#e1bee7,stroke:#7b1fa2,stroke-width:2px style GenFinal fill:#bbdefb,stroke:#1976d2,stroke-width:2px style Output fill:#f8bbd0,stroke:#c2185b,stroke-width:2px

2. Tutorial

In this tutorial, we will build a customer support agent that can check the status of an order using a custom Python function.

Prerequisites

  • Python 3.11 or later installed.
  • Basic knowledge of Python functions.
  • A Google Cloud Project with Vertex AI API enabled (or Google AI Studio key).
  • ADK installed (pip install google-adk).

Step 1: Create the Project

First, let's create a new agent project using the ADK CLI:

adk create custom_tool_agent
cd custom_tool_agent

Set up our environment variables:

cp .env.example .env
# Edit .env and add our GOOGLE_API_KEY

Step 2: Define the Tool and Agent

Open agent.py. We will define a function called get_order_status that mimics a database lookup. ADK automatically wraps Python functions as tools, so we can pass them directly to the agent.

The docstring is crucial here—it tells the model when to use this tool and what parameters it needs.

from google.adk.agents import Agent

def get_order_status(order_id: str) -> dict:
    """
    Retrieves the status of an order by order ID.
    
    Args:
        order_id (str): The unique order identifier.
    
    Returns:
        dict: Order status information including status and delivery date.
    """
    # In a real scenario, this would query a database or API.
    return {
        "status": "success",
        "order_id": order_id,
        "order_status": "Shipped",
        "delivery_date": "Nov 24, 2025"
    }

root_agent = Agent(
    model="gemini-2.5-flash",
    name="support_agent",
    instruction="You are a helpful customer support assistant. Use the get_order_status tool when users ask about their orders.",
    tools=[get_order_status],  # ADK automatically wraps functions as FunctionTool
)

Step 3: Run the Agent

Now, let's run our agent and test the custom tool:

adk web

Open the URL provided (usually http://localhost:8000) and try asking:

"Can you check the status of order #12345?"

The agent will:

  1. Recognize that it needs to check an order status.
  2. Identify the get_order_status tool as the correct tool to use.
  3. Extract "12345" as the order_id argument.
  4. Execute our Python function.
  5. Use the returned JSON data to generate a natural language response like: "Order #12345 has been shipped and is expected to arrive on Nov 24, 2025."

Next Steps

Now that we've built a custom tool, explore how to automate tool creation from API specifications:

Resources

Comments