VS Code Codex 插件 + DeepSeek V4 Pro 完整配置教程
推荐方案:VS Code Codex 插件 + DeepSeek V4 Pro + codex-bridge 本地桥接(最轻量)
概述
本教程将 OpenAI Codex 的 VS Code 插件接入 DeepSeek V4 Pro 模型,通过本地 codex-bridge 桥接,实现:
- ✅ 在 VS Code 中直接使用 Codex Agent 写代码
- ✅ 使用 DeepSeek V4 Pro 模型(而非 OpenAI 官方模型)
- ✅ 本地运行,数据不出机器
- ✅ 无需科学上网,国内网络友好
架构说明
VS Code
└─ Codex 插件 ──→ 本地 bridge ──→ DeepSeek API
↑
(API 格式转换 / 路由)
codex-bridge 是一个本地代理服务,负责:
前置条件
| VS Code | 已安装最新版(1.96+) |
| Node.js | v18+(运行 bridge 用) |
| DeepSeek API Key | 已开通 DeepSeek V4 Pro 权限 |
| 操作系统 | Windows / macOS / Linux 均可 |
第一步:安装 VS Code Codex 插件
打开 VS Code
进入 扩展市场(Ctrl+Shift+X)
搜索 “Codex”(OpenAI 官方出品)
点击 Install
也可以直接从 VS Code 官网扩展市场下载:
https://marketplace.visualstudio.com/items?itemName=OpenAI.codex
安装完成后,左侧会出现 Codex 图标。
第二步:获取 DeepSeek API Key
访问 DeepSeek 开放平台:https://platform.deepseek.com/
登录 账号(未注册则先注册)
进入 API Keys 页面
创建 新的 API Key(如 sk-xxx…xxxx)
确认 账户已开通 V4 Pro 模型权限
💡 DeepSeek V4 Pro 当前定价远低于 OpenAI GPT-4o,且在中文场景和编程任务上表现优秀。
第三步:配置 codex-bridge 本地桥接
方法 A:使用 Node.js 本地代理(推荐)
创建本地 API 桥接服务:
# 1. 创建工作目录
mkdir -p ~/codex-bridge && cd ~/codex-bridge
# 2. 初始化项目
npm init -y
# 3. 安装依赖
npm install express cors body-parser
# 4. 创建桥接脚本
touch bridge.js
编辑 bridge.js:
const express = require('express');
const cors = require('cors');
const crypto = require('crypto');
const app = express();
app.use(cors());
app.use(express.json());
// ⚡ 随机生成本地代理 Key,bridge 启动时打印到终端
const PROXY_API_KEY = '***' + crypto.randomBytes(24).toString('hex');
const DEEPSEEK_API_KEY = 'sk-你的DeepSeek密钥'; // ← 替换为你的 Key
const DEEPSEEK_BASE_URL = 'https://api.deepseek.com/v1';
app.post('/v1/chat/completions', async (req, res) => {
try {
const response = await fetch(`${DEEPSEEK_BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${DEEPSEEK_API_KEY}`
},
body: JSON.stringify({
model: 'deepseek-chat', // DeepSeek V4 Pro
messages: req.body.messages,
max_tokens: req.body.max_tokens || 8192,
temperature: req.body.temperature || 0.7,
stream: req.body.stream || false
})
});
if (req.body.stream) {
// 流式响应
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
response.body.pipeTo(new WritableStream({
write(chunk) {
res.write(chunk);
}
}));
} else {
const data = await response.json();
res.json(data);
}
} catch (error) {
console.error('Bridge error:', error);
res.status(500).json({ error: error.message });
}
});
app.post('/v1/models', async (req, res) => {
// Codex 需要模型列表,告知支持 DeepSeek
res.json({
data: [
{
id: 'deepseek-chat',
object: 'model',
created: Date.now(),
owned_by: 'deepseek'
}
]
});
});
const PORT = 31415;
app.listen(PORT, () => {
console.log(`✅ codex-bridge running on http://localhost:${PORT}`);
console.log(`🔑 Proxy API Key: ${PROXY_API_KEY}`);
console.log(` Copy this key to VS Code Codex settings → codex.apiKey`);
});
方法 B:使用三方桥接工具
如果不想自己写代理,也可以使用已有桥接工具:
# 安装 cc-switch(Codex Custom Switch)
npm install -g cc-switch
# 或使用 one-api 等网关工具
docker run -d –name one-api \\
-p 3000:3000 \\
-v ~/one-api/data:/data \\
songquanpeng/one-api
配置 DeepSeek 渠道即可。
为什么推荐 codex-bridge 而非 cc-switch?
| 代码量 | 一个 JS 文件,约 50 行 | 完整 npm 包 + 依赖链 |
| 依赖 | express + cors(3 个包) | 全局安装,依赖较多 |
| 学习成本 | 开箱即读,5 分钟理解全部逻辑 | 需了解 CLI 参数 / 配置格式 |
| 灵活性 | 可自由修改路由、加日志、加断点 | 固定配置模式,可定制性弱 |
| 调试 | 加 console.log 或 Node 断点即可 | 黑盒运行,出问题难定位 |
| 适用场景 | ✅ 固定使用 DeepSeek V4 Pro | 多模型间频繁切换 |
| 启动方式 | node bridge.js | cc-switch start <配置> |
核心结论:如果目标是"在 VS Code 中用 DeepSeek V4 Pro 写代码"且不需要频繁切换模型,codex-bridge 更轻量、更可控、更好理解。cc-switch 的优势在于多模型管理和开箱即用,适合需要在一台机器上切换 DeepSeek / Claude / GPT 的复杂场景。本教程面向单一模型方案,所以推荐 codex-bridge。
第四步:启动 codex-bridge
cd ~/codex-bridge
node bridge.js
看到类似以下输出即表示启动成功:

