惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
腾讯CDC
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
B
Blog
S
SegmentFault 最新的问题
P
Privacy & Cybersecurity Law Blog
T
Threatpost
博客园 - 聂微东
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
C
Check Point Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
爱范儿
爱范儿
IT之家
IT之家
S
Secure Thoughts
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
有赞技术团队
有赞技术团队
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
雷峰网
雷峰网
Project Zero
Project Zero
博客园 - Franky
H
Heimdal Security Blog
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
Codex 自定义模型配置与使用技巧
微信公众号 · 2026-06-23 · via 陈少文的网站

Please enable Javascript to view the contents

参考:Codex 配置基础Codex 高级配置

1. 配置文件在哪

Codex 的用户配置默认在 ~/.codex/config.toml(Windows 为 %USERPROFILE%\.codex\config.toml)。

配置优先级(高 → 低):

  1. CLI 参数(--model--config 等)
  2. 项目配置 .codex/config.toml(仅 trusted 项目生效)
  3. Profile 文件 ~/.codex/<profile>.config.toml(通过 --profile 激活)
  4. 用户配置 ~/.codex/config.toml
  5. 系统配置 /etc/codex/config.toml

安全边界: 项目级 .codex/config.toml 不能 修改 model_providermodel_providers、认证相关项。克隆的仓库无法悄悄把你的请求转发到第三方 endpoint,Provider 定义必须写在用户级配置里。

2. 自定义模型 Provider

Codex 通过 [model_providers.<id>] 定义第三方 endpoint。顶层两个键决定实际调用:

  • model — 模型 ID
  • model_provider — 使用哪个 Provider

2.1 最小配置

1
2
3
4
5
6
7
model = "your-model-id"
model_provider = "custom"

[model_providers.custom]
name = "Custom Provider"
base_url = "https://api.example.com/v1"
env_key = "CUSTOM_API_KEY"
  • base_url — API 地址,必填
  • env_key — 从环境变量读取 API Key,推荐方式
  • wire_api — 默认 "responses",目前只支持 Responses API,可省略

设置 API Key:

1
export CUSTOM_API_KEY="sk-..."

2.2 wire_api 注意事项

2026 年起 Codex 只走 OpenAI Responses API,不再支持 wire_api = "chat"

如果 endpoint 只有 Chat Completions(很多自建网关、旧版 Ollama),直接配会 404 或空流。需要在中间加一层转换网关,例如 LiteLLM、OpenRouter 的 Responses 兼容面。

2.3 内置 OpenAI 改代理地址

只是给官方 OpenAI Provider 换 base URL,不需要新建 Provider:

1
2
openai_base_url = "https://us.api.openai.com/v1"
model = "gpt-5.5"

不能 创建 [model_providers.openai]openaiollamalmstudio 是保留 ID。

3. 常见 Provider 配置

3.1 OpenRouter

一个 Key 访问多种模型:

1
2
3
4
5
6
7
model = "anthropic/claude-sonnet-4.6"
model_provider = "openrouter"

[model_providers.openrouter]
name = "OpenRouter"
base_url = "https://openrouter.ai/api/v1"
env_key = "OPENROUTER_API_KEY"
1
export OPENROUTER_API_KEY="sk-or-v1-..."

3.2 Azure OpenAI

Azure 通过 api-version 查询参数做版本控制:

1
2
3
4
5
6
7
8
model = "gpt-5.5"
model_provider = "azure"

[model_providers.azure]
name = "Azure OpenAI"
base_url = "https://YOUR_RESOURCE.openai.azure.com/openai"
env_key = "AZURE_OPENAI_API_KEY"
query_params = { api-version = "2025-04-01-preview" }

model 填 Azure 上的 deployment name,不是模型系列名。

3.3 本地 Ollama

1
2
3
4
5
6
model = "qwen3.5-coder"
model_provider = "local-ollama"

[model_providers.local-ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"

本地启动:

1
2
ollama pull qwen3.5-coder
codex --oss

--oss 走本地开源 Provider 流程;也可在配置里设 oss_provider = "ollama"

3.4 DeepSeek

1
2
3
4
5
6
7
model = "deepseek-chat"
model_provider = "deepseek"

[model_providers.deepseek]
name = "DeepSeek"
base_url = "https://api.deepseek.com"
env_key = "DEEPSEEK_API_KEY"

若 endpoint 不接受 Responses API,同样需要通过网关转发。

3.5 企业内网网关

静态 Header + 环境变量 Key:

1
2
3
4
5
6
7
8
model = "internal-coder"
model_provider = "gateway"

[model_providers.gateway]
name = "Internal LLM Gateway"
base_url = "https://llm-gateway.internal.example.com/v1"
env_key = "INTERNAL_LLM_KEY"
http_headers = { "X-Team" = "platform" }

短效 Token 用 command 认证(不能与 env_key 同时使用):

1
2
3
4
5
[model_providers.gateway.auth]
command = "get-llm-token"
args = ["--scope", "codex"]
timeout_ms = 5000
refresh_interval_ms = 300000

4. Profile:按场景切换模型

Profile 是独立的 overlay 配置文件,命名规则:~/.codex/<profile>.config.toml

1
2
3
4
5
6
7
8
# ~/.codex/deepseek.config.toml
model = "deepseek-chat"
model_provider = "deepseek"

[model_providers.deepseek]
name = "DeepSeek"
base_url = "https://api.deepseek.com"
env_key = "DEEPSEEK_API_KEY"
1
2
3
4
5
6
7
# ~/.codex/local.config.toml
model = "qwen3.5-coder"
model_provider = "local-ollama"

[model_providers.local-ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"

切换使用:

1
2
3
codex --profile deepseek "批量重命名 userId 为 user_id"
codex --profile local --oss "解释这个模块"
codex exec --profile deepseek "review this change"

Profile 文件只写与默认配置不同的项即可。Provider 定义可以放在 Profile 里,也可以全部放在 config.toml,Profile 只改 modelmodel_provider

注意: 旧版 profile = "xxx"[profiles.xxx] 写法已废弃,改用 --profile + 独立文件。

5. 模型行为调优

config.toml 里调整推理和输出:

1
2
3
4
model = "gpt-5.5"
model_reasoning_effort = "medium"   # low / medium / high / xhigh
model_verbosity = "low"              # 缩短回复,仅 Responses API 生效
model_reasoning_summary = "none"     # 关闭推理摘要

单次覆盖,不必改文件:

1
2
3
codex --model gpt-5.5
codex --config model_reasoning_effort='"xhigh"' "review 这个 PR"
codex --config sandbox_workspace_write.network_access=true "跑集成测试"

6. MCP 与 AGENTS.md

6.1 接入 MCP Server

config.toml 里声明 MCP,Codex 启动时自动拉起:

1
2
3
[mcp_servers.codegraph]
command = "codegraph"
args = ["serve", "--mcp"]

带环境变量的 MCP:

1
2
3
4
[mcp_servers.morph]
command = "npx"
args = ["-y", "@morphllm/morphmcp"]
env = { "MORPH_API_KEY" = "your-key" }

单次禁用某个 MCP:

1
codex --config mcp_servers.codegraph.enabled=false

6.2 AGENTS.md

~/.codex/AGENTS.md 是全局项目指引,Codex 进入仓库时会读取。适合放跨项目的通用规则,例如:

1
2
3
4
## CodeGraph

在存在 `.codegraph/` 目录的仓库里,理解代码结构时优先用 CodeGraph MCP,
不要先用 grep 扫全库。

项目级指引放在仓库根目录的 AGENTS.md,Codex 从工作目录向上查找项目根(默认以 .git 为标记)。

7. 项目信任与沙箱

标记 trusted 项目后,Codex 才会加载项目内的 .codex/config.toml、hooks、rules:

1
2
[projects."/Users/me/Code/my-repo"]
trust_level = "trusted"

沙箱相关常用项:

1
2
3
4
5
6
approval_policy = "on-request"    # untrusted / on-request / never
sandbox_mode = "workspace-write"  # 仅写工作区

[sandbox_workspace_write]
network_access = false            # 默认禁网,需要时改为 true
writable_roots = ["/Users/me/.pyenv/shims"]

需要完全放开(仅在已隔离环境使用):

1
sandbox_mode = "danger-full-access"

8. 使用技巧

8.1 日常开发

  • 大任务用 Profile 分流:日常写代码走 OpenAI 默认配置;批量重构、文档生成切 --profile deepseek 省成本
  • 开始实现前清上下文:复杂任务开新 session,把设计文档、计划文件当作结构化上下文,不必把全部聊天历史塞进去
  • 项目级 .codex/config.toml 放团队共识:MCP、沙箱策略、排除路径等,Provider 和 Key 仍放用户级

8.2 CLI 技巧

1
2
3
4
5
6
7
8
# 非交互执行,适合脚本和 CI
codex exec "fix the failing tests"

# 查看当前读到的配置
cat ~/.codex/config.toml

# 指定 CODEX_HOME(多账号隔离)
CODEX_HOME=~/.codex-work codex

8.3 排错 checklist

现象常见原因
启动即 404 / 空流endpoint 不支持 Responses API
Key 无效env_key 对应的环境变量未 export
项目 MCP 不生效项目未标记 trusted
Profile 不生效仍在用废弃的 profile = "xxx" 写法
本地模型不编辑文件模型不支持 tool calling

8.4 与 Cursor 的分工

Codex CLI 适合终端内长任务、脚本化执行(codex exec)、企业内网网关场景。Cursor 适合 IDE 内联编辑、多文件 diff 可视化。两者可以共用 MCP 配置思路,但配置文件路径不同(Codex 用 ~/.codex/config.toml,Cursor 用 ~/.cursor/mcp.json)。

9. 完整示例

一个兼顾默认 OpenAI、便宜模型、本地模型的用户级配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# ~/.codex/config.toml
model = "gpt-5.5"
model_reasoning_effort = "medium"

[projects."/Users/me/Code"]
trust_level = "trusted"

[mcp_servers.codegraph]
command = "codegraph"
args = ["serve", "--mcp"]
1
2
3
4
5
6
7
8
# ~/.codex/deepseek.config.toml
model = "deepseek-chat"
model_provider = "deepseek"

[model_providers.deepseek]
name = "DeepSeek"
base_url = "https://api.deepseek.com"
env_key = "DEEPSEEK_API_KEY"

日常使用:

1
2
3
codex                          # 默认 gpt-5.5
codex --profile deepseek       # 切 DeepSeek
codex exec --profile deepseek "生成 CHANGELOG"

微信公众号