feat(poc) add hook system for tools that require approval (eg. hitl) #2591
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a hook system that lets you intercept tool calls for eg human approval before execution.
It's merely a proof of concept for myself with no intention to use it (yet), but maybe it is of interest here too?
I think I'm not keen on manually traversing agent graphs, so I tried to see if we can have a public
agent.on.xx
interface that works directly with observing the nodes.The current implementation is a registry-based approach that handles hooks without requiring graph iteration.
I also explored using traitlets for reactive model/instruction changes (e.g., dynamically switching models or instructions based on user context). I think traitlets integration for dynamic agent configuration/observing still remains interesting for future exploration.
But to start somehwere, first this for before/after tool calling.
Example
Current options for the
agent.on
APIagent.on.tool(my_tool).before = func
- Run before tool executionagent.on.tool(my_tool).after = func
- Run after tool executionagent.on.tool(my_tool).error = func
- Run on tool errorsModelRetry
to deny execution and suggest alternativesNone
to allow executioncontext['tool_call'].args_as_dict()
How it works
When you set
agent.on.tool(my_tool).before = func
, the system:ctx.deps.hook_registry
Testing
See
test_reactivenodes.ipynb
for a working example with simple human-in-the-loop holiday planning approval notebook.