Custom Tools
Overview
Upsonic’s custom tool system provides the ultimate flexibility for building AI agents with domain-specific capabilities. Whether you need simple utility functions or complex business logic, the framework offers multiple approaches to create, configure, and deploy custom tools that integrate seamlessly with your AI agents.Key Features
- Multiple Creation Patterns: Functions, classes, toolkits, and agent-as-tool support
- Advanced Configuration: Comprehensive tool configuration options for production use
- Execution Control: External execution, user interaction, and confirmation workflows
- Performance Features: Caching, retries, timeouts, and parallel execution
- Developer Experience: Type hints, docstring parsing, and automatic schema generation
- Production Ready: Built-in error handling, logging, and monitoring capabilities
Tool Creation Approaches
-
Function Tools: Simple, decorator-based tool creation
@tool
decorator with configuration options- Type hints and docstring-based schema generation
- Hooks for before/after execution logic
-
Class-Based Tools: Object-oriented tool organization
ToolKit
classes for related functionality- Regular classes with automatic method exposure
- Stateful tools with instance variables
-
Agent as Tool: Hierarchical agent architectures
- Use specialized agents as tools for complex workflows
- Multi-agent systems with delegation and coordination
Advanced Capabilities
- External Execution: Tools that run outside the framework (databases, file systems)
- User Interaction: Tools that require user input or confirmation
- Caching & Performance: Built-in caching, retries, and timeout management
- Error Handling: Comprehensive error handling and recovery mechanisms
- Monitoring: Execution tracking and performance metrics
When to Use Custom Tools
- Business Logic: Implement domain-specific algorithms and workflows
- External Integrations: Connect to databases, APIs, and external services
- Complex Workflows: Multi-step processes requiring orchestration
- Specialized Domains: Tools tailored to specific industries or use cases
- Performance Optimization: Custom caching, batching, and optimization strategies
- Security Requirements: Tools with specific security and access controls
Development Benefits
- Rapid Prototyping: Quick tool creation with minimal boilerplate
- Type Safety: Full type hint support with automatic validation
- Documentation: Automatic schema generation from docstrings
- Testing: Built-in testing and debugging capabilities
- Deployment: Production-ready tools with monitoring and error handling
- Maintainability: Clean, organized code with clear separation of concerns
Function Tools
The simplest way to create a custom tool is by decorating a function with the@tool
decorator.
Basic Function Tool
Function Tool with Configuration
You can configure tool behavior using the@tool
decorator with parameters:
Function Tool with Hooks
You can add before/after execution hooks to your tools:Tool Configuration Options
The@tool
decorator supports various configuration options:
requires_confirmation
: If True, the agent will pause and require user confirmation before executing the toolrequires_user_input
: If True, the agent will pause and prompt the user for input for specified fieldsuser_input_fields
: Field names that the user should provide when requires_user_input is Trueexternal_execution
: If True, the tool’s execution is handled by an external processshow_result
: If True, the output is shown to the user and NOT sent back to the LLMstop_after_tool_call
: If True, the agent’s run will terminate after this tool callsequential
: If True, this tool requires sequential execution (no parallelization)cache_results
: If True, the result will be cached based on argumentscache_dir
: Directory to store cache filescache_ttl
: Time-to-live for cache entries in secondstool_hooks
: Custom functions to run before/after tool executionmax_retries
: Maximum number of retries allowed for this tooltimeout
: Timeout for tool execution in secondsstrict
: Whether to enforce strict JSON schema validation on tool parametersdocstring_format
: Format of the docstring (‘google’, ‘numpy’, ‘sphinx’, or ‘auto’)
External Execution and User Interaction
External Execution
For tools that need to be executed outside the framework (e.g., database operations, file system operations):User Input Requirements
For tools that need user input during execution:User Confirmation
For tools that require user confirmation before execution:Combined Features
You can combine multiple features:ToolKit Classes
ToolKit classes allow you to organize related tools together. Only methods decorated with@tool
are exposed as tools:
Regular Class Tools
You can also use regular classes where all public methods are automatically exposed as tools:Agent as Tool
You can use other agents as tools by simply adding them to the tools list:Combining Multiple Tools
You can combine different types of tools in a single task:Best Practices
- Use descriptive docstrings: Always provide clear docstrings with Args and Returns sections
- Handle errors gracefully: Use try-catch blocks and return meaningful error messages
- Use type hints: Specify parameter and return types for better tool schema generation
- Organize related tools: Use ToolKit classes for related functionality
- Configure appropriately: Use tool configuration options to control behavior
- Test your tools: Always test your tools before using them in production
Tool Schema Generation
Upsonic automatically generates JSON schemas for your tools based on:- Function signatures and type hints
- Docstring documentation
- Tool configuration options
- Google style
- NumPy style
- Sphinx style
- Auto-detection