Skip to main content
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 the Agent 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, run_context, function_name, function_call, and arguments are available parameters.
For example:
or
You can assign tool hooks on agents and teams. The tool hooks will be applied to all tool calls made by the agent or team. For example:
You can also get access to the RunContext object in the tool hook. Inside the run context, you will find the session state, dependencies, and metadata.

Multiple Tool Hooks

You can also assign multiple tool hooks at once. They will be applied in the order they are assigned.
You can also assign tool hooks to specific custom tools.

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 the pre_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