# OpenAI Codex

Codex 是 OpenAI 推出的编程代理，可帮助你更快地编写、审查并交付代码。你可以在 IDE 中与 Codex 并行使用，或将更大规模的任务交由云端执行。

你可以查看 [**帮助文档**](https://developers.openai.com/codex/ide) 了解更多详情。

## 安装 Codex

你可以通过以下任一方式安装 Codex。

**方式一：通过 Homebrew 安装**

```bash
brew install codex
```

**方式二：通过 npm 安装**

```bash
npm install -g @openai/codex
```

## 配置 Codex

Codex 使用 TOML 格式的本地配置文件来读取设置。

**步骤 1：打开配置文件**

```bash
vim ~/.codex/config.toml
```

**步骤 2：定义默认模型和提供方**

```toml
# Codex 使用的默认模型
model = "o3"

# 默认模型提供方
model_provider = "openai-chat-completions"
```

**步骤 3：配置模型提供方**

```toml
[model_providers.openai-chat-completions]
# 在 Codex 界面中显示的名称
name = "gateway.theturbo"

# OpenAI 兼容 API 的基础 URL
base_url = "https://gateway.theturbo.ai"

# 存储 API Key 的环境变量名称
env_key = "YOUR_AI_GATEWAY_API_KEY"

# API 协议类型（可选，默认为 "chat"）
wire_api = "chat"
```

## 设置 API Key

将你的 API Key 设置为环境变量：

```bash
export YOUR_AI_GATEWAY_API_KEY="sk-xxxxxxxxxxxxxx"
```

该 API Key 会在创建 AI 网关实例时生成。

## 使用 Codex

完成配置后，你即可立即开始使用 Codex。

**启动交互式会话**

```bash
codex
```

**发送一次性请求**

```bash
codex "Summarize the purpose of this repository."
```

所有请求都会通过 AI Gateway 进行转发，从而实现集中式的身份认证、请求路由和使用情况统计。

{% hint style="info" %} <mark style="color:blue;">**注意**</mark>

* <mark style="color:blue;">Codex 需要使用</mark> <mark style="color:blue;">**OpenAI 兼容 API**</mark><mark style="color:blue;">，AI Gateway 提供了该接口。</mark>
* <mark style="color:blue;">API Key 应始终以环境变量的方式存储，而不应硬编码在配置文件中。</mark>
* <mark style="color:blue;">你可以随时更改默认模型或提供方，而无需重新安装 Codex。</mark>
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/welcome/cn/ai-gateway/ai-gateway-integration/openai-codex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
