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, run_context, function_name, function_call, and arguments are available parameters.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.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
Example: Human in the loop using tool hooks
This example shows how to:- Add hooks to tools for user confirmation
- Handle user input during tool execution
- Gracefully cancel operations based on user choice
1
Create the example
hitl.py
2
Run the example
Install librariesExport your keyRun the example