接入 Agent 工具

VS Code

通过 GitHub Copilot Chat 的 BYOK(自带密钥)功能,将 VS Code 对接到 AutoRouter

VS Code

Visual Studio Code 是微软推出的开源代码编辑器。自 VS Code 1.122 起,GitHub Copilot Chat 支持 BYOK(Bring Your Own Key,自带密钥) 模式,可通过 Custom Endpoint(自定义端点) 提供商接入任意 OpenAI 兼容、Responses API 或 Anthropic Messages API 端点,从而将 VS Code 对接到 AutoRouter,使用 AutoRouter 网关分发的各类模型。

需要安装 GitHub Copilot Chat 扩展,且 VS Code 版本为 1.122 或更高。Copilot Business / Enterprise 用户需由组织管理员在 GitHub Copilot 策略中启用「Bring Your Own Language Model Key in VS Code」。

📦 接入前准备

准备信息

  • 已安装 VS Code 1.122 或更高版本
  • 已安装并启用 GitHub Copilot Chat 扩展
  • 一个可用的 AutoRouter 地址
  • 一个可用的 AutoRouter API Key(在控制台生成)
  • 想使用的模型名称(如 claude-sonnet-4-6gpt-5.4 等,需与 AutoRouter 控制台中的模型 ID 完全一致)

🚀 第一步:添加 Custom Endpoint 提供商

  1. 在 VS Code 中打开命令面板(Cmd+Shift+P / Ctrl+Shift+P)。
  2. 运行 Chat: Manage Language Models(聊天:管理语言模型)。
  3. 选择 AddCustom Endpoint(自定义端点)。
  4. 按提示填写:
    • 组名称:例如 AutoRouter
    • API Key:输入在 AutoRouter 控制台生成的 API Key(VS Code 会安全存储,并在配置文件中生成 ${input:chat.lm.secret...} 引用)
    • API 类型:根据要接入的模型选择(见下方说明)
  5. VS Code 会自动打开 chatLanguageModels.json 配置文件。

API 类型选择

API 类型适用模型AutoRouter 端点
chat-completionsGPT、DeepSeek、Gemini 等 OpenAI 兼容模型https://autorouter.top/v1/chat/completions
responses支持 OpenAI Responses API 的模型https://autorouter.top/v1/responses
messagesClaude 等 Anthropic 协议模型(推荐https://autorouter.top/v1/messages

对于 Claude 等推理模型,推荐使用 messages 类型,可避免 Chat Completions 协议下 reasoning_content 回传导致的 400 错误。

🔧 第二步:配置模型

chatLanguageModels.json 中填写模型详情。以下为同时接入多种协议模型的完整示例:

[
  {
    "name": "AutoRouter",
    "vendor": "customendpoint",
    "apiKey": "${input:chat.lm.secret.example}",
    "apiType": "messages",
    "models": [
      {
        "id": "claude-sonnet-4-6",
        "name": "Claude Sonnet 4.6 (AutoRouter)",
        "url": "https://autorouter.top/v1/messages",
        "apiType": "messages",
        "toolCalling": true,
        "vision": true,
        "maxInputTokens": 200000,
        "maxOutputTokens": 8192
      },
      {
        "id": "gpt-5.4",
        "name": "GPT 5.4 (AutoRouter)",
        "url": "https://autorouter.top/v1/chat/completions",
        "apiType": "chat-completions",
        "toolCalling": true,
        "vision": true,
        "maxInputTokens": 128000,
        "maxOutputTokens": 16384
      }
    ]
  }
]

字段要点

  • apiKey 使用 VS Code 在添加提供商时自动生成的 ${input:chat.lm.secret...} 引用,不要将明文 API Key 直接写入 JSON 文件。
  • id 必须与 AutoRouter 控制台中暴露的模型 ID 完全一致
  • url 必须是完整的端点地址(含 /v1/chat/completions/v1/messages/v1/responses)。
  • 若模型支持工具调用或图像输入,请将 toolCalling / vision 设为 true,否则 Agent 模式可能无法正常使用这些能力。

保存文件后,模型会出现在 Copilot Chat 的模型选择器中。

✅ 第三步:验证接入

  1. 打开 VS Code 的 Copilot Chat 面板。
  2. 在模型选择器中选择刚添加的 AutoRouter 模型(如 Claude Sonnet 4.6 (AutoRouter))。
  3. 发送一条测试消息,例如:
帮我写一个读取 JSON 文件的 Python 函数

若模型正常响应,说明接入成功。

🔄 切换模型

在 Copilot Chat 面板的模型选择器中直接切换即可。如需添加更多模型,编辑 chatLanguageModels.json,在 models 数组中追加条目并保存。

❓ 常见问题

找不到 Custom Endpoint 选项

  • 确认 VS Code 版本为 1.122 或更高(帮助 → 关于)。
  • 确认已安装 GitHub Copilot Chat 扩展。
  • Copilot Business / Enterprise 用户需联系管理员在 GitHub Copilot 策略中启用 BYOK。

报错 401 Unauthorized 或连接失败

  • 检查 API Key 是否正确,可在 AutoRouter 控制台重新生成。
  • 确认 url 为完整端点地址,且 id 与 AutoRouter 控制台中的模型 ID 一致。

Claude 模型报 400 Bad Request

将对应模型的 apiType 改为 messages,并将 url 设为 https://autorouter.top/v1/messages

未登录 GitHub 账号时辅助功能不可用

未登录 GitHub 时,内置辅助模型(如标题生成)不可用。可在设置中将 chat.utilityModelchat.utilitySmallModel 指向已配置的 BYOK 模型。

Agent 模式无法调用工具

确认模型配置中 toolCalling 已设为 true,且所选模型在 AutoRouter 中支持 Tool Calling。

目录