Skip to content

feat: Add hooks for before/after tool calls + allow hooks to update values #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2025

Conversation

zastrowm
Copy link
Member

@zastrowm zastrowm commented Jul 3, 2025

Description

Add Tool Call Hooks with Value Modification Support

This PR adds two new hook events that fire before and after tool execution, allowing hooks to inspect and modify tool calls and their results. The hook system has been enhanced to support selective property updates on event objects.

New Hook Events

  • BeforeToolInvocation : Fires before a tool is executed, allowing modification of the selected tool and parameters
  • AfterToolInvocation: Fires after tool execution completes, allowing modification of the result

Enhanced Hook Registry

The base HookEvent class now includes a property write protection system. Event classes can override _can_write() to specify which properties hooks are allowed to modify. This prevents accidental modification of read-only properties while enabling controlled updates where needed.

Usage Examples

Tool Replacement

def replace_calculator(event: BeforeToolInvocation):
    if event.tool_use["name"] == "calculator":
        event.selected_tool = my_custom_calculator

registry.add_callback(BeforeToolInvocation, replace_calculator)

Result Modification

Allow intercepting tools and preventing their usage:

class ToolLimiter(HookProvider):
    def register_hooks(self, registry: "HookRegistry") -> None:
        registry.add_callback(BeforeToolInvocationEvent, self.before_tool_call)
        registry.add_callback(AfterToolInvocationEvent, self.after_tool_call)

    def before_tool_call(self, event: BeforeToolInvocationEvent):
        if event.tool_use.get("name") == "test_quota":
            event.selected_tool = None

    def after_tool_call(self, event: AfterToolInvocationEvent):
        if event.tool_use.get("name") == "test_quota":
            event.result = {
                "status": "error",
                "toolUseId": "test",
                "content": [{"text": "This tool has been used too many times!"}],
            }

...
agent._hooks.add_hook(ToolLimiter())

Tool Monitoring

class ToolLogger(HookProvider):
    def register_hooks(self, registry):
        registry.add_callback(BeforeToolInvocationEvent, self.log_start)
        registry.add_callback(AfterToolInvocationEvent, self.log_end)
    
    def log_start(self, event):
        logger.info(f"Starting tool: {event.tool_use['name']}")
    
    def log_end(self, event):
        logger.info(f"Tool finished: {event.result['status']}")

Related Issues

#231

Documentation PR

N/A for now. Will soon begin PR to document hooks experimentally.

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@zastrowm zastrowm force-pushed the hooks_for_tool_calls branch from c703b7a to 64fc331 Compare July 7, 2025 17:33
@zastrowm zastrowm marked this pull request as ready for review July 7, 2025 18:36
zastrowm added 2 commits July 7, 2025 17:34
…alues

Add the ability to intercept/modify tool calls by implementing support for BeforeToolInvocationEvent & AfterToolInvocationEvent hooks
@zastrowm zastrowm force-pushed the hooks_for_tool_calls branch from ec758cd to dd3478b Compare July 7, 2025 21:40
@zastrowm zastrowm requested a review from pgrayy July 8, 2025 15:54
@zastrowm zastrowm merged commit c05e037 into strands-agents:main Jul 8, 2025
22 checks passed
jsamuel1 pushed a commit to jsamuel1/sdk-python that referenced this pull request Jul 9, 2025
…alues (strands-agents#352)

Add the ability to intercept/modify tool calls by implementing support for BeforeToolInvocationEvent & AfterToolInvocationEvent hooks
malte-aws pushed a commit to malte-aws/sdk-python that referenced this pull request Jul 10, 2025
…alues (strands-agents#352)

Add the ability to intercept/modify tool calls by implementing support for BeforeToolInvocationEvent & AfterToolInvocationEvent hooks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants