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.
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 |
|---|---|---|
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
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 | |
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 |
|---|---|---|
stop_response |
Optional[ModelStopResponse]
|
The model response data if invocation was successful, None if failed. |
exception |
Optional[Exception]
|
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
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 | |
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
223 224 225 226 227 228 229 230 231 232 233 | |
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 |
Optional[AgentTool]
|
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. |
Source code in strands/hooks/events.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
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
22 23 24 25 26 27 28 29 30 31 | |
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
17 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 | |
__str__()
¶
Get the agent's last message as a string.
This method extracts and concatenates all text content from the final message, ignoring any non-text content like images or structured data. If there's no text content but structured output is present, it serializes the structured output instead.
Returns:
| Type | Description |
|---|---|
str
|
The agent's last message as a string. |
Source code in strands/agent/agent_result.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
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
78 79 80 81 82 83 84 85 86 87 88 | |
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
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 299 300 301 302 303 304 305 306 | |
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
227 228 229 | |
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
295 296 297 298 299 300 301 302 303 304 305 306 | |
mark_dynamic()
¶
Mark this tool as dynamically loaded.
Source code in strands/types/tools.py
291 292 293 | |
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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
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
Source code in strands/hooks/events.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
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.
Source code in strands/hooks/events.py
176 177 178 179 180 181 182 183 184 185 186 187 | |
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 |
Optional[AgentTool]
|
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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
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
69 70 71 72 73 74 75 76 77 | |
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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
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
87 88 89 90 91 92 93 94 95 96 97 98 | |
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
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
_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 | |