sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] API to define execution priority for hooks

Open nagabharann opened this issue 10 months ago • 3 comments

Problem Statement

There is no ability to define execution order for hooks. When multiple hooks are registered for the same event, their execution order is undefined. This is problematic for security-related hooks that must run first or last in the execution chain.

Proposed Solution

Provide an API to define hook priority, such as:

  1. A priority parameter when registering hooks (e.g., @hook(priority=100))
  2. Named priority levels (e.g., Priority.FIRST, Priority.NORMAL, Priority.LAST)
  3. A hook ordering configuration at the agent level

Use Case

We are building security hooks that must:

  1. Run before all other hooks to validate inputs
  2. Run after all other hooks to sanitize outputs
  3. Have guaranteed execution order regardless of registration order

Example: An authorization hook must validate permissions before any business logic hooks execute.

Alternatives Solutions

We currently try to control registration order, but this is fragile when hooks come from multiple sources (plugins, user code, etc.).

Additional Context

No response

nagabharann avatar Jan 29 '26 20:01 nagabharann

Hooks are guaranteed to execute in definition/insertion order. So if you have the following let's say:

from strands import Agent
from strands.plugins import Plugin, hook
from strands.hooks import BeforeToolCallEvent

class MyPlugin(Plugin):
    name = "my-plugin"

    @hook
    def op1(self, event: BeforeToolCallEvent) -> None:
        print("op1")

    @hook
    def op2(self, event: BeforeToolCallEvent) -> None:
        print("op2")

agent = Agent(plugins=[MyPlugin()])

op1 will run before op2. We do need to document this under https://strandsagents.com/docs/user-guide/concepts/agents/hooks/.

pgrayy avatar Mar 17 '26 15:03 pgrayy

Hook order in TS: https://github.com/strands-agents/sdk-typescript/pull/1005

zastrowm avatar May 12 '26 18:05 zastrowm

Closing as resolved by PR #2559 — hook ordering is now supported via an order parameter plus HookOrder levels (SDK_FIRST/DEFAULT/SDK_LAST), enforced with bisect.insort and documented on the hooks page.

Reference: https://github.com/strands-agents/harness-sdk/pull/2559

Comment to reopen if needed.

yonib05 avatar Jun 19 '26 16:06 yonib05