Agent class implements interruption on top of asyncio.CancelledError, so you can stop execution at any stage of model inference or tool execution. Once interrupted, the agent’s context is left in a consistent state and the conversation can be resumed immediately with a new message.
There are two ways to interrupt an agent, depending on whether it is actively running:
- Agent is running: cancel the coroutine that is awaiting
replyorreply_streamby callingtask.cancel()on its task. This raisesasyncio.CancelledErrorinside the agent, which unwinds the current reasoning-acting step cleanly. - Agent is paused: pass a
UserInterruptEventtoreply_stream. If the agent is currently waiting for user confirmation or external tool execution, it discards the pending state and terminates the reply. For each pending tool call it synthesises aToolResultBlockmarked as interrupted by the user and yields the correspondingToolResultStartEvent,ToolResultTextDeltaEvent,ToolResultEndEvent, and finally aReplyEndEvent, leaving the agent ready to accept new input. If the agent is not in a paused state, the event is a no-op andreply/reply_streamreturns immediately.
Use
agent.state.reply_id to reference the reply that is currently paused. See Human-in-the-Loop for how the agent enters a paused state.