For the complete documentation index, see llms.txt. This page is also available as Markdown.

Event System

The BoxLang AI module provides a comprehensive event system that allows you to intercept, monitor, and customize AI operations at various stages. These events give you fine-grained control over the AI lifecycle, from object creation to request/response handling.

📋 Table of Contents


🔍 Overview

The event system allows you to monitor, modify, validate, audit, secure, and customize AI operations without modifying core code.

All Available Events

#
Event
When Fired
Key Data

1

Message template created

message

2

Chat request object instantiated

aiRequest

3

Before provider creation

provider, apiKey

4

Provider instance created

provider

5

Provider not found

provider, options

6

Model runnable created

model, service

7

Transform runnable created

transform

8

Agent instance created

agent, name

9

Before agent execution

agent, input

10

After agent execution

agent, result

11

Before model execution

model, request

12

Before chat HTTP request

dataPacket, aiRequest, provider

13

After chat HTTP response

response, rawResponse, provider

14

After model execution completes

model, request, results

15

Before embedding generation

embeddingRequest, service

16

Before embedding HTTP request

dataPacket, embeddingRequest, provider

17

After embedding HTTP response

response, rawResponse, provider

18

After embedding generation

embeddingRequest, service, result

19

Tool created

tool, name, description

20

Before tool execution

tool, name, arguments

21

After tool execution

tool, results, executionTime

22

Document loader created

loader, type

23

Memory instance created

memory, type

24

Before pipeline starts

sequence, stepCount, input

25

After pipeline completes

sequence, result, executionTime

26

Error occurs

error, errorMessage, provider, canRetry

27

Rate limit detected (429)

provider, statusCode, retryAfter

28

Token usage available

provider, operation, model, promptTokens, completionTokens, totalTokens, aiRequest, usage, timestamp

29

MCP server instance created

server, name, description

30

MCP server instance removed

name

31

Before processing MCP request

server, requestData, serverName

32

After processing MCP response

server, response, requestData

33

Exception during MCP operations

server, context, exception, request details

34

Before TTS request is sent

speechRequest, service

35

After TTS response received

speechRequest, service, result

36

Before STT request is sent

transcriptionRequest, service

37

After STT response received

transcriptionRequest, service, result

38

Before audio translation request

transcriptionRequest, service

39

After audio translation response

transcriptionRequest, service, result

40

Message added to HybridMemory

memory, message

41

Vector semantic search runs

memory, query, limit, results

42

Before image generation request

imageRequest, service

43

After image generation response

imageRequest, service, result

44

Image request object created

imageRequest

45

Image response received

response, rawResponse, provider

46

Agent registered

agent, key, module

47

Agent unregistered

key, module

48

MCP server paused

server, name

49

MCP server resumed

server, name

50

MCP client HTTP request

client, baseURL, operation, name

51

MCP client HTTP response

client, baseURL, operation, response

52

MCP client HTTP error

client, baseURL, operation, error

🔄 Event Lifecycle Diagram

📊 Event Categories


🔌 Event Interception

To listen to events, create an interceptor and register it in your module or application.

🏗️ Interceptor Architecture

Creating an Interceptor

Registering an Interceptor

For BoxLang Modules (in ModuleConfig.bx):

For Applications/Scripts (use BoxRegisterInterceptor() BIF):

📖 Reference: BoxRegisterInterceptor() Documentation


📡 Available Events

1. onAIMessageCreate

Fired when an AI message object is created via aiMessage().

When: Message template creation Frequency: Once per aiMessage() call

Event Arguments

Argument
Type
Description

message

AiMessage

The created message object

Example


2. onAIChatRequestCreate

Fired when an AI chat request object is created via aiChatRequest().

When: Chat request object instantiation Frequency: Once per aiChatRequest() call

Event Arguments

Argument
Type
Description

aiRequest

AiChatRequest

The created request object


3. onAIProviderRequest

Fired when a provider is requested from the factory.

When: Before provider/service is created or retrieved Frequency: Once per provider request

Event Arguments

Argument
Type
Description

provider

String

Provider name (e.g., "openai", "claude")

apiKey

String

API key (if provided)

params

Struct

Request parameters

options

Struct

Request options


4. onAIProviderCreate

