Skip to content

AGENTS.md and Skills Mastery

Your Codex sessions keep going off track. The agent uses console.log instead of your structured logger. It writes tests with Jest when your project uses Vitest. It ignores your error handling patterns. Every prompt, you re-explain the same constraints. This is exactly the problem AGENTS.md and skills solve — but only when they are structured well. A poorly written AGENTS.md wastes context tokens on guidance the agent ignores, while a well-structured one makes every session start with the right constraints.

  • An AGENTS.md hierarchy that scales from personal preferences to team-wide conventions
  • The override pattern for temporary project phases
  • Skills creation and organization patterns
  • Context budget management so your guidance does not crowd out conversation space
  • Debugging techniques for when instructions seem to be ignored

Codex builds its instruction chain at session start by scanning files in this order:

  1. Global scope: ~/.codex/AGENTS.override.md (if it exists), otherwise ~/.codex/AGENTS.md
  2. Project scope: Starting at the project root (Git root), walking down to your current working directory. At each level, Codex checks AGENTS.override.md, then AGENTS.md, then fallback filenames
  3. Merge order: Files are concatenated root-to-leaf. Later files override earlier ones because they appear last in the combined prompt

Codex includes at most one file per directory and stops when the combined size exceeds project_doc_max_bytes (32 KB by default).

~/.codex/AGENTS.md
## Working Agreements
- Always run tests after modifying source files
- Prefer pnpm when installing dependencies
- Ask before adding new production dependencies
- Use TypeScript strict mode for all new files

Keep this under 20 lines. These are universal preferences that apply to every repository you work in.

repo-root/AGENTS.md
## Repository Rules
- Use Vitest for all tests (not Jest)
- Follow error handling patterns in src/lib/errors.ts
- Run pnpm lint && pnpm test before committing
- New API endpoints need OpenAPI annotations
- Database changes need a migration file in migrations/

Keep this under 50 lines. Focus on project-specific conventions that differ from your global defaults.

services/payments/AGENTS.md
## Payments Service Rules
- Use the centralized error handler, never throw raw errors
- Rate limiting must be added to all public endpoints
- Authentication middleware is in src/middleware/auth.ts
- Test utilities are in tests/utils/api-helpers.ts
- Never rotate API keys without notifying the security channel

Keep this under 30 lines. Only include rules that apply specifically to this module.

Use AGENTS.override.md for temporary guidance that should not permanently live in your repo:

services/api/AGENTS.override.md
# TEMPORARY: Remove after the v3 migration is complete (target: March 2026)
- All new endpoints must use the v3 router in src/routes/v3/
- Do NOT modify any v1 or v2 routes
- Migration tracking doc: docs/v3-migration.md
- Use the new AuthContext from src/lib/auth-v3.ts for all new middleware

When the override file exists, Codex uses it instead of the regular AGENTS.md at that directory level. Delete the override to restore the permanent guidance.

This pattern is invaluable during:

  • Large migrations where temporary rules apply
  • Feature flag rollouts that change coding patterns
  • Sprint-specific focus areas

Every token spent on AGENTS.md is a token unavailable for conversation. Check your allocation:

/status

This shows remaining context, loaded AGENTS.md files, and active MCP servers.

TechniqueImpact
Remove redundant guidance (things the model already knows)High
Split into nested files so only relevant layers loadMedium
Remove examples and keep only rulesMedium
Reduce fallback filenames to avoid loading extra filesLow

Each MCP server adds tool schemas to context. Disable ones you are not using:

[mcp_servers.unused-server]
enabled = false

If your guidance is genuinely needed and keeps getting truncated:

project_doc_max_bytes = 65536 # 64 KB (default: 32 KB)

But this is a last resort. Prefer splitting into nested files first.

A skill is a directory with a SKILL.md file:

my-skill/
SKILL.md # Required: instructions + metadata
scripts/ # Optional: executable code
references/ # Optional: documentation
assets/ # Optional: templates, resources
agents/
openai.yaml # Optional: UI appearance, MCP dependencies
---
name: pr-ready
description: Prepare the current changes for a pull request by running
all checks, fixing issues, and generating a PR description.
---
# PR Readiness Check
1. Run pnpm lint and fix any issues automatically
2. Run pnpm test and fix any failures
3. Run pnpm type-check and fix any type errors
4. Generate a PR description with:
- Summary of changes (what and why)
- Testing approach
- Breaking changes (if any)
5. Report the results with a pass/fail summary

Skills can be triggered two ways:

  • Explicit: Type $pr-ready or use /skills in the CLI/App
  • Implicit: Codex matches your prompt to a skill’s description automatically

Write descriptions with clear scope boundaries so implicit matching works reliably.

ScopeLocationUse Case
Repository.agents/skills/ (any dir up to repo root)Team-shared skills
User~/.agents/skills/Personal skills for all repos
Admin/etc/codex/skills/Organization-wide skills
SystemBundled with CodexBuilt-in skills like $skill-creator

Use the built-in skill creator:

$skill-creator

Describe what the skill should do, and Codex generates the SKILL.md with proper metadata and instructions.

$skill-installer install the linear skill from the .experimental folder

Or install from the skills marketplace using the universal CLI:

Terminal window
npx skills add openai/skills
~/.codex/config.toml
[[skills.config]]
path = "/path/to/skill/SKILL.md"
enabled = false

If your team already uses a different filename for project instructions:

~/.codex/config.toml
project_doc_fallback_filenames = ["TEAM_GUIDE.md", ".agents.md", "CODEX.md"]

Codex checks each directory in this order: AGENTS.override.md, AGENTS.md, then each fallback name. This lets you adopt Codex without renaming existing documentation.

Terminal window
codex --ask-for-approval never "Summarize the current instructions and list all instruction files you loaded."

Codex will echo back the guidance in precedence order.

An AGENTS.override.md higher in the directory tree can silently replace the regular AGENTS.md. Search for overrides:

Terminal window
find . -name "AGENTS.override.md" -type f

Codex uses .git as the project root marker by default. If your workspace has an unusual structure:

project_root_markers = [".git", ".hg", ".sl"]

Or disable parent-directory scanning entirely:

project_root_markers = []
  • Agent ignores instructions: The guidance may have been compacted away in a long session. Repeat critical constraints in your current prompt, or start a fresh thread.
  • AGENTS.md not loading: Verify the file is not empty. Check that project_doc_max_bytes has not been exceeded. Run the verification prompt above.
  • Override file causes confusion: Search for AGENTS.override.md files you may have forgotten about. Delete temporary overrides when their purpose is complete.
  • Skills not appearing: Skills must be in .agents/skills/ directories. Codex scans from the current directory up to the repository root. Restart Codex if a newly added skill does not appear.
  • Cloud tasks ignore conventions: Cloud environments use the AGENTS.md committed to the repository, not your local files. Ensure critical guidance is committed.