跳转到内容
搜索文档

OpenAI Codex

最后更新 查看 MarkdownAgent 设置

OpenAI Codex ↗ 是你在终端中运行的编码智能体。它支持在 config.toml 中定义的自定义模型提供商 ↗。此配置添加一个指向 AI Gateway OpenAI 端点 的提供商,使 Codex 通过 AI Gateway 发送请求。AI Gateway 通过 Unified Billing 为你对模型提供商进行身份验证,因此你传递的是 Cloudflare API 令牌,而不是 OpenAI API 密钥。

前提条件

开始之前,你需要:

  1. 在 ~/.codex/cloudflare-aig.config.toml 创建 Codex profile ↗ 文件。该 profile 定义一个指向 gateway 的 OpenAI 端点的自定义模型提供商,并从环境变量读取你的 Cloudflare API 令牌。

    将 <ACCOUNT_ID> 和 <GATEWAY_ID> 替换为你的值。可以将 gateway 设为 default,以通过账户的默认 gateway 路由;也可以改为其他 gateway slug。

    ~/.codex/cloudflare-aig.config.tomltoml
    model_provider = "cloudflare-ai-gateway"
    model = "gpt-5.5"
    model_reasoning_effort = "medium"
    
    [model_providers.cloudflare-ai-gateway]
    name = "Cloudflare AI Gateway"
    # Run `wrangler whoami` to get your account ID, then replace <ACCOUNT_ID>.
    # Use `default` for <GATEWAY_ID> to route through your account's default gateway.
    base_url = "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/openai"
    env_key = "CLOUDFLARE_API_KEY"
    wire_api = "responses"
  2. 将 Cloudflare API 令牌设为 CLOUDFLARE_API_KEY 环境变量。以下命令为当前会话设置该变量。若要持久化,请将其添加到 shell 配置文件(例如 ~/.zshrc 或 ~/.bashrc)。

    将 <CLOUDFLARE_API_KEY> 替换为你的值。

    # Run `wrangler auth token` to get an auth token.
    export CLOUDFLARE_API_KEY="<CLOUDFLARE_API_KEY>"
    # Run `wrangler auth token` to get an auth token.
    $env:CLOUDFLARE_API_KEY = "<CLOUDFLARE_API_KEY>"
  3. 使用该 profile 启动 Codex 并发送提示。请求现在会通过 AI Gateway 路由。

    codex --profile cloudflare-aig

要确认流量已到达 AI Gateway,请参阅验证是否生效。

这篇文档对您有帮助吗?