Fired when a provider/service instance is created.

When: After provider instantiation Frequency: Once per unique provider instance

Event Arguments

Argument
Type
Description

provider

IService

The created service instance


5. onAIModelCreate

Fired when an AI model runnable is created via aiModel().

When: Model wrapper creation Frequency: Once per aiModel() call

Event Arguments

Argument
Type
Description

model

AiModel

The created model runnable

service

IService

The underlying service


6. onAITransformCreate

Fired when a transform runnable is created via aiTransform().

When: Transform function creation Frequency: Once per aiTransform() call

Event Arguments

Argument
Type
Description

transform

AiTransformRunnable

The created transform runnable


7. beforeAIModelInvoke

Fired before an AI model is invoked (before sending to provider).

When: Before model execution Frequency: Every model invocation

Event Arguments

Argument
Type
Description

model

AiModel

The model being invoked

request

AiChatRequest

The request being sent


8. onAIChatRequest

Fired immediately before sending the HTTP request to the AI provider for chat operations.

When: Before chat HTTP request Frequency: Every chat API call (including streaming)

Event Arguments

Argument
Type
Description

dataPacket

Struct

The HTTP request data packet

aiRequest

AiChatRequest

The AI request object

provider

IService

The service making the request


9. onAIChatResponse

Fired after receiving the HTTP response from the AI provider for chat operations.

When: After chat HTTP response Frequency: Every chat API call (including streaming)

Event Arguments

Argument
Type
Description

aiRequest

AiChatRequest

The original request

response

Struct

The deserialized response

rawResponse

Struct

The raw HTTP response

provider

IService

The service that made the request


10. afterAIModelInvoke

Fired after an AI model completes its invocation.

When: After model execution completes Frequency: Every model invocation

Event Arguments

Argument
Type
Description

model

AiModel

The model that was invoked

request

AiChatRequest

The request that was sent

results

Any

The results returned by the model


11. onAIToolCreate

Fired when an AI tool is created via aiTool().

When: Tool creation Frequency: Once per aiTool() call

Event Arguments

Argument
Type
Description

tool

Tool

The created tool instance

name

String

Tool name

description

String

Tool description


12. beforeAIToolExecute

Fired immediately before a tool's callable function is executed.

When: Before tool execution Frequency: Every tool call

Event Arguments

Argument
Type
Description

tool

Tool

The tool being executed

name

String

Tool name

arguments

Struct

Arguments passed to the tool


13. afterAIToolExecute

Fired immediately after a tool's callable function completes execution.

When: After tool execution Frequency: Every tool call

Event Arguments

Argument
Type
Description

tool

Tool

The tool that was executed

name

String

Tool name

arguments

Struct

Arguments passed to the tool

results

Any

Results returned by the tool

executionTime

Numeric

Execution time in milliseconds


14. onAIError

Fired when an error occurs during AI operations (chat, embeddings, or streaming).

When: Before throwing provider errors Frequency: Every error condition

Event Arguments

Argument
Type
Description

error

Any

The error object/message from provider

errorMessage

String

Formatted error message

provider

IService

The provider where error occurred

operation

String

Operation type: "chat", "embeddings", "stream"

aiRequest

AiChatRequest

The request that caused the error (if available)

embeddingRequest

AiEmbeddingRequest

For embedding errors

canRetry

Boolean

Whether operation can be retried


15. onAIRateLimitHit

Fired when a provider returns a 429 (rate limit) HTTP status code.

When: When rate limit is detected Frequency: Every rate limit response

Event Arguments

Argument
Type
Description

provider

IService

The provider that hit rate limit

operation

String

Operation type: "chat", "embeddings"

statusCode

String

HTTP status code (429)

errorData

Struct

Error response from provider

aiRequest

AiChatRequest

The request that hit the limit

retryAfter

String

Retry-After header value (if present)


16. beforeAIPipelineRun

Fired before a runnable pipeline sequence begins execution.

When: Before pipeline execution starts Frequency: Every pipeline run

Event Arguments

Argument
Type
Description

sequence

AiRunnableSequence

The sequence being executed

name

String

Sequence name

stepCount

Numeric

Number of steps in pipeline

steps

Array

Array of step information

input

Any

Initial input to pipeline

params

Struct

