Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ These documents define the standards and processes that ensure consistency and q
│ │ │ ├── multi-agent-patterns.md
│ │ │ ├── swarm.md
│ │ │ └── workflow.md
│ │ ├── plugins
│ │ │ └── index.md
│ │ ├── streaming
│ │ │ ├── async-iterators.md
│ │ │ ├── callback-handlers.md
Expand Down
264 changes: 264 additions & 0 deletions build_output.log

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/community/get-featured.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Place your documentation in the right spot:
| Model Providers | `community/model-providers/` | `cohere.md` |
| Tools | `community/tools/` | `strands-deepgram.md` |
| Session Managers | `community/session-managers/` | `agentcore-memory.md` |
| Plugins | `community/plugins/` | `my-plugin.md` |
| Integrations | `community/integrations/` | `ag-ui.md` |

## Document Layout
Expand Down
38 changes: 34 additions & 4 deletions docs/user-guide/concepts/agents/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

Hooks are a composable extensibility mechanism for extending agent functionality by subscribing to events throughout the agent lifecycle. The hook system enables both built-in components and user code to react to or modify agent behavior through strongly-typed event callbacks.

!!! tip "Consider Plugins for High-Level Behavior Changes"
For packaging reusable agent extensions or implementing complex behavior changes, consider using [Plugins](../plugins/index.md) instead of raw hooks. Plugins provide a higher-level abstraction that encapsulates related hooks, configuration, and initialization logic into shareable packages.

## Overview

The hooks system is a composable, type-safe system that supports multiple subscribers per event type.
The hooks system is a composable, type-safe system that supports multiple subscribers per event type.

A **Hook Event** is a specific event in the lifecycle that callbacks can be associated with. A **Hook Callback** is a callback function that is invoked when the hook event is emitted.

Expand All @@ -23,18 +26,27 @@ Hook callbacks are registered against specific event types and receive strongly-

### Registering Individual Hook Callbacks

You can register callbacks for specific events using `agent.hooks` after the fact:
The simplest way to register a hook callback is using the `agent.add_hook()` method:

=== "Python"

```python
from strands import Agent
from strands.hooks import BeforeInvocationEvent, BeforeToolCallEvent

agent = Agent()

# Register individual callbacks
# Register individual callbacks using the simplified API
def my_callback(event: BeforeInvocationEvent) -> None:
print("Custom callback triggered")

agent.hooks.add_callback(BeforeInvocationEvent, my_callback)
agent.add_hook(my_callback, BeforeInvocationEvent)

# Type inference: If your callback has a type hint, the event type is inferred
def typed_callback(event: BeforeToolCallEvent) -> None:
print(f"Tool called: {event.tool_use['name']}")

agent.add_hook(typed_callback) # Event type inferred from type hint
```

=== "TypeScript"
Expand All @@ -43,6 +55,21 @@ You can register callbacks for specific events using `agent.hooks` after the fac
--8<-- "user-guide/concepts/agents/hooks.ts:individual_callback"
```

You can also use the `agent.hooks.add_callback()` method for explicit event type specification:

=== "Python"

```python
agent = Agent()

def my_callback(event: BeforeInvocationEvent) -> None:
print("Custom callback triggered")

agent.hooks.add_callback(BeforeInvocationEvent, my_callback)
```

{{ ts_not_supported_code("This syntax is not yet available in TypeScript SDK") }}

For multi-agent orchestrators, you can register callbacks for orchestration events:

=== "Python"
Expand Down Expand Up @@ -91,6 +118,9 @@ The `HookProvider` protocol allows a single object to register callbacks for mul
--8<-- "user-guide/concepts/agents/hooks.ts:hook_provider_class"
```

!!! note "HookProvider vs Plugin"
For simple hook registration, `HookProvider` works well. However, if you need initialization logic, configuration, or plan to share your extension with others, consider creating a [Plugin](../plugins/index.md) instead. Plugins extend the `HookProvider` pattern with additional capabilities like named identification and agent-aware initialization.

## Hook Event Lifecycle

### Single-Agent Lifecycle
Expand Down
3 changes: 3 additions & 0 deletions docs/user-guide/concepts/agents/retry-strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,7 @@ Built in retry constructs like `ModelRetryStrategy` are useful for customizing m

Unlike `ModelRetryStrategy`, hooks don't automatically introduce delays between retries. The example above uses `asyncio.sleep` to add a 2-second delay before each retry.

!!! tip "Package as a Plugin"
If you want to share custom retry logic with others or reuse it across projects, consider packaging it as a [Plugin](../plugins/index.md). Plugins provide a clean interface for configuration and make your extensions easier to distribute.

See [Hooks](hooks.md#model-call-retry) for more examples.
Loading