The
#[Tool] attribute is the recommended way to create tools in LarAgent. It transforms regular PHP methods into tools that your agent can invoke, with automatic schema generation from type hints and descriptions.Basic Usage
Add the#[Tool] attribute to any method in your agent class:
- Registers the method as a tool with the given description
- Extracts parameter names and types from the method signature
- Generates a JSON schema for the LLM
- Handles tool invocation and response processing
Parameter Descriptions
Provide descriptions for each parameter to help the LLM understand what values to pass:Using Enums
PHP Enums constrain parameter values to a specific set of options:enum constraint in the OpenAPI schema before sending to the LLM:
DataModel Parameters
Use DataModel classes as parameters for complex, structured input:- Detects the DataModel type hint
- Generates a nested JSON schema from the DataModel properties
- Converts the LLM’s array response back to a DataModel instance
Nested DataModels
DataModels can contain other DataModels for deeply structured data:DataModels with Enums
Combine DataModels and Enums for type-safe structured input:DataModel Arrays
UseDataModelArray for parameters that accept multiple items:
Optional Parameters
Use nullable types and/or default values for optional parameters:Union Types
PHP 8 union types allow parameters to accept multiple types:Static vs Instance Methods
Both static and instance methods work as tools:- Instance Method
- Static Method
Use instance methods when you need access to the agent instance (
$this):Injecting External Data
When your tools need data from outside the agent (e.g., from a controller), use custom setter methods:Tools Without Parameters
Some Tools don’t require parameters:Reusable Tool Traits
It’s good practice to extract tool groups into traits for reusability and better organization:Return Values
Tools can return various types — LarAgent converts them to strings for the LLM:Best Practices
Write clear descriptions
Write clear descriptions
Tool and parameter descriptions are the LLM’s only guide for when and how to use tools. Be specific about:
- What the tool does
- Expected input formats
- What the tool returns
Use appropriate types
Use appropriate types
Leverage PHP’s type system to constrain inputs:
- Use
int,float,boolfor primitives - Use Enums for fixed option sets
- Use DataModels for complex structures
- Use nullable types for optional parameters
Handle errors gracefully
Handle errors gracefully
Return informative error messages to LLM instead of throwing exceptions:
Keep tools focused
Keep tools focused
Each tool should do one thing well. Split complex operations into multiple tools:
Limit the number of tools
Limit the number of tools
Too many tools can overwhelm the LLM and lead to poor tool selection. As a general guideline:
- Small models (e.g., GPT-4o-mini, Claude Haiku): Up to 10 tools
- Large models (e.g., GPT-4o, Claude Sonnet/Opus): Up to 30 tools
- Grouping related functionality into fewer, more versatile tools
- Using different agents for different domains
- Dynamically registering only relevant tools based on context
- Orchestrating multiple agents with specialized toolsets
Next Steps
Tool Classes & Inline Tools
Create reusable tool classes or build tools dynamically at runtime.
Tool Configuration
Configure tool choice, parallel execution, and more.
DataModels
Learn more about DataModel classes for structured data.