Parameters passed to pipeline

options

Struct

Options passed to pipeline


17. afterAIPipelineRun

Fired after a runnable pipeline sequence completes execution.

When: After pipeline execution completes Frequency: Every pipeline run

Event Arguments

Argument
Type
Description

sequence

AiRunnableSequence

The sequence that was executed

name

String

Sequence name

stepCount

Numeric

Number of steps in pipeline

steps

Array

Array of step information

input

Any

Initial input to pipeline

result

Any

Final result from pipeline

executionTime

Numeric

Total execution time in milliseconds


18. onAITokenCount

Fired when token usage information is available from the AI provider response.

When: After receiving response with usage data Frequency: Every successful API call that returns usage

Event Arguments

Argument
Type
Description

provider

IService

The provider instance

operation

String

Operation type: "chat"

model

String

Model ID used for the request

promptTokens

Numeric

Input tokens used

completionTokens

Numeric

Output tokens generated

totalTokens

Numeric

Total tokens (prompt + completion)

aiRequest

AiChatRequest

The originating chat request object

usage

Struct

Full raw usage object from provider

timestamp

DateTime

When the event was fired


Multi-Tenant Usage Tracking (v2.1.0+)

Track AI usage per tenant for billing and cost allocation. Works with aiChat(), aiChatAsync(), aiChatStream(), and AI Agent runnables.

Interceptor for Multi-Tenant Billing:

Multi-Provider Tracking Example:

Usage Metadata Examples:

Benefits of Multi-Tenant Tracking:

  • Accurate Billing: Attribute AI costs to specific tenants/customers

  • Cost Allocation: Track usage by department, project, or cost center

  • Quota Management: Enforce per-tenant usage limits

  • Analytics: Understand which tenants/projects use AI most

  • Chargeback: Generate detailed usage reports for internal billing

  • Provider Agnostic: Works with all AI providers (OpenAI, Bedrock, Ollama, etc.)


Event Priority Reference

Events fire in this order during a typical AI chat with tools:

  1. onAIMessageCreate - Message template created

  2. onAIRequestCreate - Request object created

  3. onAIModelCreate - Model wrapper created

  4. onAIToolCreate - Tool(s) created (if using tools)

  5. beforeAIPipelineRun - Pipeline about to start (if using pipelines)

  6. beforeAIModelInvoke - Model about to be invoked

  7. onAIRequest - HTTP request about to be sent

  8. onAIRateLimitHit - If rate limit encountered

  9. onAIResponse - HTTP response received

  10. onAITokenCount - Token usage tracked

  11. beforeAIToolExecute - Tool about to execute (if AI requested tool call)

  12. afterAIToolExecute - Tool execution complete

  13. onAIError - If any error occurred

  14. afterAIModelInvoke - Model invocation complete

  15. afterAIPipelineRun - Pipeline execution complete


19. onMCPServerCreate

Fired when a new MCP (Model Context Protocol) server instance is created.

When: MCP server instantiation via MCPServer() Frequency: Once per unique server creation

Event Arguments

Argument
Type
Description

server

MCPServer

The created server instance

name

String

Server name/identifier

description

String

Server description

version

String

Server version


20. onMCPServerRemove

Fired when an MCP server instance is being removed from the registry.

When: Before server removal via MCPServer::removeInstance() Frequency: Once per server removal

Event Arguments

Argument
Type
Description

name

String

Name of the server being removed


21. onMCPRequest

Fired before processing an incoming MCP request (JSON-RPC 2.0).

When: After CORS handling, before request processing Frequency: Every MCP request

Event Arguments

Argument
Type
Description

server

MCPServer

The target server instance

requestData

Struct

Request metadata (method, body, urlParams)

serverName

String

Server identifier


22. onMCPResponse

Fired after processing an MCP response, before returning to client.

When: After request handling, before HTTP response Frequency: Every MCP response

Event Arguments

Argument
Type
Description

server

MCPServer

The server instance

response

Struct

Response data (content, contentType, headers, statusCode)

requestData

Struct

Original request metadata

serverName

String

Server identifier


23. onMCPError

Fired when an exception occurs during MCP server operations.

When: Exception in request handling, class scanning, or other MCP operations Frequency: When errors occur

