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

# 取消 Session

> 请求取消当前 Session 工作。

`POST /api/v1/cloud/sessions/{session_id}/cancel`

请求取消 Session 当前轮（turn）的执行。对运行中的 Session 执行 Cancel 返回 **HTTP 202 Accepted**，Session 状态从 `canceling` 自动回到 `idle`；对空闲 Session 执行 Cancel 是幂等的 no-op，返回 **HTTP 200 OK**。

## 路径参数

| 参数           | 类型     | 说明                        |
| ------------ | ------ | ------------------------- |
| `session_id` | string | 以 `sess_` 为前缀的 Session ID |

## 请求头

| 头部              | 必选 | 说明                  |
| --------------- | -- | ------------------- |
| `Authorization` | 是  | `Bearer $QODER_PAT` |

## 示例请求

```bash theme={null}
curl -X POST https://api.qoder.com/api/v1/cloud/sessions/sess_019ef30a4f0275678965d496ab542ef0/cancel \
  -H "Authorization: Bearer $QODER_PAT"
```

## 示例响应

**HTTP 200 OK** (no-op) or **HTTP 202 Accepted** (cancellation applied)

```json theme={null}
{
  "id": "sess_019ef30a4f0275678965d496ab542ef0",
  "type": "session",
  "status": "canceling"
}
```

`status` 字段在两种情况下均为 `"canceling"`。当前 turn 中止后，Session 将恢复为 `idle`。

## 错误码

| HTTP | 类型                     | 触发条件            |
| ---- | ---------------------- | --------------- |
| 401  | `authentication_error` | PAT 无效或过期       |
| 404  | `not_found_error`      | Session 不存在或已归档 |

> **备注：** 对已处于 idle 状态的 Session 调用 cancel 是安全的空操作（no-op），返回 HTTP 200。无论是否实际触发了取消（202 表示已触发，200 表示空操作），响应均使用相同的轻量格式。

**HTTP 404 Not Found**

```json theme={null}
{
  "type": "error",
  "request_id": "cb80235f-76a2-4ff3-9e28-5aa2da12dc14",
  "error": {
    "type": "not_found_error",
    "message": "Session 'sess_does_not_exist_0000000000000000000000000000' was not found."
  }
}
```

完整错误信封格式见 [错误参考](/zh/cloud-agents/api/conventions/errors)。

## 相关

<CardGroup cols={2}>
  <Card title="启动 Session" icon="play" href="/zh/cloud-agents/sessions">
    让 Agent 在环境中以有状态对话的方式运行。
  </Card>
</CardGroup>
