Principle:CrewAIInc CrewAI Tool Implementation
Overview
A set of implementation patterns for creating tools through class inheritance, function decoration, or factory methods, each providing different trade-offs between simplicity and customization.
Description
Tool Implementation provides three distinct approaches for creating tools that agents can invoke:
- Subclass BaseTool and implement
_run(): This approach offers full control over the tool's behavior, configuration, and argument schema. It is best suited for complex tools that require custom initialization, state management, or advanced validation logic. The developer explicitly defines thename,description,args_schema, and the_run()method.
- Use the
@tooldecorator on a function: This approach enables rapid development by transforming a plain Python function into a fully functional tool. The decorator automatically derives theargs_schemafrom the function's type annotations and thedescriptionfrom its docstring. The function name becomes the tool name. This is the quickest way to create a tool.
- Use
CrewStructuredTool.from_function()factory: This approach provides programmatic tool creation, useful when tools need to be generated dynamically at runtime (e.g., from configuration files, database entries, or API specs). It accepts a function along with explicit name, description, and schema overrides.
All three approaches produce tool instances that conform to the BaseTool interface and can be assigned to agents interchangeably.
Trade-Off Comparison
| Approach | Simplicity | Customization | Use Case |
|---|---|---|---|
@tool decorator |
High | Low | Quick prototyping, simple tools |
BaseTool subclass |
Low | High | Complex tools with state or custom validation |
from_function() factory |
Medium | Medium | Dynamic or programmatic tool generation |
Key Considerations
- Decorator approach: The function's docstring becomes the tool description, so it must be written for the LLM, not for human developers. Type annotations on parameters are mandatory since they drive the auto-generated args schema.
- Subclass approach: Requires more boilerplate but allows for tool-level state (e.g., API clients, database connections) stored as instance attributes. Custom validators can be added to the Pydantic model.
- Factory approach: The
infer_schemaparameter controls whether the args schema is auto-generated from the function signature or must be explicitly provided. Wheninfer_schema=True(the default), it behaves similarly to the decorator. - Interchangeability: Regardless of the creation method, all tools implement the same interface. An agent cannot distinguish between a tool created via decorator and one created via subclass.
Theoretical Basis
This principle follows the Strategy Pattern where different creation mechanisms produce interchangeable tool instances. The tool interface (BaseTool) defines a contract, and each creation approach is a different strategy for fulfilling that contract. This allows developers to choose the most appropriate creation method for their use case without affecting how agents consume the tools.
Relationship to Implementation
Implementation:CrewAIInc_CrewAI_Tool_Decorator_And_Classes
The @tool decorator, Tool class, and CrewStructuredTool factory in CrewAI provide the concrete implementations of the three approaches described in this principle.
See Also
- Principle:CrewAIInc_CrewAI_Tool_Design -- The interface specification that all tools must conform to
- Principle:CrewAIInc_CrewAI_Built_In_Tool_Selection -- Pre-built tools that use these implementation patterns
- Principle:CrewAIInc_CrewAI_Tool_Assignment -- How created tools are bound to agents and tasks