
1. 为什么要在 VS Code Copilot 里接一个外部 Agent你可能已经在 VS Code 里用 Copilot 补全代码、写注释、生成单元测试但一旦想让 Copilot 帮你做点带脑子的事——比如读一个本地文件、查一次接口、根据报错自动改配置——就会发现它只能聊天不能真正动手。这就是 AI Agent 和普通对话助手的区别Agent 能感知环境、调用工具、根据结果决定下一步而普通对话只能一问一答。我这次要做的是在 VS Code 里从零搭一个最小可跑的 AI Agent让它通过 TaoToken 这个统一 Key/API 通道去调用外部模型而不是把 Key 散落在各个插件配置里。TaoToken 在这里扮演的角色很简单你只需要维护一份 API Key 和 Base URLCopilot 侧、脚本侧、Agent 侧都指向同一个入口换模型、换 Key 只改一处。适合谁适合已经在用 VS Code、想动手跑通第一个 Agent、又不想被各家 SDK 的鉴权细节绕晕的人。整篇的路线是先给settings.json和config.toml两个骨架把 TaoToken 作为统一通道接进去再补工具调用最后做一次端到端验证并把我踩过的报错整理成排查表。照着做完你本地能跑通一个最小 Agent。2. 前置准备TaoToken 统一 Key 与本地环境2.1 拿到统一 Key 和 Base URLTaoToken 的官网入口是 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content API 地址固定为 https://taotoken.net/api 这个不加 UTM。你需要先在控制台创建一个 API Key后面所有配置都复用它。创建 Key 的入口在控制台里https://taotoken.net/console?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content Key 列表页在 https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。建议给这个 Key 起个能认出来的名字比如vscode-agent-local方便以后按用途吊销。注意Key 只显示一次复制后先存到本地环境变量或密码管理器别直接写进会提交到 Git 的文件里。2.2 本地需要装什么VS Code 本体、Copilot 扩展、Python 3.10Agent 脚本用以及一个能发 HTTP 请求的终端工具curl 或 PowerShell 的Invoke-RestMethod都行。模型侧不需要你单独申请统一走 TaoToken 的通道即可。2.3 把 Key 放进环境变量Windows PowerShellsetx TAOTOKEN_API_KEY sk-你的KeymacOS / Linuxexport TAOTOKEN_API_KEYsk-你的Key echo export TAOTOKEN_API_KEYsk-你的Key ~/.zshrc这样配置文件和脚本里都用TAOTOKEN_API_KEY引用不硬编码。3. 可复制配置settings.json 与 config.toml 骨架3.1 VS Code 侧 settings.jsonVS Code 的用户设置文件在~/.config/Code/User/settings.jsonLinux、~/Library/Application Support/Code/User/settings.jsonmacOS或%APPDATA%\Code\User\settings.jsonWindows。把下面这段合并进去重点是让 Copilot 相关请求走统一入口{ github.copilot.advanced: { debug.overrideProxyUrl: https://taotoken.net/api, debug.overrideChatUrl: https://taotoken.net/api/v1/chat/completions, debug.overrideModel: gpt-4.1 }, github.copilot.chat.localeOverride: zh-CN, terminal.integrated.env.windows: { TAOTOKEN_API_KEY: ${env:TAOTOKEN_API_KEY} }, terminal.integrated.env.linux: { TAOTOKEN_API_KEY: ${env:TAOTOKEN_API_KEY} }, terminal.integrated.env.osx: { TAOTOKEN_API_KEY: ${env:TAOTOKEN_API_KEY} } }这里overrideProxyUrl和overrideChatUrl指向 TaoToken 的 API 地址overrideModel指定默认模型。不同 Copilot 版本字段名可能略有差异如果某个字段不生效先确认你的扩展版本再对照官方文档调整。3.2 Agent 侧 config.tomlAgent 脚本用 TOML 管理配置放在项目根目录config.toml[provider] name taotoken base_url https://taotoken.net/api api_key_env TAOTOKEN_API_KEY model gpt-4.1 timeout_seconds 60 [agent] max_history 100 system_prompt 你是一个可以调用工具的本地助手回答前先判断是否需要调用工具。 tool_allowlist [read_file, write_file, run_shell] [tools.read_file] description 读取本地文本文件 params [path] [tools.write_file] description 写入本地文本文件 params [path, content] [tools.run_shell] description 执行一条 shell 命令并返回输出 params [command]api_key_env指向环境变量名脚本运行时读取不落盘。tool_allowlist控制 Agent 能碰哪些工具先只开这三个跑通后再加。3.3 两个配置怎么配合settings.json管的是 VS Code 里 Copilot 聊天面板的请求走向config.toml管的是你自己写的 Agent 脚本。两者共用同一个TAOTOKEN_API_KEY和同一个base_url这就是统一 Key的实际含义换 Key 只改环境变量换模型只改两处model字段。4. 工具调用与端到端验证4.1 最小 Agent 脚本新建agent.py核心逻辑是读 config.toml → 组装消息 → 调 TaoToken 的 chat completions → 如果模型返回工具调用就执行 → 把结果回灌再问一轮。import os, json, tomllib, subprocess, urllib.request with open(config.toml, rb) as f: cfg tomllib.load(f) API_KEY os.environ[cfg[provider][api_key_env]] BASE_URL cfg[provider][base_url].rstrip(/) MODEL cfg[provider][model] def call_model(messages, toolsNone): payload {model: MODEL, messages: messages} if tools: payload[tools] tools req urllib.request.Request( f{BASE_URL}/v1/chat/completions, datajson.dumps(payload).encode(), headers{ Authorization: fBearer {API_KEY}, Content-Type: application/json, }, ) with urllib.request.urlopen(req, timeoutcfg[provider][timeout_seconds]) as r: return json.loads(r.read()) def run_tool(name, args): if name read_file: return open(args[path], encodingutf-8).read() if name write_file: open(args[path], w, encodingutf-8).write(args[content]) return written if name run_shell: return subprocess.run(args[command], shellTrue, capture_outputTrue, textTrue).stdout return funknown tool: {name} TOOLS [ {type: function, function: {name: read_file, description: 读取本地文本文件, parameters: {type: object, properties: {path: {type: string}}, required: [path]}}}, {type: function, function: {name: run_shell, description: 执行 shell 命令, parameters: {type: object, properties: {command: {type: string}}, required: [command]}}}, ] def agent_loop(user_input): messages [ {role: system, content: cfg[agent][system_prompt]}, {role: user, content: user_input}, ] for _ in range(5): resp call_model(messages, TOOLS) msg resp[choices][0][message] messages.append(msg) if not msg.get(tool_calls): return msg[content] for tc in msg[tool_calls]: args json.loads(tc[function][arguments]) result run_tool(tc[function][name], args) messages.append({role: tool, tool_call_id: tc[id], content: str(result)}) return 达到最大轮次 if __name__ __main__: print(agent_loop(读一下 config.toml 并告诉我默认模型是什么))4.2 先验证通道是否通在跑 Agent 之前先用一条 curl 确认 TaoToken 通道正常curl -s https://taotoken.net/api/v1/chat/completions \ -H Authorization: Bearer $TAOTOKEN_API_KEY \ -H Content-Type: application/json \ -d {model:gpt-4.1,messages:[{role:user,content:回复 ok}]}返回里能看到choices[0].message.content就说明 Key 和地址都对。这一步不通后面 Agent 一定不通先解决这里。4.3 跑通 Agentpython agent.py预期输出类似模型先判断需要读文件返回一个tool_calls脚本执行read_file拿到config.toml内容回灌后再问一轮最终输出默认模型是 gpt-4.1。整个过程你能在终端看到两轮请求这就是最小 Agent 的完整闭环。4.4 在 Copilot 聊天面板里验证打开 VS Code 的 Copilot Chat输入用一句话解释这个项目的 config.toml 作用。如果settings.json的 override 生效请求会走 TaoToken如果没生效说明字段名和你的扩展版本不匹配回到 3.1 调整。5. 本篇常见报错排查报错现象可能原因处理方式401 UnauthorizedKey 没读到或写错确认TAOTOKEN_API_KEY在当前终端可见echo $TAOTOKEN_API_KEY有值404 Not Foundbase_url 拼错确认是https://taotoken.net/api路径补/v1/chat/completionsmodel not found模型名不在通道支持列表换成gpt-4.1或到模型对话页确认可用模型工具调用不触发模型不支持 function calling换支持工具调用的模型或检查tools字段格式tool_call_id不匹配回灌消息缺 id每条 tool 消息必须带对应tool_call_idCopilot 面板仍走原通道settings.json 字段未生效检查扩展版本重启 VS Code确认 JSON 无语法错误超时网络或 timeout 太短把timeout_seconds调到 60 以上再试排查顺序建议先 curl 验证通道 → 再跑agent.py看单轮请求 → 最后接工具调用。哪一步断就在哪一步查别跳。6. 接下来怎么用跑通之后你可以把tool_allowlist逐步放开加write_file、加自定义工具让 Agent 处理更具体的本地任务。想验证不同模型在工具调用上的表现可以直接在模型对话页试https://taotoken.net/models?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。如果你打算长期跑编码类 Agent、需要更稳定的额度和并发可以看 Coding Planhttps://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。接入细节和字段说明在文档里https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。Key 管理还是回到 API Keys 页https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。一个实用技巧把config.toml里的max_history设小一点比如 20本地调试时上下文短、响应快等逻辑稳定了再调大。工具调用的参数校验最好在run_tool里加一层白名单判断别让模型随便传路径进来。