上图展示的是 codex-bridge 实际运行效果:代理服务监听在 localhost:7899,自动将 Codex 发起的 gpt-5.5 请求路由到 DeepSeek V4 Pro,并对消息历史做智能裁剪以节省 Token。
将输出的 Proxy API Key 复制到 VS Code Codex 配置的 codex.apiKey 中。
建议将 bridge 设为开机自启(使用 pm2 或 systemd),或放在后台运行:
# 后台运行
nohup node bridge.js > bridge.log 2>&1 &
# 或使用 pm2
npm install -g pm2
pm2 start bridge.js –name codex-bridge
pm2 save
第五步:配置 VS Code Codex 插件
打开命令面板:Ctrl+Shift+P
搜索设置:“Codex: Open Settings” 或 “Codex: Configure”
配置项如下:
{
// 将 API 地址指向本地 bridge
"codex.apiBaseUrl": "http://localhost:31415/v1",
// API Key:填入 bridge 启动时打印的 Key(见终端输出)
"codex.apiKey": "sk-pro…xxxx",
// 指定模型
"codex.model": "deepseek-chat",
// 关闭 OpenAI 官方验证(使用自定义 endpoint 时)
"codex.skipAuthValidation": true
}
也可以直接在 VS Code 的 settings.json 中添加:
"codex.apiBaseUrl": "http://localhost:31415/v1",
"codex.apiKey": "sk-pro…xxxx",
"codex.model": "deepseek-chat",
"codex.skipAuthValidation": true
第六步:测试运行
打开项目:在 VS Code 中打开一个项目文件夹
启动Agent:Ctrl+Shift+P → 选择 “Codex: Start Agent”
输入指令:
你是什么模型?

上图是配置成功后的实际效果。可以看到 Codex Agent 正常启动并响应,虽然它自称是 GPT-5(Codex 默认标签),实际全部请求已通过 codex-bridge 路由到 DeepSeek V4 Pro。Agent 可以正常读写文件、执行命令、分析代码等。
使用其他测试指令验证功能:
帮我写一个 Python 函数,从 CSV 文件读取数据并计算每列的平均值
Codex Agent 会:
- ✅ 创建任务计划
- ✅ 生成代码文件
- ✅ 显示实时进度
- ✅ 最终输出结果
如果看到代码正确生成,说明配置成功 🎉
使用技巧
常用 Codex 命令
| Codex: Start Agent | 启动 Agent 会话 |
| Codex: Quick Chat | 快速问答(不操作文件) |
| Codex: Code Review | 审查当前文件代码 |
| Codex: Fix Bug | 自动修复 Bug |
| Codex: Explain Code | 解释选中代码 |
最佳实践
每个任务单独会话:完成一个功能后关闭再开新会话
用自然语言描述需求:越具体,Codex 产出越精准
结合 Git 使用:Codex 修改代码前会自动创建 Git commit
善用 @ 引用文件:在对话中用 @文件名 让 Codex 关注特定文件
复杂任务分步描述:先描述整体架构,再逐步细化
性能调优
// 根据需求调整 VS Code 中 Codex 配置
"codex.maxTokens": 8192, // 最大 Token 数
"codex.temperature": 0.7, // 创造力 (0-1)
"codex.timeout": 120, // 超时时间(秒)
常见问题
Q: 启动 Codex Agent 后提示 “API Key 验证失败”
原因:Codex 对自定义 endpoint 可能校验 Key 格式。
解决:确认 codex.apiKey 与 bridge 启动时打印的 Key 一致。在 VS Code settings.json 中增加:
"codex.skipAuthValidation": true,
"codex.apiKey": "sk-pro…xxxx"
Key 忘了的话重新生成:
node -e "console.log('***' + require('crypto').randomBytes(24).toString('hex'))"

如果显示这种错误的话,需要使用命令,vscode中 使用代理的key是通过这个命令随机生成的
node -e "console.log('sk-proxy-local-' + require('crypto').randomBytes(24).toString('hex'))"
修改.env里面的PROXY_AUTH_KEY这个值
Q: DeepSeek 返回速度慢或不稳定
原因:DeepSeek API 响应时间受网络和负载影响。
解决:
- 检查网络连接是否稳定
- 减少 max_tokens 到 4096
- 降低 temperature 到 0.3(更确定的输出)
Q: Bridge 启动后端口被占用
原因:31415 端口已被其他程序使用。
解决:修改 bridge.js 中的端口号,同时更新 VS Code 配置中的 codex.apiBaseUrl。
Q: 想切换回 OpenAI 官方模型
解决:在 VS Code settings 中注释掉自定义配置,或直接设为 OpenAI 官方地址:
// 注释掉以下行即可恢复官方默认
// "codex.apiBaseUrl": "http://localhost:31415/v1",
// "codex.skipAuthValidation": true
总结
VS Code Codex 插件
↓ 使用熟悉的 IDE 界面
codex-bridge 本地桥接
↓ API 路由与格式转换
DeepSeek V4 Pro 模型
↓ 高性能中文编程
你的代码 ✅
这套方案的优势:
- 轻量:一个 Node.js 脚本搞定,无需 Docker
- 省钱:DeepSeek V4 Pro 价格远低于 OpenAI
- 可控:数据走本地 bridge,不经过第三方代理
- 灵活:可随时切换模型或增加新的模型渠道
版本信息:教程编写于 2026-06-14
适用环境:VS Code 1.96+ / Codex 插件 / DeepSeek V4 Pro
如有问题:请在后台留言咨询



