梦兽编程
AI_SUITE

Event System: Understanding the Agent Event Flow

A detailed explanation of Event types in ADK Go, how to use EventHandler, and how to listen for and handle events during Agent execution.

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 TypeTriggerPurpose
EventUserMessageUser sends a messageLogging, processing user input
EventAgentMessageAgent returns a messageStreaming output, logging
EventToolCallAgent calls a ToolMonitoring Tool calls, modifying Tool arguments
EventToolResultTool returns a resultProcessing Tool results, modifying results
EventSessionStartSession createdInitialization, permission checks
EventSessionEndSession endedResource cleanup, statistics
EventErrorError occursError 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.

Frequently Asked Questions

What is an Event in ADK Go?

An Event is a signal emitted during Agent execution, such as when the Agent starts, completes a step, calls a tool, or encounters an error.

What are the common Event types?

Common Event types include start, finish, tool-call, tool-result, and error. Each type carries different metadata about what happened during execution.

How do I listen for Events?

Register an EventHandler with the Agent or Session. The handler receives each Event and can log it, update UI, or trigger side effects.

Can multiple handlers process the same Event?

Yes, you can register multiple EventHandlers. ADK Go dispatches the Event to every registered handler in order.

When should I use Events?

Use Events when you need observability, progress tracking, custom logging, or to react to specific milestones during Agent execution.