配置 (Config)

Codex 支持多种方式来设置配置值:

  • 特定的命令行参数,例如 --model o3(优先级最高)。

  • 通用的 -c/--config 参数,接受 key=value 对,例如 --config model="o3"

    • key 可以包含点号,用于设置嵌套的值,例如:--config model_providers.openai.wire_api="chat"
    • 为了与 config.toml 一致,值是 TOML 格式,而不是 JSON。例如应写作 key='{a = 1, b = 2}',而不是 key='{"a": 1, "b": 2}'

      • 外层引号是必须的,否则 shell 会在空格处分割参数,导致 Codex 收到错误的值。
    • 值可以是任意 TOML 对象,例如:

      1. --config shell_environment_policy.include_only='["PATH", "HOME", "USER"]'
    • 如果 value 不是合法 TOML,会被当作字符串。例如:

      • -c model='"o3"'-c model=o3 等效。
      • -c key="true" 会被解析为布尔值 true,如果需要字符串 "true",则写成 -c key='"true"'
  • $CODEX_HOME/config.toml 配置文件,其中 CODEX_HOME 默认路径是 ~/.codex。 (注意:CODEX_HOME 也是 Codex 存储日志和运行时数据的目录。)


示例配置 (Examples)

TOML 配置文件

  1. model = "o3"
  2. temperature = 0.2
  3. [model_providers.openai]
  4. api_key = "sk-xxxxxx"
  5. wire_api = "chat"

JSON 配置(通过 —config 传入)

  1. codex --config model='"o3"' \
  2. --config temperature=0.2 \
  3. --config model_providers.openai.api_key='"sk-xxxxxx"' \
  4. --config model_providers.openai.wire_api='"chat"'

Python 脚本动态配置

  1. import os
  2. import json
  3. # 设置环境变量
  4. os.environ["CODEX_HOME"] = "/home/user/.codex"
  5. # 动态生成 JSON 配置
  6. config = {
  7. "model": "o3",
  8. "temperature": 0.2,
  9. "model_providers": {
  10. "openai": {
  11. "api_key": "sk-xxxxxx",
  12. "wire_api": "chat"
  13. }
  14. }
  15. }
  16. print(json.dumps(config, indent=2))

日志 (Logging)

Codex 会将日志写入 $CODEX_HOME/logs/。 你可以通过设置 RUST_LOG 环境变量来调整日志级别,例如:

  1. RUST_LOG=debug codex

模型提供者 (Model Providers)

Codex 支持多个模型提供者。每个 provider 都可以在 config.toml 里配置。

示例:

  1. [model_providers.openai]
  2. api_key = "sk-xxxxxx"
  3. wire_api = "chat"

你也可以配置多个 provider 并切换使用:

  1. [model_providers.anthropic]
  2. api_key = "sk-yyyyyy"
  3. [model_providers.openai]
  4. api_key = "sk-xxxxxx"

Shell 策略 (Shell Policies)

Codex 可以执行 shell 命令。你可以通过策略限制环境变量或命令。

示例:只允许传递部分环境变量:

  1. [shell_environment_policy]
  2. include_only = ["PATH", "HOME", "USER"]

示例:阻止危险命令:

  1. [shell_command_policy]
  2. deny = ["rm -rf /", "shutdown", "reboot"]

代理 (Proxies)

Codex 可以使用代理。示例配置:

  1. [proxy]
  2. http = "http://localhost:7890"
  3. https = "http://localhost:7890"

缓存 (Cache)

Codex 会缓存模型的响应。你可以在配置文件里启用/禁用:

  1. [cache]
  2. enabled = true
  3. dir = "~/.codex/cache"

安全 (Security)

你可以启用安全模式,禁止某些高风险操作:

  1. [security]
  2. sandbox_enabled = true

API 服务器 (API Server)

Codex 也可以作为 API 服务器运行:

  1. [server]
  2. enabled = true
  3. host = "127.0.0.1"
  4. port = 8080

启动:

  1. codex server