Skip to main content

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
What are the parts of a custom tool?
  • 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
Good Tool Definition:
Bad Tool Definition:

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:

User Interaction Features

Requires Confirmation

Pause execution and require user confirmation before a tool runs. Perfect for sensitive operations like deletions, sending emails, or financial transactions.

Requires User Input

Collect specific field values from the user at runtime. Useful for credentials, personal information, or dynamic parameters.

Combined User Interaction

Use both features together for maximum control:

External Tool Execution (Human-in-the-Loop)

Execute tools outside the agent’s automatic flow, enabling human-in-the-loop workflows for security-sensitive operations, external integrations, and cost control.

Basic External Execution

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.

When to Use Each Feature

Use requires_confirmation for:
  • Destructive operations (delete, reset, drop)
  • Sensitive actions (send email, publish content)
  • Financial transactions (transfer money, make payment)
Use requires_user_input for:
  • Credentials (API keys, passwords, tokens)
  • Personal information (name, age, address)
  • Dynamic parameters that change per execution
  • Sensitive data that shouldn’t be in code
Use external_execution for:
  • Security-sensitive operations requiring manual review
  • External integrations with special credentials
  • Hardware or network resources not accessible to the agent
  • Cost control over expensive operations
  • Compliance and audit requirements

Best Practices Summary

  1. Clear Purpose: Each tool should have a single, well-defined responsibility
  2. Type Safety: All parameters and return values must have explicit type hints
  3. Documentation: Comprehensive docstrings explaining usage and expected behavior
  4. Error Handling: Graceful failure handling with meaningful error messages
  5. Security: Use requires_user_input for credentials, never hardcode sensitive data
  6. Validation: Always validate inputs before processing
  7. User Experience: Provide clear prompts and helpful error messages
Need more advanced features? The @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
For detailed examples and advanced patterns, see our comprehensive Tools Documentation.