Koog 1.0 idioms, gotchas, and scaffolding skills for Kotlin agents on the JVM
87
88%
Does it follow best practices?
Impact
87%
1.85xAverage score across 45 eval scenarios
Advisory
Suggest reviewing before use
Process steps in order. Do not skip ahead.
implementation("ai.koog:agents-features-event-handler:1.0.0")The umbrella koog-agents does not include the event handler — add it explicitly.
Proceed immediately to Step 2.
Write the modified agent construction and the dependency to disk with explicit Path: labels (same convention as scaffold-agent):
Path: src/main/kotlin/com/example/Main.kt — modified agent construction (or whichever file contains the AIAgent(...) call)Path: build.gradle.kts — appended dependency lineCreate files if they don't exist. Do not respond with prose only.
import ai.koog.agents.features.eventhandler.handleEvents
val agent = AIAgent(
promptExecutor = ...,
llmModel = ...,
systemPrompt = "...",
) {
handleEvents {
onToolCallStarting { ctx ->
println("→ Tool '${ctx.toolName}' called with args: ${ctx.toolArgs}")
}
onToolCallFinished { ctx ->
println("← Tool '${ctx.toolName}' returned: ${ctx.result}")
}
onLLMRequestStarting { ctx ->
println("LLM ▶ ${ctx.model}")
}
onLLMRequestFinished { ctx ->
println("LLM ◀ ${ctx.tokenUsage}")
}
onAgentFinished { ctx ->
println("✓ Agent finished after ${ctx.iterations} iterations")
}
onAgentError { ctx ->
System.err.println("✗ Agent error: ${ctx.error}")
}
}
}Each callback receives a typed context object with what the event carried. Don't allocate inside the hot path — for high-throughput agents, an event handler that synchronously logs every tool call to stdout will dominate the run.
Proceed immediately to Step 3.
Event handlers and OpenTelemetry coexist; they don't replace each other:
gen_ai.client.token.usage, gen_ai.client.tool.count), distributed traces, dashboardsIf the user wants production observability, install both — the event handler for the development surface, OpenTelemetry for the production signal. See add-observability for the OTel install.
If the user only wants demo-time visualization, the event handler alone is enough — no OTel collector to stand up.
Finish here.
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10
scenario-11
scenario-12
scenario-13
scenario-14
scenario-15
scenario-16
scenario-17
scenario-18
scenario-19
scenario-20
scenario-21
scenario-22
scenario-23
scenario-24
scenario-25
scenario-26
scenario-27
scenario-28
scenario-29
scenario-30
scenario-31
scenario-32
scenario-33
scenario-34
scenario-35
scenario-36
scenario-37
scenario-38
scenario-39
scenario-40
scenario-41
scenario-42
scenario-43
scenario-44
scenario-45
skills
add-observability
add-persistence
add-rag
add-structured-output
add-token-budgeting
add-tool
cache-llm-calls
define-prompt
domain-model-subtask-pipeline
references
enable-prompt-caching
handle-agent-events
manage-state
migrate-from-0-x
model-planner-subtasks
persist-chat-history
query-sql-from-agent
scaffold-agent
snapshot-and-restore
test-koog-agents
trace-agent-internals
use-attachments
use-functional-agent
use-llm-node-variants
use-planner
wire-a2a
wire-acp-server
wire-ktor-server
wire-mcp-server
wire-spring-boot