> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flexinference.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Codex

> 将 Codex 指向 FlexInference，并使用带有截止日期的密钥。

[Codex](https://developers.openai.com/codex/cli) 会构建自己的请求体，因此您无法在其中设置 `start_within`。截止日期会附加到密钥上。请先创建一个密钥（参见[代理密钥](/zh/agent-keys)）。

## 通过代理进行设置

打开下方代码块并将其复制到任何编码代理中。提示词绝不会询问您的 API 密钥：代理会配置所有其他内容，然后打印出您需要自行运行的导出行。

<Accordion title="复制代理设置提示">
  ```text theme={null}
  Configure the Codex CLI to send its requests to FlexInference. Do not change my existing Codex setup: create a separate profile I opt into per run.

  You will never see or handle my API key. The profile stores only the NAME of an environment variable, and I export the value myself. Do not ask me for the key, and do not read it from my environment.

  1. Create `~/.codex/flexinference.config.toml` with exactly this content:

  model = "gpt-5.6-sol"
  model_provider = "flexinference"

  [model_providers.flexinference]
  name = "FlexInference"
  base_url = "https://api.flexinference.com/v1"
  wire_api = "responses"
  env_key = "FLEX_API_KEY"

  2. Tell me to export the key myself, in the shell I will run Codex from:

     export FLEX_API_KEY=<paste your key here>

     Print that line for me to run. Do not run it, do not ask for the value, and do not add it to my shell profile.

  3. Tell me to start Codex with `codex --profile flexinference`.

  Rules that matter, do not deviate:
  - Keep the `model` line. Codex defaults to a slug ending in `-codex`, which FlexInference recognizes but holds no price for and never races. Use the full `gpt-5.6-sol`; the bare `gpt-5.6` alias makes Codex print a metadata warning.
  - Keep `wire_api = "responses"`. FlexInference serves that surface at `/v1/responses`.
  - Never add `service_tier`. FlexInference derives the tier from the key's deadline and returns `400 service_tier_not_allowed` if the request carries its own.
  - `env_key` and `auth` are mutually exclusive. Use `env_key` unless I ask for a keychain helper.
  - Do not set `request_max_retries`. My key may carry its own retry policy and the two stack.

  Verify by checking the file parses under `codex --profile flexinference --strict-config`, then report what you changed.
  ```
</Accordion>

## 配置 Codex

Codex 将每个配置文件保存在单独的文件中。此配置文件不会更改您通常的 Codex 设置，您可以通过 `--profile` 在每次运行时选择启用它。

1. 编写配置文件。粘贴以下内容一次：

```bash theme={null}
mkdir -p ~/.codex && cat > ~/.codex/flexinference.config.toml <<'EOF'
model = "gpt-5.6-sol"
model_provider = "flexinference"

[model_providers.flexinference]
name = "FlexInference"
base_url = "https://api.flexinference.com/v1"
wire_api = "responses"
env_key = "FLEX_API_KEY"
EOF
```

2. 导出 Codex 读取的密钥。配置文件中绝不包含密钥本身。

```bash theme={null}
export FLEX_API_KEY=flex_live_...
```

3. 使用配置文件启动 Codex。

```bash theme={null}
codex --profile flexinference
```

这就是整个集成过程。Codex 会调用 `/v1/responses`，将密钥作为 Bearer Token 发送，并且密钥会提供截止日期。会话头会指明正在使用的提供商，因此您可以看到配置文件已生效。

如果想在每次 Codex 运行时都连接到我们，请将相同的行放入 `~/.codex/config.toml` 中。其中的顶级 `model` 和 `model_provider` 会设置您的默认值。

`env_key` 和 `auth` 互斥。`env_key` 从您的环境中读取密钥，这是快捷方式。如果将密钥保存在钥匙串中，则可以省略 `env_key`，然后在 `auth` 中指定一个辅助命令。

保留 `model` 行。否则，Codex 默认使用以 `-codex` 结尾的 slug，我们对此不提供定价，也从不参与竞价。请使用完整的 `gpt-5.6-sol`，因为裸 `gpt-5.6` 别名会导致 Codex 打印元数据警告。

## 确认密钥已应用

每个响应都带有 `x-flexinference-defaults-applied` 字段。Codex 不显示响应头，因此请改为在仪表板的“**日志**”下查看请求。

## 故障排除

**完全没有请求到达我们。** 您运行的是普通的 `codex`，它使用了您常用的提供商和账户。请使用 `--profile flexinference` 启动它。您看到的任何错误（包括使用限制）都属于他们，并且会话头会指明正在使用的提供商。

**[`400 model_not_priced_for_managed`](/zh/errors#model_not_priced_for_managed)。** `model` 未设置，因此请求到达了一个我们不提供定价的 `-codex` slug。请在配置文件中设置 `model = "gpt-5.6-sol"`。如果您使用自己的提供商密钥，我们将运行该 slug，但没有我们的成本数据。

**[`400 service_tier_not_allowed`](/zh/errors#service_tier_not_allowed)。** 配置文件设置了 `service_tier`，而我们是从截止日期中读取层级的。请删除该行。

**[`400 key_default_not_applicable`](/zh/errors#key_default_not_applicable) 命名了一个固定路由。** 请求固定了一个云路由，例如 `foundry`，它没有便宜的层级可供竞价。请删除固定路由，或将密钥编辑为某个层级。

**每个请求都用尽了完整的重试预算。** Codex 有自己的 `request_max_retries`，并且密钥的 `retry` 策略会与其叠加。请设置其中一个，而不是两者都设置。

**光标停滞等待。** **Flex race** 成本较低且启动较晚，这在交互式循环中会很明显。请将密钥的截止日期编辑为 **Priority** 或 **Standard**。下一个请求将无需重启即可生效。

有关我们返回的所有拒绝信息，请参阅[错误](/zh/errors)；有关非 Codex 特有的问题，请参阅[代理密钥](/zh/agent-keys)。
