Skip to content

Commit 24c1b20

Browse files
committed
Update documentation for agent feature events
1 parent 50334a8 commit 24c1b20

File tree

8 files changed

+542
-426
lines changed

8 files changed

+542
-426
lines changed

agents/agents-features/Module.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,76 @@
11
# Module agents:agents-features
22

3-
Provides implementations of useful features of AI agents, such as Tracing, Debugger, EventHandler, Memory, OpenTelemetry, Snapshot, and more.
3+
A collection of plug-and-play features for Koog AI agents. These features hook into the agent execution pipeline to observe, enrich, and extend behavior.
44

5-
### Overview
5+
## What is inside
66

7-
Features integrate with the agent pipeline via interceptor hooks and consume standardized Feature Events emitted during agent execution. These events are defined in the agents-core module under `ai.koog.agents.core.feature.model.events`. After the 0afb32b refactor, event and interceptor names are unified across the system.
7+
Each feature lives in its own submodule, you only depend on what you need. Commonly used features include:
8+
- Tracing: End-to-end spans for agent runs and LLM, or Tool calls. Great for local dev and production observability;
9+
- Debugger: Step-through style inspection of the agent pipeline;
10+
- EventHandler: Subscribe to standardized agent events and react (log, metrics, custom side effects);
11+
- Memory: Pluggable memory interfaces for storing and retrieving agent context;
12+
- OpenTelemetry: OTel exporters and wiring for spans emitted by the agent pipeline;
13+
- Snapshot: Persist and restore agent snapshots for reproducibility and time-travel debugging.
814

9-
### Standard Feature Events
15+
Check each feature’s own README/Module docs for details and advanced configuration.
1016

11-
The canonical description and definitions of Feature Events now live in the agents-core module. See:
17+
## How features integrate
1218

13-
- agents-core module docs: ../agents-core/Module.md (section "Standard Feature Events")
14-
- Kotlin definitions: package `ai.koog.agents.core.feature.model.events`
19+
Features integrate via interceptor hooks and consume standardized events emitted during an agent execution. These events are defined in the agents-core module under:
20+
- ai.koog.agents.core.feature.model.events
1521

16-
Features in this module (Tracing, Debugger, EventHandler, etc.) consume these events to provide logging, tracing, monitoring, and remote inspection.
22+
Typical events include:
23+
- AgentStarting/Completed
24+
- LLMCallStarting/Completed
25+
- ToolCallStarting/Completed
1726

18-
### Using in your project
27+
Features can listen to these events, mutate context when appropriate, and publish additional events for downstream consumers.
1928

20-
Add the desired feature dependency, for example:
29+
## Installing features
2130

22-
```kotlin
23-
dependencies {
24-
implementation("ai.koog.agents:agents-features-trace:$version")
25-
implementation("ai.koog.agents:agents-features-debugger:$version")
26-
}
27-
```
28-
29-
Install a feature in the agent builder:
31+
Install features when constructing your agent. Multiple features can be installed together; they remain decoupled and communicate via events.
3032

3133
```kotlin
3234
val agent = createAgent(/* ... */) {
33-
install(Tracing)
34-
install(Debugger)
35+
install(Tracing) {
36+
// Tracing configuration
37+
}
38+
install(OpenTelemetry) {
39+
// OTel configuration
40+
}
3541
}
3642
```
3743

38-
### Using in unit tests
44+
Consult each feature’s README for exact configuration options and defaults.
3945

40-
Most features can be installed in tests; they honor testing configuration and can be pointed to test writers/ports.
46+
## Using in unit tests
4147

42-
### Example of usage
48+
Features are test-friendly. They honor testing configurations and can be directed to in-memory writers/ports.
49+
- Install the same feature in tests to capture events deterministically.
50+
- Point outputs to test stubs to assert behavior (e.g., assert a specific sequence of Feature Events).
51+
- Prefer higher sampling in tests so important transitions are recorded.
52+
53+
Example (pseudo):
54+
```kotlin
55+
@Test
56+
fun testAgentEmitsExpectedEvents() {
57+
val events = MutableListWriter<FeatureEvent>()
58+
createAgent {
59+
install(EventHandler) {
60+
writer = events
61+
}
62+
}.use { agent ->
63+
agent.run("Hello")
64+
assertTrue(events.any { it is LlmCallRequested })
65+
}
66+
}
67+
```
4368

44-
See each feature's Module/README in its submodule for concrete examples (Tracing, Debugger, EventHandler, Memory, OpenTelemetry, Snapshot).
69+
## Where to learn more
70+
See each feature’s Module/README in its submodule for concrete examples and advanced setup:
71+
- Tracing
72+
- Debugger
73+
- EventHandler
74+
- Memory
75+
- OpenTelemetry
76+
- Snapshot

docs/docs/agent-event-handlers.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)