strands.event_loop
¶
This package provides the core event loop implementation for the agents SDK.
The event loop enables conversational AI agents to process messages, execute tools, and handle errors in a controlled, iterative manner.
strands.event_loop.event_loop
¶
This module implements the central event loop.
The event loop allows agents to:
- Process conversation messages
- Execute tools based on model requests
- Handle errors and recovery strategies
- Manage recursive execution cycles
event_loop_cycle(agent, invocation_state)
async
¶
Execute a single cycle of the event loop.
This core function processes a single conversation turn, handling model inference, tool execution, and error recovery. It manages the entire lifecycle of a conversation turn, including:
- Initializing cycle state and metrics
- Checking execution limits
- Processing messages with the model
- Handling tool execution requests
- Managing recursive calls for multi-turn tool interactions
- Collecting and reporting metrics
- Error handling and recovery
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent
|
The agent for which the cycle is being executed. |
required |
invocation_state
|
dict[str, Any]
|
Additional arguments including:
|
required |
Yields:
Type | Description |
---|---|
AsyncGenerator[dict[str, Any], None]
|
Model and tool stream events. The last event is a tuple containing:
|
Raises:
Type | Description |
---|---|
EventLoopException
|
If an error occurs during execution |
ContextWindowOverflowException
|
If the input is too large for the model |
Source code in strands/event_loop/event_loop.py
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 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 138 139 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 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 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 |
|
recurse_event_loop(agent, invocation_state)
async
¶
Make a recursive call to event_loop_cycle with the current state.
This function is used when the event loop needs to continue processing after tool execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent
|
Agent for which the recursive call is being made. |
required |
invocation_state
|
dict[str, Any]
|
Arguments to pass through event_loop_cycle |
required |
Yields:
Type | Description |
---|---|
AsyncGenerator[dict[str, Any], None]
|
Results from event_loop_cycle where the last result contains:
|
Source code in strands/event_loop/event_loop.py
run_tool(agent, tool_use, invocation_state)
async
¶
Process a tool invocation.
Looks up the tool in the registry and streams it with the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent
|
The agent for which the tool is being executed. |
required |
tool_use
|
ToolUse
|
The tool object to process, containing name and parameters. |
required |
invocation_state
|
dict[str, Any]
|
Context for the tool invocation, including agent state. |
required |
Yields:
Type | Description |
---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/event_loop/event_loop.py
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 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
|
strands.event_loop.streaming
¶
Utilities for handling streaming responses from language models.
extract_usage_metrics(event)
¶
Extracts usage metrics from the metadata chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
MetadataEvent
|
metadata. |
required |
Returns:
Type | Description |
---|---|
tuple[Usage, Metrics]
|
The extracted usage metrics and latency. |
Source code in strands/event_loop/streaming.py
handle_content_block_delta(event, state)
¶
Handles content block delta updates by appending text, tool input, or reasoning content to the state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
ContentBlockDeltaEvent
|
Delta event. |
required |
state
|
dict[str, Any]
|
The current state of message processing. |
required |
Returns:
Type | Description |
---|---|
tuple[dict[str, Any], dict[str, Any]]
|
Updated state with appended text or tool input. |
Source code in strands/event_loop/streaming.py
handle_content_block_start(event)
¶
Handles the start of a content block by extracting tool usage information if any.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
ContentBlockStartEvent
|
Start event. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary with tool use id and name if tool use request, empty dictionary otherwise. |
Source code in strands/event_loop/streaming.py
handle_content_block_stop(state)
¶
Handles the end of a content block by finalizing tool usage, text content, or reasoning content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
dict[str, Any]
|
The current state of message processing. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Updated state with finalized content block. |
Source code in strands/event_loop/streaming.py
handle_message_start(event, message)
¶
Handles the start of a message by setting the role in the message dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
MessageStartEvent
|
A message start event. |
required |
message
|
Message
|
The message dictionary being constructed. |
required |
Returns:
Type | Description |
---|---|
Message
|
Updated message dictionary with the role set. |
Source code in strands/event_loop/streaming.py
handle_message_stop(event)
¶
Handles the end of a message by returning the stop reason.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
MessageStopEvent
|
Stop event. |
required |
Returns:
Type | Description |
---|---|
StopReason
|
The reason for stopping the stream. |
Source code in strands/event_loop/streaming.py
handle_redact_content(event, state)
¶
Handles redacting content from the input or output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
RedactContentEvent
|
Redact Content Event. |
required |
state
|
dict[str, Any]
|
The current state of message processing. |
required |
Source code in strands/event_loop/streaming.py
process_stream(chunks)
async
¶
Processes the response stream from the API, constructing the final message and extracting usage metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks
|
AsyncIterable[StreamEvent]
|
The chunks of the response stream from the model. |
required |
Yields:
Type | Description |
---|---|
AsyncGenerator[dict[str, Any], None]
|
The reason for stopping, the constructed message, and the usage metrics. |
Source code in strands/event_loop/streaming.py
remove_blank_messages_content_text(messages)
¶
Remove or replace blank text in message content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
Messages
|
Conversation messages to update. |
required |
Returns:
Type | Description |
---|---|
Messages
|
Updated messages. |
Source code in strands/event_loop/streaming.py
stream_messages(model, system_prompt, messages, tool_specs)
async
¶
Streams messages to the model and processes the response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
Model provider. |
required |
system_prompt
|
Optional[str]
|
The system prompt to send. |
required |
messages
|
Messages
|
List of messages to send. |
required |
tool_specs
|
list[ToolSpec]
|
The list of tool specs. |
required |
Yields:
Type | Description |
---|---|
AsyncGenerator[dict[str, Any], None]
|
The reason for stopping, the final message, and the usage metrics |