> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentscope.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Interrupt Agent

> Stop a running or paused agent cleanly and resume from a consistent state

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.

<CodeGroup>
  ```python Interrupt a running agent theme={null}
  import asyncio
  from agentscope.agent import Agent
  from agentscope.message import UserMsg

  async def chat(agent: Agent) -> None:
      async for event in agent.reply_stream(
          UserMsg(name="user", content="..."),
      ):
          ...

  async def main() -> None:
      agent = Agent(...)
      task = asyncio.create_task(chat(agent))

      # Cancel the task at any point to interrupt the agent
      await asyncio.sleep(1)
      task.cancel()

  asyncio.run(main())
  ```

  ```python Interrupt a paused agent theme={null}
  from agentscope.agent import Agent
  from agentscope.event import UserInterruptEvent

  async def main() -> None:
      agent = Agent(...)

      # The agent has previously paused on a RequireUserConfirmEvent
      # or a RequireExternalExecutionEvent. Send UserInterruptEvent to
      # clear the pending state and end the current reply.
      async for event in agent.reply_stream(
          UserInterruptEvent(reply_id=agent.state.reply_id),
      ):
          print(event)
  ```
</CodeGroup>

<Note>
  Use `agent.state.reply_id` to reference the reply that is currently paused. See [Human-in-the-Loop](/versions/2.0.5dev/en/building-blocks/agent/human-in-the-loop) for how the agent enters a paused state.
</Note>
