Skip to main content
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:
LarAgent automatically:
  • 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:
Clear parameter descriptions significantly improve the LLM’s ability to call tools correctly. Describing the expected format and providing examples is generally considered as best practice.

Using Enums

PHP Enums constrain parameter values to a specific set of options:
LarAgent automatically generates an enum constraint in the OpenAPI schema before sending to the LLM:

DataModel Parameters

Use DataModel classes as parameters for complex, structured input:
LarAgent automatically:
  1. Detects the DataModel type hint
  2. Generates a nested JSON schema from the DataModel properties
  3. 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

Use DataModelArray for parameters that accept multiple items:
DataModelArray is like a collection for DataModels, but strictly typed. See DataModel documentation for details.
DataModelArray also supports polymorphic arrays with multiple DataModel types using discriminator fields. See DataModel documentation for details.

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:
Use instance methods when you need access to the agent instance ($this):
Prefer static methods whenever possible — they’re slightly more performant and make dependencies explicit.

Injecting External Data

When your tools need data from outside the agent (e.g., from a controller), use custom setter methods:
Call the setter before interacting with the agent:
Return $this from setter methods to enable fluent chaining with other agent 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:
Use traits in your agents:
Organizing tools into traits by domain (e.g., WeatherTools, CalendarTools, PaymentTools) creates a library of reusable capabilities you can mix and match across agents.

Return Values

Tools can return various types — LarAgent converts them to strings for the LLM:
Keep tool responses concise. Large responses consume tokens and may confuse the LLM. Return only the information needed to continue the conversation.

Best Practices

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
Leverage PHP’s type system to constrain inputs:
  • Use int, float, bool for primitives
  • Use Enums for fixed option sets
  • Use DataModels for complex structures
  • Use nullable types for optional parameters
Return informative error messages to LLM instead of throwing exceptions:
Each tool should do one thing well. Split complex operations into multiple 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
If you need more tools, consider:
  • 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.