Cloudflare AI Gateway - 日志溯源 apikey保护

作者:matrix 发布时间:2026-07-31 分类:零零星星

赛博菩萨Cloudflare 功能实在太多了,之前研究了一阵但是没派上用场。这次正好发现 openclaw 出现异常数据请求,这就把AI Gateway加上能更好溯源查看请求/响应日志

Cloudflare AI Gateway支持很多功能, 像接口Proxy翻译,智能路由,BYOK(存储密钥),接口缓存,请求控制,日志查看...

比如我用openclaw接入了 DeepSeek 模型,但是对话日志溯源有点麻烦,并且我还在环境中暴露了DeepSeek的apikey。接入Cloudflare AI Gateway的话 我能控制很多东西了

创建 AI Gateway

创建网关

开启路径:AI Gateway -> 创建网关

Cloudflare AI Gateway - 日志溯源 apikey保护

收集日志

启用日志收集,方便以后溯源查看
Cloudflare AI Gateway - 日志溯源 apikey保护

开启验证网关

请求AI Gateway时需要使用的 apikey
目的是替换掉之前的大模型提供商的 apikey来请求接口,使用Cloudflare单独的鉴权apikey
Cloudflare AI Gateway - 日志溯源 apikey保护

点击创建鉴权令牌

新建的令牌权限选择AI Gateway 运行

Cloudflare AI Gateway - 日志溯源 apikey保护

配置大模型提供商

我这里使用的 DeepSeek,找到「提供程序密钥」tab 页面把DeepSeek 的 apikey 托管到Cloudflare
之后我请求Cloudflare网关接口,携带 cf 的鉴权 token,内部会自动路由请求我需要的大模型api

Cloudflare AI Gateway - 日志溯源 apikey保护

测试


# 使用通用Authorization鉴权请求头 curl https://gateway.ai.cloudflare.com/v1/{cloudflare账号 ID}/{ai网关名}/compat/chat/completions \ -H 'Authorization: Bearer {cf鉴权令牌}' \ -H 'Content-Type: application/json' \ -d '{"model": "deepseek/deepseek-v4-flash", "messages": [{"role": "user", "content": "say hi"}]}' # 使用 cloudflare 的请求头cf-aig-authorization curl https://gateway.ai.cloudflare.com/v1/{cloudflare账号 ID}/{ai网关名}/compat/chat/completions \ -H 'cf-aig-authorization: Bearer {cf鉴权令牌}' \ -H 'Content-Type: application/json' \ -d '{"model": "deepseek/deepseek-v4-flash", "messages": [{"role": "user", "content": "say hi"}]}'

模型参数中设置为 {模型提供商}/{模型ID}

参考:
https://developers.cloudflare.com/ai-gateway/