Skip to content
stacks

AGENTS.md example for Codex, with config

An AGENTS.md example for Codex records real project commands and rules; .codex/config.toml sets the sandbox and when approval is requested.

Published 7 min read

A useful AGENTS.md example for Codex starts with the commands that actually work in your repository and the rules an agent needs before editing. Put the file at the repository root. Codex reads it as project instructions before working.

You can write the file yourself or use Codex's /init command to draft it from the repository, then check its contents. Include a command for running one test file and explain what must pass before the agent reports completion. Put sandbox and approval settings in .codex/config.toml.

Where does Codex look for AGENTS.md?

Codex combines global instructions with files along the path to its working directory. Global guidance normally lives in ~/.codex/AGENTS.md. Project discovery starts at the repository root and continues down to the directory where you start Codex. Instructions nearer that directory take precedence when they conflict. OpenAI's AGENTS.md discovery guide documents the order.

An AGENTS.override.md takes priority over AGENTS.md in the same directory. Check for that file if edits to your regular instructions seem to have no effect. Keep shared project rules at the root and use nested files for rules that belong to a particular part of the repository.

Checking which files loaded is a useful first diagnostic. Start a fresh Codex session in the directory where you intend to work and ask it to list the instruction sources it read. Compare that answer with your directory layout before changing the wording of a rule that may never have reached the agent.

What should an AGENTS.md for Codex include?

Tested commands are the most useful content in the file. Copy them from your package scripts or Makefile and run each once yourself. Record the working directory if a command must run inside a workspace. A plausible command that invokes the wrong runner can waste an entire debugging session.

A single-test command gives Codex a short feedback loop. Separate that command from the complete checks you expect before handoff. For example, an agent fixing a formatter should be able to run its tests repeatedly without rebuilding the site after every edit. The final instructions should still require the checks appropriate to the finished change.

The layout section should explain boundaries an agent could otherwise miss. Describe where reusable logic belongs and which directories are generated. Link longer explanations from the root file, with an instruction to read them before editing the relevant area. A directory listing alone says little about what code may depend on what.

Rules work best when they describe observable behavior. Replace "write good tests" with "reproduce a bug in a failing test before fixing it." Ask the agent to report commands and results, including anything it could not run. That gives you evidence to review alongside the diff.

What does an AGENTS.md example for Codex look like?

This example assumes a small TypeScript library using npm and Vitest, with a committed package-lock.json. Its test script runs vitest run; the other script names must exist in package.json. The paths describe an illustrative repository. Replace them with your own paths and verify every command before adopting the file.

AGENTS.md
# text-tools

A TypeScript library for formatting text. npm manages dependencies.

## Commands

Run these from the repository root:

- Install: `npm ci`
- Lint: `npm run lint`
- Typecheck: `npm run typecheck`
- All tests: `npm test`
- One test file: `npm test -- src/format.test.ts`
- Build: `npm run build`

## Layout

- `src/`: library code, with tests beside the module they exercise.
- `src/index.ts`: public exports. Keep internal helpers private.
- `dist/`: generated build output. Change the source and rebuild.

## Working rules

- Read the relevant source and tests before editing.
- Keep changes within the requested scope and follow existing patterns.
- Ask before adding a production dependency or changing the public API.
- Never write credentials into tracked files.

## Testing and handoff

- Start bug fixes with a regression test and confirm it fails.
- Run the relevant test file while making the change.
- Before handoff, run lint, typecheck, all tests and the build.
- Fix the implementation when a check fails. Do not skip tests or lower limits.
- Report changed files and each command's result. Explain blocked checks.
- Leave changes uncommitted unless the user asks for a commit.

Codex's /init can provide a repository-specific first draft. Run codex in the repository, enter /init, then review the resulting instructions against the scripts you maintain. OpenAI describes that workflow in its guide to iterating development workflows. Generated instructions still need someone to check that the commands run and the rules reflect team decisions.

Hand-maintained instructions are enough for a repository with a small, stable workflow. Keep the file focused on decisions that affect the agent's work. When a setup command changes, update the instructions in the same change so the next session gets the working command.

How do I configure Codex's sandbox and approvals per project?

Project configuration belongs in .codex/config.toml. Personal defaults belong in ~/.codex/config.toml; Codex loads project configuration only for a trusted project. These two settings select the workspace-write sandbox and on-request approvals:

.codex/config.toml
sandbox_mode = "workspace-write"
approval_policy = "on-request"

The sandbox controls execution access, while the approval policy controls when Codex asks for permission. Consult OpenAI's configuration documentation for precedence and the linked sandbox details. Command-line overrides or managed requirements can affect the effective configuration, so inspect the active settings when behavior differs from the file.

Instructions express the workflow you want the agent to follow. A sentence asking it to avoid a directory does not configure filesystem restrictions. Review the execution settings separately, especially when copying an instruction file between repositories with different trust assumptions.

Can Codex run hooks before commands?

