|
| 1 | +# Event handlers |
| 2 | + |
| 3 | +You can monitor and respond to specific events during the agent workflow by using event handlers for logging, testing, debugging, and extending agent behavior. |
| 4 | + |
| 5 | +## Feature overview |
| 6 | + |
| 7 | +The EventHandler feature lets you hook into various agent events. It serves as an event delegation mechanism that: |
| 8 | + |
| 9 | +- Manages the lifecycle of AI agent operations. |
| 10 | +- Provides hooks for monitoring and responding to different stages of the workflow. |
| 11 | +- Enables error handling and recovery. |
| 12 | +- Facilitates tool invocation tracking and result processing. |
| 13 | + |
| 14 | +<!--## Key components |
| 15 | +
|
| 16 | +The EventHandler entity consists of five main handler types: |
| 17 | +
|
| 18 | +- Initialization handler that executes at the initialization of an agent run |
| 19 | +- Result handler that processes successful results from agent operations |
| 20 | +- Error handler that handles exceptions and errors that occur during execution |
| 21 | +- Tool call listener that notifies when a tool is about to be invoked |
| 22 | +- Tool result listener that processes the results after a tool has been called--> |
| 23 | + |
| 24 | + |
| 25 | +### Installation and configuration |
| 26 | + |
| 27 | +The EventHandler feature integrates with the agent workflow through the `EventHandler` class, |
| 28 | +which provides a way to register callbacks for different agent events, and can be installed as a feature in the agent configuration. For details, see [API reference](https://api.koog.ai/agents/agents-features/agents-features-event-handler/ai.koog.agents.features.eventHandler.feature/-event-handler/index.html). |
| 29 | + |
| 30 | +To install the feature and configure event handlers for the agent, do the following: |
| 31 | + |
| 32 | +<!--- INCLUDE |
| 33 | +import ai.koog.agents.core.agent.AIAgent |
| 34 | +import ai.koog.agents.features.eventHandler.feature.handleEvents |
| 35 | +import ai.koog.prompt.executor.llms.all.simpleOllamaAIExecutor |
| 36 | +import ai.koog.prompt.llm.OllamaModels |
| 37 | +
|
| 38 | +val agent = AIAgent( |
| 39 | + promptExecutor = simpleOllamaAIExecutor(), |
| 40 | + llmModel = OllamaModels.Meta.LLAMA_3_2, |
| 41 | +) { |
| 42 | +--> |
| 43 | +<!--- SUFFIX |
| 44 | +} |
| 45 | +--> |
| 46 | + |
| 47 | +```kotlin |
| 48 | +handleEvents { |
| 49 | + // Handle tool calls |
| 50 | + onToolCallStarting { eventContext -> |
| 51 | + println("Tool called: ${eventContext.tool.name} with args ${eventContext.toolArgs}") |
| 52 | + } |
| 53 | + // Handle event triggered when the agent completes its execution |
| 54 | + onAgentCompleted { eventContext -> |
| 55 | + println("Agent finished with result: ${eventContext.result}") |
| 56 | + } |
| 57 | + |
| 58 | + // Other event handlers |
| 59 | +} |
| 60 | +``` |
| 61 | +<!--- KNIT example-events-01.kt --> |
| 62 | + |
| 63 | +For more details about event handler configuration, see [API reference](https://api.koog.ai/agents/agents-features/agents-features-event-handler/ai.koog.agents.features.eventHandler.feature/-event-handler-config/index.html). |
| 64 | + |
| 65 | +You can also set up event handlers using the `handleEvents` extension function when creating an agent. |
| 66 | +This function also installs the event handler feature and configures event handlers for the agent. Here is an example: |
| 67 | + |
| 68 | +<!--- INCLUDE |
| 69 | +import ai.koog.agents.core.agent.AIAgent |
| 70 | +import ai.koog.agents.features.eventHandler.feature.handleEvents |
| 71 | +import ai.koog.prompt.executor.llms.all.simpleOllamaAIExecutor |
| 72 | +import ai.koog.prompt.llm.OllamaModels |
| 73 | +--> |
| 74 | +```kotlin |
| 75 | +val agent = AIAgent( |
| 76 | + promptExecutor = simpleOllamaAIExecutor(), |
| 77 | + llmModel = OllamaModels.Meta.LLAMA_3_2, |
| 78 | +){ |
| 79 | + handleEvents { |
| 80 | + // Handle tool calls |
| 81 | + onToolCallStarting { eventContext -> |
| 82 | + println("Tool called: ${eventContext.tool.name} with args ${eventContext.toolArgs}") |
| 83 | + } |
| 84 | + // Handle event triggered when the agent completes its execution |
| 85 | + onAgentCompleted { eventContext -> |
| 86 | + println("Agent finished with result: ${eventContext.result}") |
| 87 | + } |
| 88 | + |
| 89 | + // Other event handlers |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | +<!--- KNIT example-events-02.kt --> |
0 commit comments