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
MAX_DELAY
Section titled “MAX_DELAY”4 minutes
event_loop_cycle
Section titled “event_loop_cycle”async def event_loop_cycle( agent: "Agent", invocation_state: dict[str, Any], structured_output_context: StructuredOutputContext | None = None) -> AsyncGenerator[TypedEvent, None]Defined in: src/strands/event_loop/event_loop.py:78
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
Arguments:
-
agent- The agent for which the cycle is being executed. -
invocation_state- Additional arguments including:- request_state: State maintained across cycles
- event_loop_cycle_id: Unique ID for this cycle
- event_loop_cycle_span: Current tracing Span for this cycle
-
structured_output_context- Optional context for structured output management.
Yields:
Model and tool stream events. The last event is a tuple containing:
- StopReason: Reason the model stopped generating (e.g., “tool_use”)
- Message: The generated message from the model
- EventLoopMetrics: Updated metrics for the event loop
- Any: Updated request state
Raises:
EventLoopException- If an error occurs during executionContextWindowOverflowException- If the input is too large for the model
recurse_event_loop
Section titled “recurse_event_loop”async def recurse_event_loop( agent: "Agent", invocation_state: dict[str, Any], structured_output_context: StructuredOutputContext | None = None) -> AsyncGenerator[TypedEvent, None]Defined in: src/strands/event_loop/event_loop.py:236
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.
Arguments:
agent- Agent for which the recursive call is being made.invocation_state- Arguments to pass through event_loop_cyclestructured_output_context- Optional context for structured output management.
Yields:
Results from event_loop_cycle where the last result contains:
- StopReason: Reason the model stopped generating
- Message: The generated message from the model
- EventLoopMetrics: Updated metrics for the event loop
- Any: Updated request state