Visual Studio Code ↗ 支持为聊天使用自定义端点模型。将自定义端点指向受 Cloudflare Access 保护的 AI Gateway 自定义域。Visual Studio Code 使用存储在其密钥存储中的 Access 服务令牌进行身份验证。
此集成使用静态凭据。Visual Studio Code 无法使用 cloudflared 生成短期 Access 令牌。
开始之前,你需要:
- 带 自定义域 的 AI Gateway。
- 已在自定义域上启用 Cloudflare Access。
- 由 Service Auth 策略允许的 Access 服务令牌。
- Access 应用已配置为使用
Authorization标头验证服务令牌。 - 每个模型的 Unified Billing 积分或已存储的提供商密钥。
- 已安装并更新到最新版本的 Visual Studio Code。
-
在 Visual Studio Code 中打开命令面板,运行 Chat: Manage Language Models。
-
选择 Add Models > Custom Endpoint。
-
输入组名称,例如
AI Gateway。 -
输入显示名称和 API 密钥。对于 API 密钥,使用以下单标头服务令牌值。将
<CLIENT_ID>和<CLIENT_SECRET>替换为你的 Access 服务令牌值。{"cf-access-client-id":"<CLIENT_ID>","cf-access-client-secret":"<CLIENT_SECRET>"} -
将 API 类型选为 Chat Completions。
-
在打开的
chatLanguageModels.json文件中配置模型。将ai.example.com替换为你的 AI Gateway 自定义域。将模型 ID 和 token 限制替换为你的模型所支持的值。chatLanguageModels.jsonjson [ { "name": "AI Gateway", "vendor": "customendpoint", "apiKey": "${input:cloudflareAccessServiceToken}", "apiType": "chat-completions", "models": [ { "id": "openai/gpt-4.1-mini", "name": "GPT-4.1 mini", "url": "https://ai.example.com/compat/chat/completions", "toolCalling": true, "vision": true, "maxInputTokens": 1000000, "maxOutputTokens": 32768, "requestHeaders": { "Authorization": "${apiKey}" } } ] } ]Visual Studio Code 会将
${apiKey}替换为密钥存储中保存的服务令牌值。覆盖Authorization可防止 Visual Studio Code 的默认身份验证行为添加Bearer前缀。 -
保存文件,然后从聊天模型选择器中选择该模型。
-
发送提示。请求现在会通过 AI Gateway 路由。
有关自定义端点设置的更多信息,请参阅 AI language models in Visual Studio Code ↗。
使用 AI Gateway 自定义域时,WAF 规则
如果该规则阻止了 Visual Studio Code 请求,请创建 WAF 例外,仅针对你的 AI Gateway 自定义域跳过此规则。请尽可能缩小例外范围。
要确认流量已到达 AI Gateway,请参阅验证是否生效。