Skip to main content
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

Tool creation artisan command is comming soon…

Tool choice

You can set the tool choice for your agent using the following methods:
forceTool method requires tool’s name as a parameter.
toolRequired & forceTool is set only at the first call, after that it will automatically switched to ‘auto’ avoiding infinite loop.
If tools are registered, default value is ‘auto’, otherwise it’s ‘none’.

Phantom Tools 👻

Phantom Tools are dynamically registered tools that are not executed on the LarAgent side, but instead returns ToolCallMessage
Phantom Tools are particularly useful when:
  • You need to integrate with external services dynamically
  • You want to handle tool execution outside of LarAgent
  • You need to make tool registration/execution available from API
Phantom Tools follow the same interface as regular tools but instead of automatical execution, they return ToolCallMessage instance, providing more flexibility in terms of when and how they are executed.

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
Set toolChoice property
Enable/Disable parallel tool calls

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