strands.experimental.bidi.agent.loop
Agent loop.
The agent loop handles the events received from the model and executes tools when given a tool use request.
_BidiAgentLoop
Section titled “_BidiAgentLoop”class _BidiAgentLoop()Defined in: src/strands/experimental/bidi/agent/loop.py:40
Agent loop.
Attributes:
_agent- BidiAgent instance to loop._started- Flag if agent loop has started._task_pool- Track active async tasks created in loop._event_queue- Queue model and tool call events for receiver._invocation_state- Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter._send_gate- Gate the sending of events to the model. Blocks when agent is reseting the model connection after timeout.
__init__
Section titled “__init__”def __init__(agent: "BidiAgent") -> NoneDefined in: src/strands/experimental/bidi/agent/loop.py:55
Initialize members of the agent loop.
Note, before receiving events from the loop, the user must call start.
Arguments:
agent- Bidirectional agent to loop over.
async def start(invocation_state: dict[str, Any] | None = None) -> NoneDefined in: src/strands/experimental/bidi/agent/loop.py:71
Start the agent loop.
The agent model is started as part of this call.
Arguments:
invocation_state- Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter.
Raises:
RuntimeError- If loop already started.
async def stop() -> NoneDefined in: src/strands/experimental/bidi/agent/loop.py:105
Stop the agent loop.
async def send(event: BidiInputEvent | ToolResultEvent) -> NoneDefined in: src/strands/experimental/bidi/agent/loop.py:124
Send model event.
Additionally, add text input to messages array.
Arguments:
event- User input event or tool result.
Raises:
RuntimeError- If start has not been called.
receive
Section titled “receive”async def receive() -> AsyncGenerator[BidiOutputEvent, None]Defined in: src/strands/experimental/bidi/agent/loop.py:148
Receive model and tool call events.
Returns:
Model and tool call events.
Raises:
RuntimeError- If start has not been called.