Event Arguments

Argument
Type
Description

server

MCPServer

The server instance

context

String

Where error occurred (handleRequest, scanClass, etc.)

exception

Struct

Exception object (message, detail, stackTrace, type)

method

String

Request method (context: handleRequest)

requestId

Any

Request ID (context: handleRequest)

params

Struct

Request parameters (context: handleRequest)

responseTime

Numeric

Time elapsed in ms (context: handleRequest)

errorCode

Numeric

RPC error code (context: handleRequest)

classPath

String

Class being scanned (context: scanClass)

Example


24. onAIToolRegistryRegister

Fired when a tool is registered with the AIToolRegistry.

When: After a tool is added via aiToolRegistry().register() or scan() Frequency: Once per registered tool

Event Arguments

Argument
Type
Description

tool

ITool

The tool instance that was registered

key

string

The registry key (e.g., "search" or "search@my-module")

module

string

The module name (empty string if none)

Example


25. onAIToolRegistryUnregister

Fired when a tool is removed from the AIToolRegistry.

When: After a tool is removed via aiToolRegistry().unregister() or unregisterByModule() Frequency: Once per removed tool

Event Arguments

Argument
Type
Description

key

string

The registry key of the removed tool

module

string

The module name extracted from the key

Example


💡 Common Use Cases

1. Request Logging and Monitoring

2. Cost Tracking and Budgeting

3. Response Caching

4. Content Filtering and Moderation

5. Multi-Provider Fallback

6. A/B Testing Different Models

7. Adding Safety Guardrails


✅ Best Practices

1. Keep Event Handlers Lightweight

Event handlers are called frequently. Keep processing minimal:

2. Handle Errors Gracefully

Don't let interceptor errors break AI operations:

3. Document Side Effects

Make it clear what your interceptors modify:

4. Use Naming Conventions

5. Order Matters

Interceptors execute in registration order. Be mindful:

6. Test Interceptors Independently

Write unit tests for your interceptor logic:

7. Make Interceptors Configurable


📚 Examples

Complete Monitoring Solution

Security and Compliance



🔊 Audio Events (34–39)

34. beforeAISpeech

Fired before a text-to-speech request is sent to the provider.

When: Before aiSpeak() dispatches the HTTP request

Event Arguments

Argument
Type
Description

speechRequest

Struct

The full TTS request object

service

any

The AI service provider instance

Example


35. afterAISpeech

Fired after a text-to-speech response has been received from the provider.

When: After aiSpeak() receives and wraps the audio binary in AiSpeechResponse

Event Arguments

Argument
Type
Description

speechRequest

Struct

The original TTS request object

service

any

The AI service provider instance

result

AiSpeechResponse

The response object with audio data

Example


36. beforeAITranscription

Fired before a speech-to-text request is sent to the provider.

When: Before aiTranscribe() dispatches the HTTP request

Event Arguments

Argument
Type
Description

transcriptionRequest

Struct

The full STT request object

service

any

The AI service provider instance

Example


37. afterAITranscription

Fired after a speech-to-text response has been received from the provider.

When: After aiTranscribe() receives the transcription response

Event Arguments

Argument
Type
Description

transcriptionRequest

Struct

The original STT request object

service

any

The AI service provider instance

result

AiTranscriptionResponse

The transcription response object

Example


38. beforeAITranslation

Fired before an audio translation request is sent to the provider.

When: Before aiTranslate() dispatches the HTTP request

Event Arguments

Argument
Type
Description

transcriptionRequest

Struct

The full translation request object

service

any

The AI service provider instance

Example


39. afterAITranslation

Fired after an audio translation response has been received from the provider.

When: After aiTranslate() receives the English text response

Event Arguments

Argument
Type
Description

transcriptionRequest

Struct

The original translation request object

service

any

The AI service provider instance

result

AiTranscriptionResponse

The response with English text

Example


🧠 Memory Events (40–41)

40. onHybridMemoryAdd

Fired when a message is added to a HybridMemory instance. Use this to audit, transform, or track messages as they enter hybrid memory.

When: Inside HybridMemory.add() after the message has been processed

Event Arguments

Argument
Type
Description

memory

HybridMemory

The memory instance receiving the message

message

AiMessage

The message being added

Example


