Logging and Monitoring
Log the agent’s internal state, reasoning process, and actions for debugging and analysis.
Customizing Behavior
Modify or intercept the input/output of core functions to extend or override agent behavior.
Hooking Signature
To simplify the usage, AgentScope provides unified signatures for all hooks.Pre-hooks
All the pre-hooks have the same signature:
A pre-hook template looks like this:
Post-hooks
All post-hooks have an additionaloutput argument, which is the output of the core function or the previous hook. If the core function has no output, the output will be None.
The post-hook template looks like this:
Hook Management
Registration and Execution
AgentScope provides the following APIs to manage the hooking functions:
Once registered, the hooks will be executed automatically at the corresponding execution points of the core functions.
Execution order:
- Hooks are executed in registration order.
- Multiple hooks can be chained together.
- For pre-hooks, non-None return values are passed to the next hook or core function, that is
- When a hook returns
None, the next hook will use the most recent non-None return value from previous hooks - If all previous hooks return
None, the next hook receives a copy of the original arguments - The final non-None return value (or original arguments if all hooks return
None) is passed to the core function
- When a hook returns
- The post-hooks works similarly as the pre-hooks.
Example
Tasking the pre-reply hook as an example, we first create a hook function that modifies the input messages before replying:instance_pre_reply_hook will be executed before the reply, and
the message content will be modified accordingly.