An Agent generates various Events during execution—user input, model output, Tool calls, errors, and more. Understanding the Event system lets you insert your own logic at the right places.
Event Types
ADK Go has several main Event types:
| Event Type | Trigger | Purpose |
|---|---|---|
EventUserMessage | User sends a message | Logging, processing user input |
EventAgentMessage | Agent returns a message | Streaming output, logging |
EventToolCall | Agent calls a Tool | Monitoring Tool calls, modifying Tool arguments |
EventToolResult | Tool returns a result | Processing Tool results, modifying results |
EventSessionStart | Session created | Initialization, permission checks |
EventSessionEnd | Session ended | Resource cleanup, statistics |
EventError | Error occurs | Error alerts, automatic recovery |
Registering an Event Handler
agent, _ := llmagent.New(llmagent.Config{
Name: "my-agent",
Model: model,
Instruction: "You are an assistant",
EventHandlers: []agent.EventHandler{
{
EventType: agent.EventToolCall,
Handler: func(ctx context.Context, e *agent.Event) error {
log.Printf("Tool called: %s", e.ToolName)
return nil
},
},
{
EventType: agent.EventError,
Handler: func(ctx context.Context, e *agent.Event) error {
log.Printf("Agent error: %v", e.Error)
return nil // return nil to continue, return error to interrupt
},
},
},
})
Practical Event Handler Patterns
Pattern 1: Logging
{
EventType: agent.EventUserMessage,
Handler: func(ctx context.Context, e *agent.Event) error {
log.Printf("User input: %s", e.Message)
return nil
},
},
Pattern 2: Modifying Tool Arguments
{
EventType: agent.EventToolCall,
Handler: func(ctx context.Context, e *agent.Event) error {
// Automatically add current user info to Tool calls
e.ToolArgs["user_id"] = ctx.Value("user_id")
return nil
},
},
Pattern 3: Automatic Error Recovery
{
EventType: agent.EventError,
Handler: func(ctx context.Context, e *agent.Event) error {
if strings.Contains(e.Error.Error(), "network") {
// Network error, wait 1 second then retry
time.Sleep(time.Second)
return nil // continue execution
}
return e.Error // other errors: interrupt
},
},
FAQ
Q: What happens if an Event Handler returns an error? A: The Agent will stop execution and the error will propagate upward. Decide whether to interrupt based on the error type.
Q: Can I register multiple Handlers? A: Yes. Multiple Handlers for the same EventType run in registration order.
Q: Are Events handled synchronously or asynchronously? A: Synchronously. The subsequent flow continues only after the Handler finishes.
Next Step
Now that you understand the Event system, let’s look at Context Caching—how to cache context and reduce token consumption.
← State Read/Write | Context Caching →
Want to learn more hands-on Go ADK? Follow the “Full Stack Summit — Dream Beast Programming” WeChat official account for weekly Go / AI programming tutorials.