Codex supports PreToolUse hooks that can inspect a pending tool call and block it. Project hooks can live in .codex/hooks.json. OpenAI's hook documentation describes the event format and review process. You can maintain your own hook scripts if a particular command needs an automatic check.

Project hooks require trust before they run. Review the project and each hook through /hooks; having a hook file on disk does not mean its guards are active. Read the scripts a hook calls as part of that review, since their behavior determines what the check actually does.

Shell-command guards need tests of their own. Check a command that should be blocked and a similar one that should be allowed. Keep an instruction telling the agent to stop and ask when a guard blocks it. Otherwise a workaround can defeat the behavior you added the hook to enforce.

How do I set up a whole project for Codex?

stacks (@steamfield/stacks) writes starter files and agent configuration from a curated project recipe. It is useful when you also need a build brief and recorded verification steps. The CLI never calls an LLM: the coding agent fills in the project-specific instructions after reading the brief.

The scripted demo below shows a Go CLI project created with --agents codex. Its captured stacks 0.7.0 output includes the file listing and both configuration settings. Playback shows recorded commands; it runs nothing on your computer.

Agent + stacks
Demo

You: Start a Go CLI for Codex and show me its project config.

Agent: I’ll select the Codex adapter and inspect the generated files.

●Bash(stacks new go-cli codex-cli --agents codex --sandbox --no-launch)
Created codex-cli from go-cli (sandbox).
Fragments: agent-ops, ci-github, git-hooks
Agents: codex
Git: new repository, nothing committed
Brief: .stacks/BRIEF.md (3279 words)
Output abbreviated
●Bash(find codex-cli/AGENTS.md codex-cli/.codex codex-cli/.claude -type f | sort)
codex-cli/.claude/hooks/block-destructive.sh
codex-cli/.claude/hooks/detect-secrets.sh
codex-cli/.codex/config.toml
codex-cli/.codex/hooks.json
codex-cli/.codex/hooks/check-patch.sh
codex-cli/AGENTS.md
Output abbreviated
●Bash(cat codex-cli/.codex/config.toml)
# https://developers.openai.com/codex/config-advanced/
sandbox_mode = "workspace-write"
approval_policy = "on-request"

Agent: The config selects workspace-write and on-request approvals. The hook configuration wires Bash and apply_patch guards.

●Bash(cat codex-cli/.codex/hooks.json)
{
"hooks": {
"PreToolUse": [
"matcher": "^Bash$",
"matcher": "^apply_patch$",
Output abbreviated
●Bash(cat codex-cli/AGENTS.md)
# codex-cli
## Commands
## Architecture map
## Agent harness
## Guardrails
## Testing
- A bug fix starts with a test that reproduces the bug.
- Weakening or skipping a test to get green is a failure, not a fix.
Output abbreviated

Agent: AGENTS.md still needs the project’s commands and conventions. Review and trust the project hooks through /hooks before relying on them.

Example agent conversation. Playback can be paused.

A scripted session writes a Go CLI project for Codex and inspects its instruction file, sandbox settings and hook wiring.

The Codex target gets AGENTS.md and .codex/config.toml, plus .codex/hooks.json and its patch-check script. It also gets shell guards under .claude/hooks/, because the Codex hooks call those scripts. A Codex-only project does not receive CLAUDE.md; AGENTS.md vs CLAUDE.md explains how one file serves both agents when you add Claude Code later. The agent adapter documentation explains the files and the trust step.

The generated hooks check Bash commands for destructive operations and apply_patch writes for credential-shaped text. They refuse the operation if a required guard script is missing. These shell checks help catch accidents; indirect commands can get past their text matching. Linting and formatting remain instructions for Codex, so they still need explicit commands in AGENTS.md.

The instruction template still needs the project's real commands and conventions. The agent fills its stacks:fill markers, and the agents-md-filled check fails while any remain. The template includes regression-test guidance and tells the agent to stop after repeated failed fixes without a new diagnostic step. For projects shared with Claude Code, it also tells Codex to read applicable .claude/rules/ files before editing, since Codex does not load them automatically.

The manifest records the checks that stacks verify runs. With the project's local state record present, verify compares the step digest before execution and refuses changed steps until a person accepts them; acceptance is refused under a detected agent. A fresh clone has only the manifest's digest, so changing both the steps and that digest can pass. The digest also leaves test contents and the code those steps execute outside its protection. Review those changes in the diff.

The published package's ledger provides limited evidence for the Codex adapter. Of its 50 maintainer-run records, 43 used Codex 0.165.0. Across both agents, 49 passed and one failed. All used the sandbox preset, headless mode and default compositions, with no added fragments. Here, sandbox names a project preset; the bench runs had full local permissions. The records were made with earlier CLI versions, not 0.7.0. They do not measure user outcomes.

Codex can also access stacks through MCP using its user-level config.toml. The MCP setup instructions cover the server, which exposes tools to list entries, create a project and run verification. Use the CLI when you need search or to add fragments to an existing repository.