Advanced Codex Usage Tips
After completing the basic installation and your first conversation, Codex's real value lies in embedding it into your daily development workflow.
This article covers five advanced areas: project configuration, session management, editor integration, CI/CD automation, and prompting techniques, helping you make Codex understand your project, remember your conventions, and work automatically in the pipeline.
1. Writing an "onboarding document" for Codex with AGENTS.md
AGENTS.md is a project description file for Codex, placed in the project root directory, automatically loaded at each startup, and effective throughout the entire session.
Why is it needed
By default, Codex knows nothing about your project.
It doesn't know whether you use App Router or Pages Router, which file database operations should consistently go through, or which files must not be touched.
If you have to re-explain the background in every conversation, efficiency will be very low and errors are likely.
AGENTS.md lets you write this information once and have it take effect continuously, saving the trouble of repeated explanations.
What to write
An effective AGENTS.md typically contains four types of information: project overview, tech stack, important conventions, and prohibitions.
The following is a complete example:
# AGENTS.md
## Project Overview
这是一个基于 Next.js 14 + Prisma + PostgreSQL 的 SaaS 应用。
使用 App Router,不使用 Pages Router。
## 技术栈
- 前端:Next.js 14, React 18, TailwindCSS, shadcn/ui
- 后端:Next.js API Routes, Prisma ORM
- 数据库:PostgreSQL 15
- 认证:NextAuth.js
## Important Conventions
- 所有数据库操作必须通过 lib/db.ts 中的 prisma 实例
- API 路由错误统一用 lib/api-error.ts 处理
- 环境变量在 .env.local 中,参考 .env.example
## Prohibited Items
- 不要修改 prisma/schema.prisma,除非我明确要求
- 不要删除任何现有测试
- 生产环境的 .env 文件不要碰
The "Prohibitions" section is especially important. When executing tasks, Codex actively infers which files need to be modified; without clear boundaries, it may touch files you don't want it to. Clearly defining the red lines is far less troublesome than fixing problems afterward.
Configuration hierarchy
AGENTS.md supports three levels of nesting, with priority arranged from low to high.
Files closer to the current directory have higher priority. In the same directory, if AGENTS.override.md exists, the AGENTS.md at the same level will be skipped.
| Level | Path | Scope | Precedence |
|---|---|---|---|
| Global level | ~/.codex/AGENTS.md | Cross-project common conventions | Low |
| Project level | repo/AGENTS.md | Repository-level specifications | Medium |
| Override level | repo/services/payments/AGENTS.override.md | Subdirectory-specific rules | High |
The global level is suitable for conventions that hold true in all projects, for example:
# ~/.codex/AGENTS.md
## Global Conventions
- 安装依赖时优先使用 pnpm
- 修改 JavaScript 文件后始终运行 npm test
- 新增生产依赖前先请求确认
After configuration, you can use the following command to verify that loading is correct:
codex --ask-for-approval never "Summarize the current instructions."
2. Session management: continuing large tasks across days
The session management feature allows you to export the current conversation state to a file and restore it directly next time, without re-laying the background.
Problem background
Codex's context window is limited.
When handling a large task that spans multiple files and needs to be advanced in stages, if you close the terminal midway or switch to something else, the context is broken when you return — Codex doesn't remember what was discussed or which decisions were made.
Exporting a session can solve this problem, allowing you to restore the previous conversation state at any point in time.
Basic Usage
The following are common commands related to session management:
Example
Export the current session at any time during the conversation
/
export
session-
2024
-01-
15
.json
Restore when continuing next time
/
load session-
2024
-01-
15
.json
Directly restore the most recent session (most commonly used)
codex resume
--last
View all saved sessions
ls
~
/
.codex
/
sessions
/
When to export
You don't need to export every conversation. The following situations are worth saving:
| Scenario | Description |
|---|---|
| Multi-stage tasks spanning multiple days | Tasks carried out over multiple days with clear phase divisions can be saved so you can continue directly from the previous breakpoint next time |
| Important architectural decisions | You've discussed an important architectural decision with Codex, and subsequent tasks need to proceed based on that decision |
| Complex debugging processes | Debugging a complex bug where you've ruled out several directions and don't want to start from scratch next time |
3. Integration with VS Code
Codex officially provides a VS Code extension; after installation you can start conversations directly within the editor without switching to the terminal.
Installation and login
The installation steps are as follows:
-
Step 1: Open the Extensions marketplace (Cmd/Ctrl + Shift + X).
-
Step 2: Search for "Codex" or "OpenAI Codex" and install the official plugin.

-
Step 3: On first use, you need to log in with a ChatGPT account.
-
Step 4: Once the Codex icon appears in the sidebar, you can use it.
After installation, you can use the following shortcuts for quick operations:
| Keyboard shortcut | Function |
|---|---|
| Alt + G | Send selected code to Codex with the current file context |
| Cmd + Shift + P | Open the command palette and type "Codex" to see all available commands |
BYO mode (for users without a ChatGPT subscription)
If you have already configured your own API Key in the CLI (Anthropic, OpenAI, or other compatible providers), you can log into the VS Code plugin with a free ChatGPT account.
The plugin automatically reuses the CLI's model configuration and does not force the use of ChatGPT Plus.
| Applicable scenarios | Description |
|---|---|
| Already have an API Key but don't want an extra ChatGPT subscription | Use your own API Key directly, no extra paid subscription needed |
| Want VS Code and CLI to use consistent models | The plugin automatically reuses the CLI configuration, so the experience is completely consistent on both ends |
4. CI/CD Integration
Codex CLI supports running in headless mode without human interaction, making it suitable for integration into automated workflows.
What Codex can do in a pipeline
The following are common uses of Codex in CI/CD under headless mode:
| Purpose | Description | Typical trigger timing |
|---|---|---|
| Automatically update CHANGELOG | Automatically update CHANGELOG.md based on commit records after each merge into the main branch | After merging into the main branch |
| Generate API documentation | Automatically generate or synchronize API documentation based on code changes | After code is pushed |
| Automatic Code Review | Automatically run code review in the PR pipeline and leave comments | When a PR is created or updated |
GitHub Actions Example
The following workflow automatically lets Codex update CHANGELOG.md based on the latest commit each time a push is made to the main branch:
Example
File path: .github/workflows/codex-changelog.yml
name
:
Auto Update Changelog
on
:
push
:
branches
:
[
main
]
jobs
:
update-changelog
:
runs-on
:
ubuntu-latest
steps
:
- uses
:
actions/checkout@v4
- name
:
Setup Node.js
uses
:
actions/setup-node@v4
with
:
node-version
:
'
22
'
- name
:
Install Codex CLI
run
:
npm install -g @openai/codex
- name
:
Run Codex Task
env
:
OPENAI_API_KEY
:
$
{
{
secrets.OPENAI_API_KEY
}
}
CODEX_QUIET_MODE
:
1
run
: |
codex exec --full-auto "Update CHANGELOG.md based on the latest commits"
- name
:
Commit changes
run
: |
git config --local user.email 'action@github.com'
git add CHANGELOG.md
git commit -m 'chore: update changelog [skip ci]'
git push
There are two key points in the configuration that need special attention:
| Configuration Item | Purpose | Description |
|---|---|---|
| CODEX_QUIET_MODE: 1 | Suppress interactive output | Avoid the pipeline getting stuck waiting for interactive input |
| --full-auto | Run in headless mode | Let Codex execute directly without waiting for confirmation in unattended situations |
When using in CI, it is recommended to explicitly limit the range of files Codex is allowed to modify in automatic mode in the AGENTS.md at the project root, to prevent unexpected changes caused by misinterpretation of prompts.
5. Prompting Techniques
The quality of Codex's output largely depends on how you ask.
The following five techniques target the problems new users most commonly encounter, each with a concrete example.
Technique 1: Provide enough context
Codex cannot read minds. An instruction like "fix the bug" makes it guess blindly, and the result is often not what you want.
The following is a comparison example:
| Not recommended | Recommended approach |
|---|---|
| "Fix the bug" | "User login throws TypeError: Cannot read properties of null, the error occurs at line 42 of src/auth/login.ts, this function is responsible for validating the JWT token, help me find and fix this issue" |
Effective context contains three elements:
| Element | Description | Example |
|---|---|---|
| Specific symptom | Error message or specific manifestation | TypeError: Cannot read properties of null |
| Location involved | File and line number | src/auth/login.ts line 42 |
| Code responsibility | The original function of this code | Validate the JWT token |
Technique 2: Handle complex tasks in two steps
For tasks with a large scope of changes, first let Codex analyze and list a plan, then execute after confirming there are no issues.
Doing it all at once may seem faster, but the cost is greater when the direction deviates.
Example
Step 1: Analyze only, do not modify
codex
"Analyze the code quality of the src/api/ directory, list the main problems, do not modify any files"
Step 2: Execute after confirming the plan
codex
"OK, following the plan you described, first handle the error handling issues; I'll review the rest and get back to you"
Technique 3: Use ask mode to first understand the codebase
ask mode is read-only, suitable for understanding the structure and logic of existing code before making changes.
Understanding before changing saves more time than understanding after changing.
Example
# ask 模式不会触发任何文件修改
codex -a ask "这个项目是如何处理用户认证的?梳理完整的认证流程"
After understanding clearly, exit the current session and restart in editable mode:
# Editable mode (requires confirmation)
codex -a auto
# Fully automatic execution (use with caution)
codex -a full-auto
Recommended workflow:
ask
↓
理解代码结构
↓
确定修改方案
↓
重新启动 Codex
↓
进入可编辑模式
↓
执行修改
Note: The CLI no longer supports using
/approvalsto switch permission modes during runtime; you need to specify it at startup via the-aparameter. The desktop client still supports switching within a session.
Technique 4: Use negative instructions to define boundaries
Telling Codex what not to touch is as important as telling it what to do.
Especially for refactoring tasks involving multiple related files, unclear boundaries easily lead to unnecessary collateral changes.
Example
Clearly define the scope that must not be modified
codex
"Refactor the date formatting function in utils/date.ts, do not change the function signature, do not change the test files"
Technique 5: Ask for a plan first, then start executing
For tasks you are not sure about, first let Codex list what it plans to do.
This step takes almost no time, but lets you detect direction deviations early and avoid going too far down the wrong path.
# Plan first
codex "你打算怎么实现这个功能?先列出步骤,不要执行"
# Confirm the plan is reasonable before proceeding
codex "计划没问题,开始执行第一步"
Summary
These five areas cover the complete path from "project configuration" to "daily operations" to "automation".
You don't need to use them all at the beginning; it is recommended to go in order: first write a good AGENTS.md to let Codex understand your project, then gradually integrate it into your VS Code workflow and CI/CD pipeline.
Prompting techniques accumulate gradually in daily use, which is more effective than memorizing them all at once.