Skip to main content

Quickstart

Start with creation of the listener:
Name AgentListener is used as example, you can name it as you wish
Import LarAgent event of your choice (You can find a list below) and implement handle method:
Open the app\Providers\AppServiceProvider.php and register event with listener:
You are good to go! Anytime, when agent will be initialized, the handle method will be executed.

Available Events

AgentDTO Structure

Each event that includes agentDto uses the following Data Transfer Object structure:

Lifecycle Events

These events track the main lifecycle stages of an agent conversation.

AgentInitialized

Dispatched when the agent completes its initialization process and is ready to handle conversations. Namespace:
Available Data:
agentDto
AgentDTO
required
Complete agent configuration including provider, tools, instructions, and response schema.
Example:

ConversationStarted

Dispatched when the respond() method begins execution, marking the start of a new conversation turn. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration at the start of the conversation.
Example:

ConversationEnded

Dispatched when the respond() method completes execution, indicating the end of a conversation turn. Namespace:
Available Data:
agentDto
AgentDTO
required
Final agent configuration at the end of the conversation.
message
MessageInterface|array|null
required
The final response message or null if no response was generated.
Message instance includes the token usage data. You can use toArrayWithMeta to get it as an array
Example:

ToolChanged

Dispatched when tools are dynamically added to or removed from the agent during runtime. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
tool
ToolInterface
required
The tool instance that was added or removed.
added
boolean
required
true if the tool was added, false if it was removed.
Example:

AgentCleared

Dispatched when the agent’s state is cleared, typically resetting conversation history and context. Namespace:
Available Data:
agentDto
AgentDTO
required
Agent configuration at the time of clearing.
Example:

EngineError

Dispatched when an error occurs in the LLM engine during processing. Namespace:
Available Data:
agentDto
AgentDTO
required
Agent configuration when the error occurred.
exception
Throwable
required
The exception that was thrown by the LLM engine.
Example:

Hook Events (Before/After)

These events provide hooks before and after critical operations, allowing you to modify behavior or track execution flow.

BeforeReinjectingInstructions

Dispatched before instructions are reinjected into the conversation history. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
chatHistory
ChatHistoryInterface
required
The chat history interface before instruction reinjection.
Example:

BeforeSend

Dispatched just before adding message in chat history Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
history
ChatHistoryInterface
required
The complete conversation history being sent.
message
MessageInterface|null
required
The message about to be sent, or null if sending history only.
Example:

AfterSend

Dispatched immediately after adding LLM response to Chat history. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
history
ChatHistoryInterface
required
The conversation history that was sent.
message
MessageInterface
required
The message that was successfully sent.
Example:

BeforeSaveHistory

Dispatched before persisting the conversation history to storage. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
history
ChatHistoryInterface
required
The conversation history about to be saved.
Example:

BeforeResponse

Dispatched before sending message to LLM Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
history
ChatHistoryInterface
required
Current conversation history including the new response.
message
MessageInterface|null
required
The message that is about to sent to the LLM API or null.
Example:

AfterResponse

Dispatched right after receiving a message from LLM API Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
message
MessageInterface
required
The response message.
Example:

BeforeToolExecution

Dispatched just before a tool is executed by the agent. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
tool
ToolInterface
required
The tool instance about to be executed.
toolCall
ToolCallInterface
required
The tool call instance is going to be executed. Get details with getId, getToolName, & getArguments methods
Example:

AfterToolExecution

Dispatched immediately after a tool completes execution. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration.
tool
ToolInterface
required
The tool instance that was executed.
toolCall
ToolCallInterface
required
The tool call instance is going to be executed. Get details with getId, getToolName, & getArguments methods
result
mixed
required
The result returned by the tool execution.
Example:

BeforeStructuredOutput

Dispatched before processing structured output from the LLM response. Namespace:
Available Data:
agentDto
AgentDTO
required
Current agent configuration including the response schema.
response
array
required
The raw structured response array before processing.
Example: