Skip to content

Add Model Invocation Hooks #387

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 1 commit into from
Jul 10, 2025
Merged

Conversation

zastrowm
Copy link
Member

@zastrowm zastrowm commented Jul 9, 2025

Description

This PR introduces new hook events that allow subscribers to monitor and interact with model invocations during agent execution. Each call to the model from the event loop will fire these events, even if we're going to retry the request. In the future I think we could expand the AfterModelInvocationEvent to reimplemenet retries and allow customers to specify how the request is retried, but that will require further design.

Changes:

  • Added BeforeModelInvocationEvent and AfterModelInvocationEvent to the experimental hooks system
  • The AfterModelInvocationEvent includes model response data and stop reason information
  • Initially I had tool specifications included on the events and modifable in the BeforeEvent. I decided against this after initial implementation to refine the API design
  • Note that structured_output calls are not included in these events - because it doesn't have a stop_data it didn't make sense to combine the event. I'm still considering how best to include structured_output calls
  • I also simplify the exception logic for the event_loop - condensing all exception handling into a single except block.

Feedback wanted:

  • Naming of the stop_data field and dataclass

Related Issues

#231

Documentation PR

Todo

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.

… the model for processing.

We also don't support structured output as the parameters are not applicable
@zastrowm zastrowm merged commit c412292 into strands-agents:main Jul 10, 2025
12 checks passed
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