﻿# How Codex discovers AGENTS.md from the project directory

Understand the Codex AGENTS.md discovery chain, directory precedence and overrides, then verify which instructions a particular repository run actually loads.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-codex-instruction-discovery/
Language: en

AGENTS.md gives Codex project guidance before work begins. The important mechanism is directory discovery: Codex assembles a chain rather than selecting whichever file a developer happened to edit most recently. Understanding that chain makes instruction problems much easier to reproduce.

## Begin with global guidance

The official discovery guide describes a global scope in the Codex home directory. An override file is considered before the ordinary guidance file, with only the first nonempty candidate used at that level. This provides a way to replace a default intentionally. It also explains a common puzzle: changing the ordinary file may appear ineffective while an override still supplies the global instructions for the next run.

## Walk from the project root toward the working directory

For project guidance, Codex follows the path from the project root toward the current working directory. At each directory it considers the documented filename precedence and includes at most a single guidance file. More local guidance appears later in the assembled chain. Consequently, a run started in a service directory can receive a different effective context from a run started at the repository root.

## Test discovery with an observable convention

A hypothetical monorepo can define a general test command at the root and a service-specific command in a nested override. Start the agent in the service directory and ask which instruction sources were loaded. Then compare the reported chain with the intended layout. This is a proposed configuration check, not a claim that a particular repository has been tested. The relevant result is whether the intended local convention actually enters the run.

## Keep discovery and instruction quality distinct

A correctly loaded file can still contain vague or conflicting requirements. A missing file, by contrast, cannot guide the run at all. Diagnose these cases separately. First establish the chain and the selected files; then examine whether their combined text explains the intended behavior. This avoids repeatedly rewriting a rule that the current launch context never discovers, or treating an ambiguous requirement as a filesystem discovery defect.

## Rebuild the chain after an intentional change

The documentation describes instruction discovery at run startup. After changing the instruction layout, start a fresh run in the intended directory and repeat the discovery check. Keep the verification short: selected sources and the relevant working convention are enough. The resulting record is useful to another maintainer because it describes both the repository layout and the launch conditions needed to reproduce the same guidance.

## Documentation and further study

- [Custom instructions with AGENTS.md](https://learn.chatgpt.com/docs/agent-configuration/agents-md).

- [Codex as a platform: build on the open agent harness](https://developers.openai.com/blog/codex-as-a-platform).

- [Codex App Server](https://learn.chatgpt.com/docs/app-server).

Related engineering guides: [Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [Choose a local AI stack: desktop chat, shared UI or coding agent](/journal/local-ai-app-stack-selection/).
