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

# Codex

> Codex 配置 AutoDL 大模型接口

// todo检查是否需要终端交互配置

Codex 是 OpenAI 提供的命令行编程助手。通过配置 AutoDL 的第三方 Token，你可以在本地终端直接使用 Codex。

## 前置准备

1. 安装 Node.js 和 npm。
2. 安装 Codex：

```Shell theme={null}
npm install -g @openai/codex
```

3. 前往「大模型」->「[令牌管理](https://www.autodl.art/large-model/tokens)」创建并复制 `API_KEY`。

> [!NOTE]
>
> 下方命令中的 `<你的API_KEY>` 需要替换为在 AutoDL 令牌管理页面获取的 Token。

## CC Switch

推荐使用 [CC Switch](https://docs.autodl.art/model/tool/ccswitch.md) 统一管理 Codex、Claude Code 等工具的环境变量。

## Windows 配置

### 临时配置

在 PowerShell 中执行以下命令，本次终端会话立即生效：

```PowerShell theme={null}
$env:OPENAI_BASE_URL="https://www.autodl.art/api/v1"
$env:OPENAI_API_KEY="<你的API_KEY>"
```

### 永久配置

如果希望后续打开 PowerShell 时自动生效，可以执行：

```PowerShell theme={null}
setx OPENAI_BASE_URL "https://www.autodl.art/api/v1"
setx OPENAI_API_KEY "<你的API_KEY>"
```

执行完成后，关闭当前终端并重新打开，再运行：

```PowerShell theme={null}
codex
```

> [!TIP]
>
> Windows 用户也可以在 WSL 或 Git Bash 中按照 Linux 的方式配置。

## Linux 配置

### 临时配置

在终端中执行以下命令，本次终端会话立即生效：

```Shell theme={null}
export OPENAI_BASE_URL="https://www.autodl.art/api/v1"
export OPENAI_API_KEY="<你的API_KEY>"
```

### 永久配置

如果使用 Bash，可以写入 `~/.bashrc`：

```Shell theme={null}
echo 'export OPENAI_BASE_URL="https://www.autodl.art/api/v1"' >> ~/.bashrc
echo 'export OPENAI_API_KEY="<你的API_KEY>"' >> ~/.bashrc
source ~/.bashrc
```

如果使用 Zsh，将 `~/.bashrc` 替换为 `~/.zshrc`。

## macOS 配置

### 临时配置

在终端中执行以下命令，本次终端会话立即生效：

```Shell theme={null}
export OPENAI_BASE_URL="https://www.autodl.art/api/v1"
export OPENAI_API_KEY="<你的API_KEY>"
```

### 永久配置

macOS 默认使用 Zsh，可以写入 `~/.zshrc`：

```Shell theme={null}
echo 'export OPENAI_BASE_URL="https://www.autodl.art/api/v1"' >> ~/.zshrc
echo 'export OPENAI_API_KEY="<你的API_KEY>"' >> ~/.zshrc
source ~/.zshrc
```

如果你使用 Bash，将 `~/.zshrc` 替换为 `~/.bash_profile` 或 `~/.bashrc`。

## 启动 Codex

完成配置后，在任意项目目录运行：

```Shell theme={null}
codex
```

如果 Codex 正常启动，并且能够读取 AutoDL 的接口地址和令牌，说明配置成功。

可以发送一句“您好”测试，并观察[数据看板](https://www.autodl.art/large-model/dashboard)消耗。

## 更多资源

高级配置：如果你需要调整模型参数、超时时间或日志设置，请参考官方文档：[Codex 高级配置指南](https://learn.chatgpt.com/docs/config-file/config-advanced)

返回/取消：在输入 Key 的界面，可以按 Esc 键返回主菜单。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.