Tool Hooks
You can use tool hooks to perform validation, logging, or any other logic before or after a tool is called. A tool hook is a function that takes a function name, function call, and arguments. Optionally, you can access theAgent or Team object as well. Inside the tool hook, you have to call the function call and return the result.
It is important to use exact parameter names when defining a tool hook.
agent, team, function_name, function_call, and arguments are available parameters.Agent or Team object in the tool hook.
Multiple Tool Hooks
You can also assign multiple tool hooks at once. They will be applied in the order they are assigned.Pre and Post Hooks
Pre and post hooks let’s you modify what happens before and after a tool is called. It is an alternative to tool hooks. Set thepre_hook in the @tool decorator to run a function before the tool call.
Set the post_hook in the @tool decorator to run a function after the tool call.
Here’s a demo example of using a pre_hook, post_hook along with Agent Context.
pre_and_post_hooks.py