# Cloudflare AI 网关

> Cloudflare AI Gateway 设置（身份验证 + 模型选择）

- 网址：https://funcoding.ai/agents/openclaw/providers/cloudflare-ai-gateway/
- 来源：OpenClaw 官方文档原文（中文），MIT 许可，同步于 2026-10-11
- 官方原文：https://docs.openclaw.ai/zh-CN/providers/cloudflare-ai-gateway

---
[Cloudflare AI Gateway](https://developers.cloudflare.com/ai-gateway/) 位于提供商 API 前端，并提供分析、缓存和控制功能。对于 Anthropic，OpenClaw 通过你的 Gateway 网关端点使用 Anthropic Messages API。

| 属性          | 值                                                                                       |
| ------------- | ---------------------------------------------------------------------------------------- |
| 提供商        | `cloudflare-ai-gateway`                                                                       |
| 插件          | 官方外部软件包（`@openclaw/cloudflare-ai-gateway-provider`）                                                     |
| 基础 URL      | `https://gateway.ai.cloudflare.com/v1/<account_id>/<gateway_id>/anthropic`                                                                       |
| 默认模型      | `cloudflare-ai-gateway/claude-sonnet-4-6`                                                                       |
| API 密钥      | `CLOUDFLARE_AI_GATEWAY_API_KEY`（用于通过 Gateway 网关发出请求的提供商 API 密钥）                     |

<div class="callout callout-note">

对于通过 Cloudflare AI Gateway 路由的 Anthropic 模型，请使用你的 **Anthropic API 密钥**作为提供商密钥。

</div>

为 Anthropic Messages 模型启用思考功能后，OpenClaw 会先移除末尾的
助手预填充轮次，再通过 Cloudflare AI Gateway 发送载荷。
Anthropic 拒绝在扩展思考功能下进行响应预填充，而普通的
非思考预填充仍然可用。

## 安装插件

安装官方插件，然后重启 Gateway 网关：

```bash
openclaw plugins install @openclaw/cloudflare-ai-gateway-provider
openclaw gateway restart
```

## 入门指南

**设置提供商 API 密钥和 Gateway 网关详细信息**

运行新手引导并选择 Cloudflare AI Gateway 身份验证选项：

```bash
openclaw onboard --auth-choice cloudflare-ai-gateway-api-key
```

系统会提示你输入账户 ID、Gateway 网关 ID 和 API 密钥。

**设置默认模型**

将模型添加到你的 OpenClaw 配置中：

```json5
{
  agents: {
    defaults: {
      model: { primary: "cloudflare-ai-gateway/claude-sonnet-4-6" },
    },
  },
}
```

**验证模型可用**

```bash
openclaw models list --provider cloudflare-ai-gateway
```

## 非交互式示例

对于脚本化或 CI 设置，请在命令行中传递所有值：

```bash
openclaw onboard --non-interactive \
  --mode local \
  --auth-choice cloudflare-ai-gateway-api-key \
  --cloudflare-ai-gateway-account-id "your-account-id" \
  --cloudflare-ai-gateway-gateway-id "your-gateway-id" \
  --cloudflare-ai-gateway-api-key "$CLOUDFLARE_AI_GATEWAY_API_KEY"
```

## 高级配置

<details>
<summary>经过身份验证的 Gateway 网关</summary>

如果你已在 Cloudflare 中启用 Gateway 网关身份验证，请添加 `cf-aig-authorization` 标头。此标头是对提供商 API 密钥的**额外补充**。

```json5
{
  models: {
    providers: {
      "cloudflare-ai-gateway": {
        headers: {
          "cf-aig-authorization": "Bearer <cloudflare-ai-gateway-token>",
        },
      },
    },
  },
}
```

<div class="callout callout-tip">

`cf-aig-authorization` 标头用于向 Cloudflare Gateway 网关本身进行身份验证，而提供商 API 密钥（例如你的 Anthropic 密钥）用于向上游提供商进行身份验证。

</div>

</details>

<details>
<summary>环境说明</summary>

如果 Gateway 网关作为守护进程（launchd/systemd）运行，请确保该进程可以访问 `CLOUDFLARE_AI_GATEWAY_API_KEY`。

<div class="callout callout-warning">

仅在交互式 shell 中导出的密钥无法供 launchd/systemd 守护进程使用，除非该环境也被导入其中。请在 `~/.openclaw/.env` 中或通过 `env.shellEnv` 设置密钥，以确保 Gateway 网关进程能够读取它。

</div>

</details>

## 相关内容

- [模型选择](https://funcoding.ai/agents/openclaw/concepts/model-providers/)：选择提供商、模型引用和故障转移行为。
- [故障排查](https://funcoding.ai/agents/openclaw/help/troubleshooting/)：常规故障排查和常见问题。
