Skip to main content
Hooks provide extension points to monitor and modify agent behavior at specific execution stages.

Hook Overview

AgentScope Java uses a unified event model where all hooks implement the onEvent(HookEvent) method:
  • Event-based: All agent activities generate events
  • Type-safe: Pattern matching on event types
  • Priority-ordered: Hooks execute by priority (lower value = higher priority)
  • Modifiable: Some events allow modification of execution context

Supported Events

Creating Hooks

Basic Hook

Hook with Priority

Modifying Events

Some events allow modification:

Configure Hooks in Agent

Register hooks when building an agent:
Hooks are immutable after agent construction.

Built-in JSONL Trace Exporter

For local debugging and offline troubleshooting, AgentScope Java provides a built-in JSONL exporter:
Warning: the JSONL trace exporter writes full prompts, messages, tool inputs, and error stack traces to local files. These records may contain sensitive user data, credentials, or other secrets, so only enable it in trusted environments and handle the output file as sensitive data.

Hook Examples

Monitoring Tool Execution

Track tool calls:

Monitoring Errors

Monitor and handle errors:

Complete Example

See the complete Hook example:
  • agentscope-examples/documentation/quickstart/src/main/java/io/agentscope/examples/quickstart/HookExample.java
Run the example: