Hooks

Hooks execute custom JavaScript code at specific cycles of your bot’s operation. For example:

You can use Hooks to intercept and modify messages, perform logging, integrate with external systems, and more.

Overview

When you add a new Hook, it defines a function that executes every time your bot reaches a certain cycle of its operation.

All Hooks take an event parameter, which contains information about the event that triggered the Hook.

Add a Hook

  1. Navigate to the Hooks section in the Studio’s left navigation bar.
  2. Select Create Hook, then choose a type for the Hook.
  3. Choose a name for the Hook. Done!

Hook types

Here’s a breakdown of each type of Hook:

Before Incoming

This Hook type executes its code before your bot processes an incoming event.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.

After Incoming

This Hook type executes its code after the incoming message has been processed, but before the bot has responded.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.

Before Outgoing

This Hook type executes its code before the bot’s reply is sent to the user.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.
outgoingEvent?IO.OutgoingEvent

The message sent by the bot

Before LLMz Execution

This Hook type executes its code before the LLMz engine begins its execution.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.
context?LLMZ.CreateContext

The current context provided to the LLMz

After LLMz Execution

This Hook type executes its code after the LLMz engine finishes its execution.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.
execution?LLMZ.ExecutionResult

The result of the execution

After LLMz iteration

Depending on how demanding the task is, the LLMz may loop through multiple iterations before finishing its execution. This Hook type executes its code after each iteration.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.
iteration?LLMZ.IterationResult

The result of the iteration

After Conversation End

This Hook type executes after the conversation has explicitly ended (transitioned to an End Node)

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.

After Turn End

This Hook type executes after a full turn has completed—meaning the bot has received user input, processed it, and responded.

You can use a Hook of this type to track AI spend and token cost for your bot.

Parameters:

eventIncomingEvent
Information about the last incoming event in your bot's execution.
metadata?IO.TurnMetadata

The turn’s metadata

Troubleshooting

If your Hook isn’t behaving as expected, you can check your bot’s logs to diagnose any problems.