Skip to main content
The 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 reply or reply_stream by calling task.cancel() on its task. This raises asyncio.CancelledError inside the agent, which unwinds the current reasoning-acting step cleanly.
  • Agent is paused: pass a UserInterruptEvent to reply_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 a ToolResultBlock marked as interrupted by the user and yields the corresponding ToolResultStartEvent, ToolResultTextDeltaEvent, ToolResultEndEvent, and finally a ReplyEndEvent, leaving the agent ready to accept new input. If the agent is not in a paused state, the event is a no-op and reply / reply_stream returns 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.