41. onVectorSearch

Fired whenever a semantic search runs against a vector memory store. Use this for observability, caching query results, or logging retrieval quality.

When: After IVectorMemory.getRelevant() or findSimilar() returns results

Event Arguments

Argument
Type
Description

memory

IVectorMemory

The vector memory instance that was searched

query

String

The search query string

limit

Numeric

Maximum number of results requested

results

Array

Array of retrieved documents/messages

Example


42. beforeAIImageGeneration

Fired before an image generation request is sent to the provider.

When: Before aiImage() sends the HTTP request

Event Arguments

Argument
Type
Description

imageRequest

AiImageRequest

The image generation request object

service

IAiImageService

The image service instance

Example


43. afterAIImageGeneration

Fired after an image generation response is received from the provider.

When: After aiImage() receives the response

Event Arguments

Argument
Type
Description

imageRequest

AiImageRequest

The image generation request object

service

IAiImageService

The image service instance

result

AiImageResponse

The generated image response

Example


44. onAIImageRequest

Fired when an image request object is created.

When: During aiImage() request construction

Event Arguments

Argument
Type
Description

imageRequest

AiImageRequest

The image request object

Example


45. onAIImageResponse

Fired after an image response is received from the provider.

When: After the provider returns generated images

Event Arguments

Argument
Type
Description

response

AiImageResponse

The image response

rawResponse

struct

Raw provider response

provider

string

Provider name

Example


46. onAIAgentRegistryRegister

Fired when an agent is registered in the global agent registry.

When: On aiAgentRegistry().register() or aiAgent( register: true )

Event Arguments

Argument
Type
Description

agent

AiAgent

The registered agent

key

string

Registry key (e.g., agentName@module)

module

string

Module namespace

Example


47. onAIAgentRegistryUnregister

Fired when an agent is removed from the global agent registry.

When: On aiAgentRegistry().unregister() or unregisterByModule()

Event Arguments

Argument
Type
Description

key

string

Registry key

module

string

Module namespace

Example


48. onMCPServerPause

Fired when an MCP server is paused.

When: On MCPServer.pause()

Event Arguments

Argument
Type
Description

server

MCPServer

The MCP server instance

name

string

Server name

Example


49. onMCPServerResume

Fired when an MCP server is resumed.

When: On MCPServer.resume()

Event Arguments

Argument
Type
Description

server

MCPServer

The MCP server instance

name

string

Server name

Example


50. onMCPClientRequest

Fired before an MCP client sends an HTTP request.

When: Before every MCP client HTTP call

Event Arguments

Argument
Type
Description

client

MCPClient

The MCP client instance

baseURL

string

Server base URL

operation

string

Operation type (tool, resource, prompt, discovery)

name

string

Tool/resource/prompt name

requestBody

struct

Request payload

Example


51. onMCPClientResponse

Fired after an MCP client receives a successful HTTP response.

When: On successful MCP client HTTP response

Event Arguments

Argument
Type
Description

client

MCPClient

The MCP client instance

baseURL

string

Server base URL

operation

string

Operation type

name

string

Tool/resource/prompt name

response

MCPResponse

The response object

executionTime

numeric

Request duration in ms

statusCode

numeric

HTTP status code

Example


52. onMCPClientError

Fired when an MCP client encounters an HTTP error or network exception.

When: On HTTP errors (bad status / JSON-RPC error) or network exceptions

Event Arguments

Argument
Type
Description

client

MCPClient

The MCP client instance

baseURL

string

Server base URL

operation

string

Operation type

name

string

Tool/resource/prompt name

error

string

Error message

statusCode

numeric

HTTP status code (if available)

executionTime

numeric

Request duration in ms

exception

any

Exception object (if from catch block)

Example


Next Steps

Now that you understand the event system, you can:

  • Monitor: Track AI usage and performance

  • Secure: Add authentication and content filtering

  • Optimize: Implement caching and cost controls

  • Extend: Build custom behaviors without modifying core code

Additional Resources

  • BoxLang Interceptor Documentation: Learn more about the interceptor system

  • Event-Driven Architecture: Best practices for event handling

  • Security Guidelines: Protecting AI operations


Copyright © 2023-2025 Ortus Solutions, Corp

Last updated