strands.hooks.events
¶
Hook events emitted as part of invoking Agents.
This module defines the events that are emitted as Agents run through the lifecycle of a request.
Messages = list[Message]
module-attribute
¶
A list of messages representing a conversation.
StopReason = Literal['content_filtered', 'end_turn', 'guardrail_intervened', 'interrupt', 'max_tokens', 'stop_sequence', 'tool_use']
module-attribute
¶
Reason for the model ending its response generation.
- "content_filtered": Content was filtered due to policy violation
- "end_turn": Normal completion of the response
- "guardrail_intervened": Guardrail system intervened
- "interrupt": Agent was interrupted for human input
- "max_tokens": Maximum token limit reached
- "stop_sequence": Stop sequence encountered
- "tool_use": Model requested to use a tool
AfterInvocationEvent
dataclass
¶
Bases: HookEvent
Event triggered at the end of an agent request.
This event is fired after the agent has completed processing a request, regardless of whether it completed successfully or encountered an error. Hook providers can use this event for cleanup, logging, or state persistence.
Note: This event uses reverse callback ordering, meaning callbacks registered later will be invoked first during cleanup.
This event is triggered at the end of the following api calls
- Agent.call
- Agent.stream_async
- Agent.structured_output
Attributes:
| Name | Type | Description |
|---|---|---|
invocation_state |
dict[str, Any]
|
State and configuration passed through the agent invocation. This can include shared context for multi-agent coordination, request tracking, and dynamic configuration. |
result |
AgentResult | None
|
The result of the agent invocation, if available. This will be None when invoked from structured_output methods, as those return typed output directly rather than AgentResult. |
Source code in strands/hooks/events.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
should_reverse_callbacks
property
¶
True to invoke callbacks in reverse order.
AfterModelCallEvent
dataclass
¶
Bases: HookEvent
Event triggered after the model invocation completes.
This event is fired after the agent has finished calling the model, regardless of whether the invocation was successful or resulted in an error. Hook providers can use this event for cleanup, logging, or post-processing.
Note: This event uses reverse callback ordering, meaning callbacks registered later will be invoked first during cleanup.
Note: This event is not fired for invocations to structured_output.
Attributes:
| Name | Type | Description |
|---|---|---|
invocation_state |
dict[str, Any]
|
State and configuration passed through the agent invocation. This can include shared context for multi-agent coordination, request tracking, and dynamic configuration. |
stop_response |
ModelStopResponse | None
|
The model response data if invocation was successful, None if failed. |
exception |
Exception | None
|
Exception if the model invocation failed, None if successful. |
retry |
bool
|
Whether to retry the model invocation. Can be set by hook callbacks to trigger a retry. When True, the current response is discarded and the model is called again. Defaults to False. |
Source code in strands/hooks/events.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
should_reverse_callbacks
property
¶
True to invoke callbacks in reverse order.
ModelStopResponse
dataclass
¶
Model response data from successful invocation.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_reason |
StopReason
|
The reason the model stopped generating. |
message |
Message
|
The generated message from the model. |
Source code in strands/hooks/events.py
265 266 267 268 269 270 271 272 273 274 275 | |
AfterMultiAgentInvocationEvent
dataclass
¶
Bases: BaseHookEvent
Event triggered after orchestrator execution completes.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
MultiAgentBase
|
The multi-agent orchestrator instance |
invocation_state |
dict[str, Any] | None
|
Configuration that user passes in |
Source code in strands/hooks/events.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
should_reverse_callbacks
property
¶
True to invoke callbacks in reverse order.
AfterNodeCallEvent
dataclass
¶
Bases: BaseHookEvent
Event triggered after individual node execution completes.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
MultiAgentBase
|
The multi-agent orchestrator instance |
node_id |
str
|
ID of the node that just completed execution |
invocation_state |
dict[str, Any] | None
|
Configuration that user passes in |
Source code in strands/hooks/events.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
should_reverse_callbacks
property
¶
True to invoke callbacks in reverse order.
AfterToolCallEvent
dataclass
¶
Bases: HookEvent
Event triggered after a tool invocation completes.
This event is fired after the agent has finished executing a tool, regardless of whether the execution was successful or resulted in an error. Hook providers can use this event for cleanup, logging, or post-processing.
Note: This event uses reverse callback ordering, meaning callbacks registered later will be invoked first during cleanup.
Attributes:
| Name | Type | Description |
|---|---|---|
selected_tool |
AgentTool | None
|
The tool that was invoked. It may be None if tool lookup failed. |
tool_use |
ToolUse
|
The tool parameters that were passed to the tool invoked. |
invocation_state |
dict[str, Any]
|
Keyword arguments that were passed to the tool |
result |
ToolResult
|
The result of the tool invocation. Either a ToolResult on success or an Exception if the tool execution failed. |
cancel_message |
str | None
|
The cancellation message if the user cancelled the tool call. |
retry |
bool
|
Whether to retry the tool invocation. Can be set by hook callbacks to trigger a retry. When True, the current result is discarded and the tool is called again. Defaults to False. |
Source code in strands/hooks/events.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
should_reverse_callbacks
property
¶
True to invoke callbacks in reverse order.
AgentInitializedEvent
dataclass
¶
Bases: HookEvent
Event triggered when an agent has finished initialization.
This event is fired after the agent has been fully constructed and all built-in components have been initialized. Hook providers can use this event to perform setup tasks that require a fully initialized agent.
Source code in strands/hooks/events.py
25 26 27 28 29 30 31 32 33 34 | |
AgentResult
dataclass
¶
Represents the last result of invoking an agent with a prompt.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_reason |
StopReason
|
The reason why the agent's processing stopped. |
message |
Message
|
The last message generated by the agent. |
metrics |
EventLoopMetrics
|
Performance metrics collected during processing. |
state |
Any
|
Additional state information from the event loop. |
interrupts |
Sequence[Interrupt] | None
|
List of interrupts if raised by user. |
structured_output |
BaseModel | None
|
Parsed structured output when structured_output_model was specified. |
Source code in strands/agent/agent_result.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
__str__()
¶
Return a string representation of the agent result.
Priority order: 1. Interrupts (if present) → stringified list of interrupt dicts 2. Structured output (if present) → JSON string 3. Text content from message → concatenated text blocks
Returns:
| Type | Description |
|---|---|
str
|
String representation based on the priority order above. |
Source code in strands/agent/agent_result.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
from_dict(data)
classmethod
¶
Rehydrate an AgentResult from persisted JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary containing the serialized AgentResult data |
required |
Returns: AgentResult instance Raises: TypeError: If the data format is invalid@
Source code in strands/agent/agent_result.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
to_dict()
¶
Convert this AgentResult to JSON-serializable dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing serialized AgentResult data |
Source code in strands/agent/agent_result.py
89 90 91 92 93 94 95 96 97 98 99 | |
AgentTool
¶
Bases: ABC
Abstract base class for all SDK tools.
This class defines the interface that all tool implementations must follow. Each tool must provide its name, specification, and implement a stream method that executes the tool's functionality.
Source code in strands/types/tools.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
is_dynamic
property
¶
Whether the tool was dynamically loaded during runtime.
Dynamic tools may have different lifecycle management.
Returns:
| Type | Description |
|---|---|
bool
|
True if loaded dynamically, False otherwise. |
supports_hot_reload
property
¶
Whether the tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
False by default. |
tool_name
abstractmethod
property
¶
The unique name of the tool used for identification and invocation.
tool_spec
abstractmethod
property
¶
Tool specification that describes its functionality and parameters.
tool_type
abstractmethod
property
¶
The type of the tool implementation (e.g., 'python', 'javascript', 'lambda').
Used for categorization and appropriate handling.
__init__()
¶
Initialize the base agent tool with default dynamic state.
Source code in strands/types/tools.py
219 220 221 | |
get_display_properties()
¶
Get properties to display in UI representations of this tool.
Subclasses can extend this to include additional properties.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of property names and their string values. |
Source code in strands/types/tools.py
287 288 289 290 291 292 293 294 295 296 297 298 | |
mark_dynamic()
¶
Mark this tool as dynamically loaded.
Source code in strands/types/tools.py
283 284 285 | |
stream(tool_use, invocation_state, **kwargs)
abstractmethod
¶
Stream tool events and return the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request containing tool ID and parameters. |
required |
invocation_state
|
dict[str, Any]
|
Caller-provided kwargs that were passed to the agent when it was invoked (agent(), agent.invoke_async(), etc.). |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/types/tools.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
BaseHookEvent
dataclass
¶
Base class for all hook events.
Source code in strands/hooks/registry.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
should_reverse_callbacks
property
¶
Determine if callbacks for this event should be invoked in reverse order.
Returns:
| Type | Description |
|---|---|
bool
|
False by default. Override to return True for events that should |
bool
|
invoke callbacks in reverse order (e.g., cleanup/teardown events). |
__post_init__()
¶
Disallow writes to non-approved properties.
Source code in strands/hooks/registry.py
49 50 51 52 53 | |
__setattr__(name, value)
¶
Prevent setting attributes on hook events.
Raises:
| Type | Description |
|---|---|
AttributeError
|
Always raised to prevent setting attributes on hook events. |
Source code in strands/hooks/registry.py
55 56 57 58 59 60 61 62 63 64 65 66 67 | |
BeforeInvocationEvent
dataclass
¶
Bases: HookEvent
Event triggered at the beginning of a new agent request.
This event is fired before the agent begins processing a new user request, before any model inference or tool execution occurs. Hook providers can use this event to perform request-level setup, logging, or validation.
This event is triggered at the beginning of the following api calls
- Agent.call
- Agent.stream_async
- Agent.structured_output
Attributes:
| Name | Type | Description |
|---|---|---|
invocation_state |
dict[str, Any]
|
State and configuration passed through the agent invocation. This can include shared context for multi-agent coordination, request tracking, and dynamic configuration. |
messages |
Messages | None
|
The input messages for this invocation. Can be modified by hooks to redact or transform content before processing. |
Source code in strands/hooks/events.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
BeforeModelCallEvent
dataclass
¶
Bases: HookEvent
Event triggered before the model is invoked.
This event is fired just before the agent calls the model for inference, allowing hook providers to inspect or modify the messages and configuration that will be sent to the model.
Note: This event is not fired for invocations to structured_output.
Attributes:
| Name | Type | Description |
|---|---|---|
invocation_state |
dict[str, Any]
|
State and configuration passed through the agent invocation. This can include shared context for multi-agent coordination, request tracking, and dynamic configuration. |
Source code in strands/hooks/events.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
BeforeMultiAgentInvocationEvent
dataclass
¶
Bases: BaseHookEvent
Event triggered before orchestrator execution starts.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
MultiAgentBase
|
The multi-agent orchestrator instance |
invocation_state |
dict[str, Any] | None
|
Configuration that user passes in |
Source code in strands/hooks/events.py
361 362 363 364 365 366 367 368 369 370 371 | |
BeforeNodeCallEvent
dataclass
¶
Bases: BaseHookEvent, _Interruptible
Event triggered before individual node execution starts.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
MultiAgentBase
|
The multi-agent orchestrator instance |
node_id |
str
|
ID of the node about to execute |
invocation_state |
dict[str, Any] | None
|
Configuration that user passes in |
cancel_node |
bool | str
|
A user defined message that when set, will cancel the node execution with status FAILED.
The message will be emitted under a MultiAgentNodeCancel event. If set to |
Source code in strands/hooks/events.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
BeforeToolCallEvent
dataclass
¶
Bases: HookEvent, _Interruptible
Event triggered before a tool is invoked.
This event is fired just before the agent executes a tool, allowing hook providers to inspect, modify, or replace the tool that will be executed. The selected_tool can be modified by hook callbacks to change which tool gets executed.
Attributes:
| Name | Type | Description |
|---|---|---|
selected_tool |
AgentTool | None
|
The tool that will be invoked. Can be modified by hooks to change which tool gets executed. This may be None if tool lookup failed. |
tool_use |
ToolUse
|
The tool parameters that will be passed to selected_tool. |
invocation_state |
dict[str, Any]
|
Keyword arguments that will be passed to the tool. |
cancel_tool |
bool | str
|
A user defined message that when set, will cancel the tool call.
The message will be placed into a tool result with an error status. If set to |
Source code in strands/hooks/events.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
HookEvent
dataclass
¶
Bases: BaseHookEvent
Base class for single agent hook events.
Attributes:
| Name | Type | Description |
|---|---|---|
agent |
Agent
|
The agent instance that triggered this event. |
Source code in strands/hooks/registry.py
70 71 72 73 74 75 76 77 78 | |
Message
¶
Bases: TypedDict
A message in a conversation with the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
list[ContentBlock]
|
The message content. |
role |
Role
|
The role of the message sender. |
Source code in strands/types/content.py
178 179 180 181 182 183 184 185 186 187 | |
MessageAddedEvent
dataclass
¶
Bases: HookEvent
Event triggered when a message is added to the agent's conversation.
This event is fired whenever the agent adds a new message to its internal message history, including user messages, assistant responses, and tool results. Hook providers can use this event for logging, monitoring, or implementing custom message processing logic.
Note: This event is only triggered for messages added by the framework itself, not for messages manually added by tools or external code.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Message
|
The message that was added to the conversation history. |
Source code in strands/hooks/events.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
MultiAgentBase
¶
Bases: ABC
Base class for multi-agent helpers.
This class integrates with existing Strands Agent instances and provides multi-agent orchestration capabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique MultiAgent id for session management,etc. |
Source code in strands/multiagent/base.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
__call__(task, invocation_state=None, **kwargs)
¶
Invoke synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Source code in strands/multiagent/base.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
deserialize_state(payload)
¶
Restore orchestrator state from a session dict.
Source code in strands/multiagent/base.py
253 254 255 | |
invoke_async(task, invocation_state=None, **kwargs)
abstractmethod
async
¶
Invoke asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Source code in strands/multiagent/base.py
190 191 192 193 194 195 196 197 198 199 200 201 202 | |
serialize_state()
¶
Return a JSON-serializable snapshot of the orchestrator state.
Source code in strands/multiagent/base.py
249 250 251 | |
stream_async(task, invocation_state=None, **kwargs)
async
¶
Stream events during multi-agent execution.
Default implementation executes invoke_async and yields the result as a single event. Subclasses can override this method to provide true streaming capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[dict[str, Any]]
|
Dictionary events containing multi-agent execution information including: |
AsyncIterator[dict[str, Any]]
|
|
AsyncIterator[dict[str, Any]]
|
|
AsyncIterator[dict[str, Any]]
|
|
Source code in strands/multiagent/base.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
MultiAgentInitializedEvent
dataclass
¶
Bases: BaseHookEvent
Event triggered when multi-agent orchestrator initialized.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
MultiAgentBase
|
The multi-agent orchestrator instance |
invocation_state |
dict[str, Any] | None
|
Configuration that user passes in |
Source code in strands/hooks/events.py
292 293 294 295 296 297 298 299 300 301 302 | |
ToolResult
¶
Bases: TypedDict
Result of a tool execution.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
list[ToolResultContent]
|
List of result content returned by the tool. |
status |
ToolResultStatus
|
The status of the tool execution ("success" or "error"). |
toolUseId |
str
|
The unique identifier of the tool use request that produced this result. |
Source code in strands/types/tools.py
88 89 90 91 92 93 94 95 96 97 98 99 | |
ToolUse
¶
Bases: TypedDict
A request from the model to use a specific tool with the provided input.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
Any
|
The input parameters for the tool. Can be any JSON-serializable type. |
name |
str
|
The name of the tool to invoke. |
toolUseId |
str
|
A unique identifier for this specific tool use request. |
Source code in strands/types/tools.py
53 54 55 56 57 58 59 60 61 62 63 64 65 | |
_Interruptible
¶
Bases: Protocol
Interface that adds interrupt support to hook events and tools.
Source code in strands/types/interrupt.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
interrupt(name, reason=None, response=None)
¶
Trigger the interrupt with a reason.
reason: User provided reason for the interrupt.
response: Preemptive response from user if available.
Returns:
| Type | Description |
|---|---|
Any
|
The response from a human user when resuming from an interrupt state. |
Raises:
| Type | Description |
|---|---|
InterruptException
|
If human input is required. |
RuntimeError
|
If agent instance attribute not set. |
Source code in strands/types/interrupt.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |