Skip to content

CodeX CLI 集成教程

CodeX CLI 是 OpenAI 开源的命令行 AI 编程助手。本文介绍如何配置 CodeX CLI 使用玄州API。

配置步骤

方法一:配置文件(推荐)

CodeX CLI 的配置文件位于 ~/.codex/config.toml(Linux/Mac)或 %USERPROFILE%\.codex\config.toml(Windows)。

首先创建配置目录和文件:

bash
mkdir -p ~/.codex

编辑 ~/.codex/config.toml

toml
#:schema https://developers.openai.com/codex/config-schema.json
openai_base_url = "https://xuanzhouapi.top/v1"
model = "gpt-5.5"
wire_api = "chat"

重要:必须设置 wire_api = "chat",因为大多数第三方 API(包括玄州API)只支持 Chat Completions 格式,不支持 OpenAI 的 Responses API。

方法二:环境变量

bash
export OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
export OPENAI_BASE_URL=https://xuanzhouapi.top/v1

配置完成后直接运行:

bash
codex

方法三:自定义 Provider

~/.codex/config.toml 中定义自定义 provider:

toml
#:schema https://developers.openai.com/codex/config-schema.json
model_provider = "xuanzhou"

[model_providers.xuanzhou]
name = "玄州API"
base_url = "https://xuanzhouapi.top/v1"
wire_api = "chat"
env_key = "XUANZHOU_API_KEY"

然后设置环境变量:

bash
export XUANZHOU_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx

指定模型

bash
# 使用 GPT-5.5
codex --model gpt-5.5

# 使用 Claude Sonnet 4.6
codex --model claude-sonnet-4-6

# 使用 DeepSeek V4 Pro
codex --model deepseek-v4-pro

设置默认模型

在配置文件中设置:

toml
model = "claude-sonnet-4-6"

验证配置

新版 CodeX CLI 提供 codex doctor 命令用于诊断:

bash
codex doctor

该命令会检查认证状态、网络连通性和配置合法性。

也可以直接测试:

bash
codex exec "你好,请确认连接正常,用中文回复"

推荐模型

使用场景推荐模型
复杂代码重构/大型项目gpt-5.5 / claude-opus-4-7
日常编码辅助claude-sonnet-4-6
快速脚本/小型修改deepseek-v4-flash / gpt-5.5-s

常见问题

1. 连接失败

  • 确认 API Key 完整(包括 sk- 前缀)
  • 确认 openai_base_url 格式为 https://xuanzhouapi.top/v1
  • 确认 wire_api = "chat" 已设置

2. 报错 "Unsupported wire_api"

  • 说明目标 API 不支持 Responses API,必须设置 wire_api = "chat"
  • 检查配置文件中是否有拼写错误

3. 模型返回错误

  • 确认模型名称拼写完全正确
  • 在控制台「模型广场」确认该模型在你的分组中可用

4. Windows 环境下配置文件路径

C:\Users\<用户名>\.codex\config.toml

5. 诊断命令

bash
# 检查配置是否正确
codex doctor

# 查看当前配置
codex config

玄州API — 企业级大模型 API 聚合平台