> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://developers.alephant.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://developers.alephant.io/_mcp/server.

# n8n 工作流治理

> 治理、追踪和衡量使用 AI 模型、工具和付费端点的 n8n 工作流

n8n 是强大的工作流编排层。Alephant 为这些工作流中的 AI 工作增加了治理和财务层。

当工作流需要的不只是模型响应时，请将 Alephant 与 n8n 一起使用。常见需求包括每次运行的成本、在高成本分支前的策略控制、请求级审计跟踪，以及将有用的工作流封装为付费端点的路径。

## 工作流模式

```text
n8n trigger
-> prepare workflow context
-> Alephant Cost Control node
-> model/tool/workflow branch
-> Alephant AI Analytics node
-> cost, policy, and trace output
-> alert, approval, dashboard, or paid endpoint response
```

## 推荐设置

1. 在 Alephant 中为 n8n 工作流创建一个 Agent。
2. 复制该 Agent 的 Virtual Key。
3. 在 n8n 中配置 **Alephant Virtual Key** 凭据。
4. 通过 **Alephant Cost Control** 节点发送 AI 请求。
5. 在工作流中继续传递 `requestId` 或 `requestLogId`。
6. 使用 **Alephant AI Analytics** 获取请求或用量上下文。
7. 将高成本、失败、被阻止或异常情形路由至审批、告警或审核步骤。

对于通过 HTTP Request 节点调用 Alephant Gateway 的工作流，请直接加入 `Alephant-Agent-Id` 和 `Alephant-Run-Id` 请求头。对于使用 Alephant 社区节点的工作流，请保留返回的 `requestId` 和 `requestLogId`，使后续 analytics 步骤可查询网关日志。

## 要追踪的内容

| 工作流字段               | 重要原因                    |
| ------------------- | ----------------------- |
| 工作流名称               | 按自动化任务归类成本和跟踪           |
| 运行 ID               | 连接一次工作流执行中的所有步骤         |
| Agent 或 Virtual Key | 标识受治理的执行主体              |
| 模型和提供商              | 解释路由和成本决策               |
| 请求日志 ID             | 将 n8n 输出关联到 Alephant 日志 |
| 成本和 token           | 展示该次运行的财务影响             |
| 策略决策                | 解释允许、阻止、限流或升级的操作        |

在可能的情况下，请通过 Alephant 网关请求头传递工作流上下文，例如 `x-request-id`、`alephant-session-id`、`alephant-session-name` 和 `alephant-property-*`。

有关具体请求头示例，请参阅 [Agent IDs And Run IDs](/docs/overview/core-concepts/agent-i-ds-and-run-i-ds)。

## 成本感知分支

Alephant analytics 可帮助工作流决定下一步操作：

* 如果预算使用情况健康，则继续。
* 在高成本分支前请求审批。
* 对低风险工作切换到成本更低的模型。
* 如果 Virtual Key 或 Agent 预算已耗尽，则停止。
* 当一次运行超出预期成本时发送通知。

## 从工作流到付费端点

有些 n8n 工作流会成为可复用的能力：线索丰富、研究、合规检查、支持分流或数据转换。

Alephant 可帮助将该工作流封装为受治理的付费端点：

1. 定义工作流能力。
2. 为访问、预算、模型和速率限制附加端点策略。
3. 当端点商业化时要求付款。
4. 在付款和策略通过后运行 n8n 工作流。
5. 记录收入、模型成本、外部支出、延迟和已知利润率。

## 相关页面

* [n8n 节点](/n8n)
* [Agent Gateway](/docs/overview/core-concepts/agent-gateway)
* [Agent Run Tracing](/docs/overview/core-concepts/agent-run-tracing)
* [Paid Endpoints](/docs/overview/monetize/paid-endpoints)