> ## 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.

> ## Agent Instructions
> For AgentScope Python, use https://docs.agentscope.io/stable/en/index for new projects. For existing projects, check the installed agentscope version and use matching versioned documentation.
> The /latest/ alias points to the newest documentation and may move ahead of /stable/ when a development version exists. Do not mix AgentScope 1.x and 2.x APIs.
> State the AgentScope version when providing installation commands or code examples. ReMe uses its own continuously updated /reme/latest/ documentation.

# 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.

```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)
```

> [!NOTE]
>
> Use `agent.state.reply_id` to reference the reply that is currently paused. See [Human-in-the-Loop](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/agent/human-in-the-loop.md) for how the agent enters a paused state.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.