编码工具

Codex CLI

配置 OpenAI Codex CLI 以使用 TokenLab API

交给我的 Agent 做

把这段任务交给电脑上已经可用的 Agent:

阅读此教程,帮我将 Codex CLI 接入 TokenLab:
https://tokenlab.sh/docs/zh/integrations/codex-cli
先检查已安装版本和实际配置。
保留已有账户、提供商、权限及无关设置。
备份本机文件并展示修改内容。
让我在本机输入所需 API Key,不要在聊天里索取、打印或粘贴密钥。
先验证配置能否加载;真实请求测试的费用须单独说明后再运行。

概述

Codex CLI 通过 /v1/responses 连接 TokenLab。所选模型的 tokenlab.accepted_request_formats 必须包含 openai_responses,具体功能可能因模型而异。

远程压缩使用 POST /v1/responses/compact。Codex 会把当前会话模型放在 body.model 中,因此这个模型也必须支持 Responses。不要配置 /v1/compact。

系统要求

macOS、Linux 或 Windows 请按官方 CLI 安装指南选择安装方式。原生 Windows 与 WSL 是不同环境,参见 Windows 指南。下方 npm 命令是一种安装方式,不是原生二进制的前置要求。

安装

npm install -g @openai/codex@0.149.0

验证安装:

codex --version

配置

第 1 步:设置 API 密钥

按快速开始中 macOS、Linux、WSL 或 Windows PowerShell 的终端步骤设置 TOKENLAB_API_KEY,避免把密钥写进命令历史,然后从同一终端启动 Codex。长期使用请通过本地密钥管理器提供密钥,不要把明文密钥追加到 shell 配置文件。

第 2 步:配置 tokenlab.config.toml

在 Codex 0.149.0 的当前 CODEX_HOME(通常为 ~/.codex)中新建独立的 tokenlab.config.toml。仅在名称未被占用时创建;修改已有 TokenLab profile 前先备份。使用 codex --profile tokenlab 显式加载,保留默认 config.toml、auth.json、账户及权限策略。不要使用旧式 [profiles.tokenlab] 表。正常启动时不带该 profile 即可回到原设置。参见官方 profile 配置。

model_provider = "tokenlab"
model = "gpt-5.6-sol"
model_reasoning_effort = "xhigh"

[model_providers.tokenlab]
env_key = "TOKENLAB_API_KEY"
name = "TokenLab"
base_url = "https://api.tokenlab.sh/v1"
wire_api = "responses"
supports_websockets = true
websocket_connect_timeout_ms = 15000

此 WebSocket 模式是面向 Codex 客户端的 Responses-over-WebSocket 桥接层。它只接受官方 response.create 事件;stream 是隐含行为,该 transport 不提供 background 或 response.cancel。它不是 OpenAI Realtime API,也不接受 session.update、conversation.item.*、input_audio_buffer.*、二进制音频或嵌套的 Realtime response.create.response 信封。

如果所选配置文件不存在,请创建该文件。修改后,使用下方对应命令启动新会话。

当前 Codex 使用 wire_api = "responses",不要配置 chat/completions。此自定义提供商的 requires_openai_auth 应省略或为 false,以便使用 env_key。

Responses 请求不会转换为 Chat Completions。可选及未来字段的支持取决于所选模型。

基本用法

启动交互模式:

codex --profile tokenlab

直接命令:

codex --profile tokenlab "Fix the bug in main.py line 42"

指定模型:

codex --profile tokenlab -m gpt-5.6-sol "Build a REST API server"

选择模型

选择当前 tokenlab.accepted_request_formats 包含 openai_responses 的模型。请用自己的编程任务比较候选模型,当前价格以模型页为准。

交互命令

Command说明
/help显示帮助
/exit 或 Ctrl+C退出
/clear清除会话
/status查看当前模型、权限和 token 用量
/model <name>切换模型

验证配置

只检查环境变量是否已设置,不输出其值。这个检查不能判断密钥是否有效:

if [ -n "${TOKENLAB_API_KEY:-}" ]; then
  printf '%s\n' 'TOKENLAB_API_KEY is set'
else
  printf '%s\n' 'TOKENLAB_API_KEY is not set'
fi

启动 Codex 后运行 /status,核对当前模型和权限。如果使用 profile,请启动同一个 profile。参见官方状态命令说明。

准备好发起真实请求后,再运行下面的命令;它会使用 TokenLab 余额。核对回答和请求记录中的对应记录。环境检查通过或客户端成功启动,都不等于请求已经成功。

codex --profile tokenlab "Reply only with OK. Do not use tools or modify files."

常见用例

代码审查:

git diff | codex --profile tokenlab "Review these code changes"

生成提交信息:

git diff --staged | codex --profile tokenlab "Generate a commit message for these changes"

修复错误:

codex --profile tokenlab "Fix the TypeScript errors in src/components/"

解释代码:

cat main.py | codex --profile tokenlab "Explain what this code does"

故障排除

本页内容