Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:CrewAIInc CrewAI Tool Implementation

From Leeroopedia

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:

  1. 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 the name, description, args_schema, and the _run() method.
  1. Use the @tool decorator on a function: This approach enables rapid development by transforming a plain Python function into a fully functional tool. The decorator automatically derives the args_schema from the function's type annotations and the description from its docstring. The function name becomes the tool name. This is the quickest way to create a tool.
  1. 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_schema parameter controls whether the args schema is auto-generated from the function signature or must be explicitly provided. When infer_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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment