Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex AGENTS.md

Codex AGENTS.md

AGENTS.md is Codex's custom instruction file, allowing you to set global guidance and workflow specifications for Codex.

This section details how to create and use AGENTS.md.


What is AGENTS.md?

Codex automatically reads at startup AGENTS.md files, and through a hierarchical mechanism of 'global configuration + project-level overrides', every task inherits consistent working conventions, no matter which code repository you open.

AGENTS.md is the instruction file that Codex reads before performing any work, allowing you to:

Through AGENTS.md, you can ensure Codex follows consistent standards throughout the codebase.


How Codex Discovers Guidelines

Codex builds the instruction chain according to the following priorities.

Three-tier loading mechanism

Level Load rules Description
Global level Read ~/.codex/AGENTS.override.md If it does not exist, read ~/.codex/AGENTS.md, taking only the first non-empty file
Project level Scan level by level from the Git root directory toward the current directory Each level checks in turn AGENTS.override.md → AGENTS.md → project_doc_fallback_filenames for alternative file names among
Merge layer Concatenate in order from root to leaf Files closer to the current directory appear later and have higher priority

The default upper limit for the merged total size is 32 KiB (controlled by project_doc_max_bytes ), and the excess will be truncated.


Step 1: Create the Global Configuration

Global configuration applies to all projects and is suitable for general working conventions.

Create the configuration directory



mkdir -p ~/.codex

Ensure the Codex home directory exists; all global configuration is placed in this directory.

Write global conventions

creation ~/.codex/AGENTS.md file, writing conventions common to all projects:



# ~/.codex/AGENTS.md



## Global Working Agreement



- 修改 JavaScript 文件后,始终运行 npm test

- 安装依赖时优先使用 pnpm

- 新增生产依赖前先请求确认

Verify the configuration takes effect

Run the following command, and Codex should echo the entries you wrote:



codex --ask-for-approval never "Summarize the current instructions."

If you need a temporary global override, you can create AGENTS.override.md, and deleting that file restores the original configuration.


Step 2: Configure by Repository Layer

Project-level files make Codex follow repository-specific standards while still inheriting the global configuration.

Add repository-level conventions

Create in the project root directory AGENTS.md:



# AGENTS.md



## Repository Conventions



- 提 PR 前运行 npm run lint

- 修改公共工具行为时,同步更新 docs/ 中的文档

Add overrides for subdirectories

For subdirectories with special needs (such as services/payments/), create AGENTS.override.md for a local override:



# services/payments/AGENTS.override.md



## Payment Service Rules



- 使用 make test-payments 替代 npm test

- 轮换 API Key 前必须通知安全频道

Complete directory structure

The following is the directory layout of a typical repository after loading AGENTS.md :



repo/

├── AGENTS.md                          # 仓库级约定(加载)

└── services/

    ├── payments/

    │   ├── AGENTS.md                  # 被跳过(override 优先)

    │   └── AGENTS.override.md         # 支付服务覆写(加载)

    └── search/

        └── AGENTS.md                  # 搜索服务约定(加载)

In the same directory,AGENTS.override.md when present, the same-level AGENTS.md will be skipped.

Verify the loading order

Launch Codex from a subdirectory and view the loaded instructions Source:



codex --cd services/payments --ask-for-approval never \

  "List the instruction sources you loaded."

Expected result: the global file, the repository root file, and the payment service override file are listed in order.


Advanced Options

The following two features apply to scenarios that require further customization of instruction loading behavior.

Custom file names

If the project already has an established TEAM_GUIDE.md, you can append it to the fallback list in the configuration file, and Codex will treat it as an instruction file:



# ~/.codex/config.toml

# Add fallback filename and increase merge limit

project_doc_fallback_filenames = ["TEAM_GUIDE.md", ".agents.md"]

project_doc_max_bytes = 65536

After modification, the check order for each directory becomes:AGENTS.override.md → AGENTS.md → TEAM_GUIDE.md → .agents.md, taking the first existing file.

After modifying the configuration, you need to restart Codex for config.toml changes to take effect.

Switching the Configuration Directory

Pass CODEX_HOME environment variable, you can use a separate configuration profile for different projects or automation flows:



CODEX_HOME=$(pwd)/.codex codex exec "List active instruction sources"

In this case, Codex will read the global configuration from the .codex subdirectory of the current directory, rather than the default ~/.codex。


Verification and Debugging

The following commands can quickly check the sources of the instructions currently in effect.

Quick check



# Check global + project commands from repository root directory

codex --ask-for-approval never "Summarize the current instructions."



# Check nested overwrite from subdirectory

codex --cd subdir --ask-for-approval never "Show which instruction files are active."

Enable detailed logs

If you need to audit the instruction loading process in detail, you can enable TUI logging:



# Output TUI logs to local directory

codex -c log_dir=./.codex-log

# Log file is located at ./.codex-log/codex-tui.log


Troubleshooting common problems

The following table summarizes problems that may be encountered during configuration and their troubleshooting directions.

Symptom Troubleshooting direction
Nothing is loaded Confirm the file is non-empty; run codex status Check whether the workspace root directory is correct
Wrong instructions appear Check whether there are leftover AGENTS.override.md
Fallback file names do not take effect Confirm config.toml is spelled correctly, and restart Codex to make it take effect
Instruction content is truncated Increase the project_doc_max_bytes, or split large files into multiple directory levels
Profile confusion Run echo $CODEX_HOME, confirm it points to the expected directory