Skip to main content

Engine Hooks

LarAgent’s engine hooks provide fine-grained control over the conversation flow, message handling, and tool execution. Unlike agent events that focus on lifecycle, engine hooks dive deeper into the conversation processing pipeline, allowing you to intercept and modify behavior at crucial points.
Each engine hook returns a boolean value where true allows the operation to proceed and false prevents it. In most cases, it’s better to throw and handle exceptions instead of just returning false, since returning false silently stops execution.

Available Engine Hooks

You can override any of these methods in your agent class to customize behavior at different points in the conversation flow.

beforeReinjectingInstructions

This hook is called before the engine reinjects system instructions into the chat history. Use this to modify or validate the chat history before instructions are reinjected or even change the instructions completely.
Instructions are always injected at the beginning of the chat history. The $reinjectInstructionsPer property defines when to reinject the instructions again. By default, it is set to 0 (disabled).
Example: Modify instructions based on chat history

beforeSend & afterSend

These hooks are called before and after a message is added to the chat history. Use them to modify, validate, or log messages. Example: Filter sensitive information and log messages

beforeSaveHistory

Triggered before the chat history is saved. Perfect for validation or modification of the history before persistence. Example: Add metadata before saving

beforeResponse / afterResponse

These hooks are called before sending a message (message is already added to the chat history) to the LLM and after receiving its response. Use them for request/response manipulation or monitoring. Example: Log user messages and validate responses

beforeToolExecution / afterToolExecution

These hooks are triggered before and after a tool is executed. Perfect for tool-specific validation, logging, or result modification. Example: Check permissions and format results
Tool Call has been added from version 0.8.0, if you are using these hooks from version before, please update them, since it is a breaking change!

beforeStructuredOutput

This hook is called before processing structured output. Use it to modify or validate the response structure. Example: Validate and enhance structured output

Practical Use Cases

Here are some common use cases for engine hooks:

Security and Validation

  • Filter out sensitive information before sending messages
  • Validate tool inputs and outputs
  • Enforce rate limiting or usage quotas

Logging and Monitoring

  • Track token usage and conversation metrics
  • Log user interactions for compliance
  • Monitor tool execution performance

Response Modification

  • Enhance responses with additional context
  • Format or standardize tool outputs
  • Add metadata to structured responses

Integration with External Systems

  • Sync conversation data with CRM systems
  • Trigger notifications based on conversation events
  • Update analytics dashboards in real-time

Best Practices

  1. Return values matter - Always return true unless you explicitly want to halt execution
  2. Use exceptions for errors - Throw exceptions where possible with clear messages instead of just returning false
  3. Keep hooks lightweight - Avoid heavy processing in hooks that run frequently
  4. Be careful with references - When modifying referenced parameters (like &$result), ensure you understand the implications
  5. Test thoroughly - Hooks can have subtle effects on the conversation flow, so test them carefully