Hooks vs. Middleware: What's the main difference?

Hi folks,

I’ve been looking at the execution diagrams for Hooks and Middleware, and they appear to have very similar flows. Both allow for intercepting the process before and after the model call.

Could someone explain the key difference? I’m trying to understand when it’s more appropriate to use one over the other. Is there a difference in terms of control or intended use case?

Thanks!

@nicobytes thanks for the feedback we’ll try to clarify in the docs!

Middleware is a generalization of the pre-model hooks.

The main differences are:

  • Middleware defines additional ways to hook into the execution cycle (e.g., modify_request allows modifying the entire model request; e.g., change the prompt, the tools, and even the model itself)
  • You can use multiple Middleware implementations at once (e.g., use built-in summarization and human in the loop) – this was not possible with the pre-model hook.
  • Middleware can register additional tools or extend the state (making it easier to ship pre-built middleware)
  • Middleware will be extended with additional life cycle hooks down the road (e.g., before_agent, after_agent)

Hi @nicobytes

Hi @pawel-twardziak , hooks and callbacks refer to two different things in this context. The hooks in discussion are pre_model_hook and post_model_hook in the prebuilt create react agent abstraction. (They also conceptually don’t work like callbacks, but like interceptors or hooks.)

1 Like

Hi @eyurtsev ,

Thanks for the clarification, that makes more sense.
And that means my article can be confusing (hooks !== callbacks), right? I can correct it if necessary by removing “hooks” word since what I am describing in the article are not hooks but pure callbacks for specific events, right?

That’s right! The article is fine in terms of discussing callbacks vs. middleware. However, @nicobytes asked a different question than what the article is discussing.

1 Like

Great! Thanks for that @eyurtsev :+1: