What are Custom Tools?
Custom tools are functions that extend your AI agent’s capabilities beyond the built-in functionality. They allow agents to interact with external systems, perform specialized operations, and execute complex workflows. What do they do?- Execute specific business logic or API calls
- Interact with databases, file systems, or external services
- Perform data processing, calculations, or transformations
- Enable human-in-the-loop workflows with confirmation and input requirements
- Provide caching and performance optimization capabilities
- Function Definition: The core logic that performs the actual work
- Type Hints: Required parameter and return type annotations for the LLM to understand usage
- Documentation: Clear docstrings explaining the tool’s purpose and parameters
- Configuration: Optional behavioral settings like confirmation requirements, caching, or external execution
- Error Handling: Robust error management for production reliability
Core Principles For Custom Tools
When creating custom tools, ensure you define these elements:- Clear Purpose: Each tool should have a single, well-defined responsibility
- Type Safety: All parameters and return values must have explicit type hints
- Documentation: Comprehensive docstrings that explain usage and expected behavior
- Error Handling: Graceful failure handling with meaningful error messages
- Configuration: Appropriate behavioral settings for your use case
Defining Tool Functions
The function definition is the foundation of your custom tool. Follow these guidelines:- Single Responsibility: Each tool should do one thing well
- Type Annotations: Every parameter and return value must have type hints
- Clear Naming: Use descriptive function names that indicate the tool’s purpose
- Documentation: Write comprehensive docstrings that explain parameters, behavior, and return values
Tool Best Practices
1. Error Handling
Always implement proper error handling in your tools:2. Input Validation
Validate inputs before processing:3. Clear Documentation
Always provide comprehensive docstrings:Let’s Create Custom Tools for a Website Analysis Agent
In this example, we’ll create basic tools and show how to add user confirmation for sensitive operations.@tool
decorator supports many powerful configuration options including:
- User Confirmation: Require manual approval before executing sensitive tools
- User Input Collection: Prompt users for specific values during execution
- External Execution: Pause tool execution for external processes
- Result Caching: Cache expensive operations with TTL control
- Result Display: Show tool outputs directly to users
- Execution Control: Stop agent execution after specific tools