Tools
Tools extend your agent’s capabilities, allowing it to perform tasks like sending messages, making API calls, or executing commands.
Tools (also known as function calling) allow your AI agents to interact with external systems, APIs, and services, greatly expanding their capabilities beyond simple text generation.
Tool Configuration
Tools in LarAgent can be configured using these properties in your Agent class:
You can set $parallelToolCalls
to null
if you want to remove it from the
request, as some models (o1) do not support parallel tool calls.
Creating Tools
There are three ways to create and register tools in your agent:
#[Tool] Attribute
Best for service-based or static tools specific to a single agent.
registerTools Method
Ideal for dynamically creating tools based on runtime conditions or user state.
Tool Classes
Perfect for complex tools that may be reused across multiple agents or require extensive logic.
1. Using the Tool Attribute
The simplest approach is using the #[Tool]
attribute to transform your agent’s methods into tools:
The agent will automatically register the tool with the given description and extract method information, including parameter names and types.
You can add tools without properties (= Method without parameters)
Using Enum Types with Tools
You can use PHP Enums to provide the AI with a specific set of options to choose from, as well as provide separate descriptions for each property (argument):
It’s recommended to use the #[Tool]
attribute with static methods if there’s
no need for the agent instance ($this
).
2. Using the registerTools Method
This method allows you to programmatically create and register tools using the LarAgent\Tool
class:
setCallback
method accepts any php
callable, such as
a function name, a closure, or a class method.
3. Using Tool Classes
For complex tools, you can create dedicated tool classes and add them to the $tools
property:
Example Tool Class
Chainable Tool Methods
You can dynamically add or remove tools during runtime using withTool
and removeTool
methods:
Add tool with instance
Add tool with predefined tool class
Remove tool by name
Remove tool by instance
Remove tool by class name
Best Practices
Do create separate tool classes for complex functionality that might be reused
Do provide clear, descriptive names and parameter descriptions
Do use Enums when you need to restrict the AI to specific options
Don’t create tools with ambiguous functionality or unclear parameter requirements
Don’t expose sensitive operations without proper validation and security checks