What are tools?
Tools in Agenite are specialized components that extend an agent’s capabilities beyond simple text generation. They allow agents to perform specific actions, access external systems, process data, and interact with the real world. Think of tools as the “hands” of an agent - they enable it to reach out and interact with systems and data outside of its language model, giving it practical abilities to accomplish real tasks.Key aspects of tools
- Action-oriented: Tools perform specific actions like calculations, API calls, or data processing
- Schema-defined: Tools use JSON Schema to validate inputs and ensure proper usage
- Error handling: Tools provide consistent error reporting for reliable agent interactions
- Composable: Multiple tools can be combined to create complex agent capabilities
- Type-safe: Tools are built with TypeScript for type safety and better developer experience
Tool structure
A tool in Agenite consists of the following key elements:- Name: A unique identifier for the tool
- Description: Helps the LLM understand when and how to use the tool
- Input schema: Defines the expected structure of inputs using JSON Schema
- Execute function: The actual implementation that performs the tool’s function
- Version (optional): For tracking tool versions
Basic tool architecture
Creating tools
You can create tools using theTool
class from the @agenite/tool
package. Let’s explore some examples:
Basic calculator tool
Here’s a simple calculator tool that performs arithmetic operations:API integration tool
Tools can also integrate with external APIs. Here’s a weather tool example:Input validation
Tools validate inputs based on the provided schema, ensuring that they receive the correct data structure before execution. Validation happens automatically before the execute function is called.Using JSON Schema
The simplest approach is to define a JSON Schema directly:Using Zod for validation
For more advanced validation, you can use Zod with thezod-to-json-schema
package:
Tool response structure
Tool responses follow a consistent format:- isError: Indicates whether the tool execution was successful
- data: The result as a string (for successful executions) or error message (for failures)
- error: Detailed error information when
isError
is true - duration: Execution time in milliseconds (added automatically if not provided)
Integrating tools with agents
To use tools with an agent, pass them in thetools
array when creating an agent:
- Parse the user’s request
- Identify which tool to use
- Format the appropriate input
- Call the tool with that input
- Process the tool’s response
- Incorporate the result into its final answer
Best practices for creating tools
- Clear descriptions: Write clear tool descriptions to help the LLM understand when to use them
- Robust error handling: Always handle potential errors and provide meaningful error messages
- Input validation: Use comprehensive schemas to validate inputs and prevent errors
- Focused functionality: Each tool should do one thing well rather than trying to handle multiple functions
- Stateless design: Design tools to be stateless whenever possible for easier debugging and testing
- Documentation: Document expected inputs and outputs, especially any special cases