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

# 根因分析

> 读取或生成事件的 AI 根因分析。

```http theme={null}
GET /api/v1/alerts/incidents/{id}/rca
POST /api/v1/alerts/incidents/{id}/rca
```

[Mole Agent](https://docs.molesignal.com/zh-Hans/agent) 根据事件上下文为事件撰写根因分析（RCA）。每个事件最多有一份分析。

`alert` 角色会自动创建分析。组织配置了已启用且带有 API Key 的模型服务商时，该角色每三分钟为尚无分析的活动事件撰写分析，每轮最多 20 个。`GET` 调用读取已保存的分析。`POST` 调用立即生成新的分析，并替换已保存的分析。

| 调用 | 权限 | 说明 |
| - | - | - |
| `GET` | `alerts.read` | 分析生成之前返回 `404`，消息为 `no root-cause analysis for this incident yet`。 |
| `POST` | `alerts.manage` | 等待模型并返回新的分析。可选的查询参数 `locale` 选择输出语言，`en-us` 或 `zh-cn`，默认使用调用用户的语言偏好。 |

```bash cURL theme={null}
curl -X POST "http://localhost:5080/api/v1/alerts/incidents/$INCIDENT_ID/rca?locale=zh-cn" \
  -H "authorization: Bearer $MS_JWT"
```

```json 200 theme={null}
{
  "incident_id": "3KEDNEyqouYJeBDTLTMTBui7XHW",
  "org_id": "<organization-id>",
  "summary": "# 根因分析 — High error rate · checkout\n\n## 摘要\n\n...",
  "provider": "openai_compatible",
  "model": "<model-name>",
  "prompt_builtin_key": "analysis.root_cause",
  "prompt_hash": "cc4b0b3bccf9e24bb4590ca854cf524cfd07af6ae902797ad2dc108b6726e62c",
  "prompt_tokens": 412,
  "completion_tokens": 506,
  "finish_reason": "stop",
  "created_at": 1790000120000000,
  "updated_at": 1790000120000000
}
```

**响应**

| 字段 | 说明 |
| - | - |
| `incident_id`、`org_id` | 事件与组织的标识符。 |
| `summary` | Markdown 文本形式的分析内容。 |
| `provider`、`model` | 模型服务商类型，如 `openai`、`anthropic` 或 `openai_compatible`，以及模型名称。 |
| `prompt_builtin_key`、`prompt_hash` | 提示词模板，以及渲染后提示词的哈希，用于审计。 |
| `prompt_tokens`、`completion_tokens`、`finish_reason` | Token 用量，以及服务商报告的结束原因。 |
| `created_at`、`updated_at` | 自 Unix 纪元起的微秒数。重新生成分析时，`updated_at` 会变化。 |

**错误**

| 状态码 | 原因 |
| - | - |
| `400` | `POST` 调用没有找到该组织已启用且带有 API Key 的模型服务商。 |
| `403` | Token 缺少该调用所需的权限。 |
| `404` | 事件不存在，或 `GET` 调用尚未找到分析。 |


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