What problem it solves
Without instructions, every new agent session starts the same way: explaining how to build the project, where tests live, which folders to leave untouched, and how to format commits. The first impulse is to dump all of it into one big file at the repository root.
Once that file crosses 300–500 lines, three things break. First, the entire file enters context on every turn: the agent tweaks one UI button while carrying database migration rules, release checklists, and SQL conventions in memory (Long conversations). Second, model attention thins out: “don’t touch production configs” drowns among a hundred minor notes, and the agent starts skipping items in the middle. Third, a written ban is still just a request — the model can work around it or lose it after history compaction.
One file for everything
- 600–1,000 lines in context before the first question.
- Rare procedures and unrelated module details distract from routine edits.
- Hard bans, code style, and build commands piled into one list.
- Switching editors or CLIs breaks the setup.
Five layers by role
- A short entry file at the root, under 200 lines.
- Folder rules load only when touching files in that folder.
- Step-by-step procedures open only when the task matches their description.
- Hard bans and checks are enforced by the agent program, not the model.
How context gets assembled before a reply
An agent doesn’t read the whole repository or load every setting at once. Instructions enter context in stages as the task unfolds and files open:
open project and state a task
- Session start
AGENTS.md · MEMORY.mdGlobal user rules, the repository entry file, and the auto memory index enter context. This is the fixed baseline on every turn. - Skill map
name + descriptionOnly the YAML frontmatter of eachSKILL.mdis read — roughly a hundred tokens per skill. The body stays out of context for now. - Files in progress
paths / globsThe agent opens a file, saysrc/api/orders.ts. The program finds rules with a matching pattern and loads them into context. - Skill on demand
SKILL.md bodyThe task matches a skill’s description, so the agent reads the full steps — plus any scripts or templates referenced inside the skill folder. - Program hook
PreToolUse · PostToolUseBefore or after a tool call, the agent program runs a configured command on its own. The model cannot override it.
reply and tool calls
With on-demand loading (called progressive disclosure in the Agent Skills spec), a repository can hold dozens of rules and skills without bloating every request.
Five configuration layers
Entry file
AGENTS.md / CLAUDE.mdA short project reference: what the repo does, build and test commands, top bans, and a “task to document” routing table. Written by a human, tracked in git. Target length: under 200 lines.
Path-scoped rules
.claude/rules · .agents/rulesNarrow rules for one slice of the repo: API, database migrations, frontend layout, docs. A path glob in the header loads the rule only when the agent reads or edits matching files.
Skills
SKILL.md · agentskills.ioA folder with SKILL.md and optional scripts/, references/, assets/. Captures a recurring multi-step
procedure: cutting a release, adding an endpoint, triaging logs.
Memory
MEMORY.mdNotes the agent saves for itself across sessions: user corrections, local environment quirks, settled conventions. It does not replace the entry file and needs periodic cleanup.
Hooks and permissions
settings.jsonCommands and allow/deny lists tied to lifecycle events: before a tool runs (PreToolUse), after a file edit
(PostToolUse), on session start. The only layer enforced by code rather than model judgment.
Advice for the model vs. a program-level block
The main boundary sits between the first four layers and the fifth.
Everything in AGENTS.md, path rules, skills, and memory reaches the model as text in context. The model
follows it most of the time, with no guarantee: in a long chat, after history compaction, or when two rules collide,
the model may skip a written ban. Telling the model to use two-space indents in AGENTS.md burns tokens and still
slips, whereas a formatter or linter checks code in milliseconds without involving the model.
Hooks and permission lists are executed by the agent program itself, not the model. When the model emits a
tool_use call (Agents and tools), the program checks the command against its deny list and runs the PreToolUse
hook first. If the hook returns an error or the command is denied, the tool never runs regardless of what the model
decided.
Where each rule belongs
| If you need to… | Put it in |
|---|---|
| define build, test, and lint commands plus 5–10 top project constraints | AGENTS.md (under 200 lines) |
set rules for one directory — src/api/**, migrations/**, docs/** |
a path-scoped rule with paths / globs |
| document a recurring 5+ step workflow — release, review, module scaffold | a skill (SKILL.md + scripts alongside) |
| enforce indentation, imports, types, and code style | a linter, formatter, and PostToolUse hook — not a prose rule |
| guarantee a command or file read is blocked | deny permissions and a PreToolUse hook |
| save a minor environment quirk the agent discovered mid-session | memory (MEMORY.md); if it’s a project rule, move it to AGENTS.md |
A personal layer sits outside git: in Claude Code, ~/.claude/CLAUDE.md (all projects on the machine) and
CLAUDE.local.md at the repo root (this machine only, in .gitignore); in Codex CLI, ~/.codex/AGENTS.md; in
Antigravity, ~/.gemini/GEMINI.md. Put personal habits there (reply language, report format, local paths) so they
don’t end up in the shared team repository.
A compact AGENTS.md, block by block
A good entry file reads like a table of contents, not a textbook. Anything needed for only one task lives in its
own doc, and AGENTS.md points there:
# order-api
TypeScript HTTP order service (Node 24, Fastify, PostgreSQL).
Handles order creation and statuses; payments live in the sibling billing service.Two or three lines of context: what the project is, its stack, and where its boundary ends. No project history.
## Task routing
| Task | Read |
|---|---|
| New endpoint or schema | `.claude/rules/api.md` |
| Database migration | `db-migration` skill |
| Version release | `release` skill |
| Image build and deploy | `deploy/README.md` |The main trick that keeps the file short: the agent reads detailed instructions only for the current task, not on every startup.
## Commands
- `npm test` — unit tests (run after every edit in `src/`)
- `npm run typecheck` — type check (`tsc --noEmit`)
- `npm run lint` — ESLint and formatting checkExact verification commands so the agent doesn’t guess from package.json or run extra scripts.
## Constraints
- Never edit existing files in `migrations/` — add a new migration file,
or rollbacks on the live server break.
- Never log tokens or `email`, `phone` fields in API logs or error replies.
- Check for a clean `git status` before starting a refactor.Only hard project constraints where mistakes are costly. State the reason briefly right beside each rule so the model bypasses it less often (A rule in a prompt gets a reason instead of caps).
If a script can check a rule, add that check to npm run lint or a test and leave a single line with the command
name in AGENTS.md. Code-enforced rules never go stale and don’t depend on context length.
One project, several agents
Teams often touch the same repository from different tools: Claude Code, Cursor, Codex CLI, Antigravity, Gemini CLI. Duplicating rule text across three files guarantees drift within a week: you update one copy and forget the rest.
Keep each rule’s text in one place and leave thin pointers for tools that expect different paths.
| Tool | Entry file | Path-scoped rules | Skills |
|---|---|---|---|
| Codex CLI | AGENTS.md |
nested AGENTS.md in subfolders |
.agents/skills/ |
| Cursor | AGENTS.md |
.cursor/rules/*.mdc (globs:) |
.cursor/skills/ and .agents/skills/ |
| Antigravity | AGENTS.md, GEMINI.md |
.agents/rules/*.md (trigger: glob, globs:) |
.agents/skills/ or paths in .agents/skills.json |
| Claude Code | CLAUDE.md (@AGENTS.md) |
.claude/rules/*.md (paths:) |
.claude/skills/ |
Claude Code looks for CLAUDE.md by default, and supports @path imports. Keep the actual text in AGENTS.md
and put one line in CLAUDE.md:
@AGENTS.md
Skills already share the SKILL.md format (the agentskills.io standard): YAML frontmatter with name (matching
the folder name) and description, followed by Markdown steps. When a tool looks for skills in another directory,
point its config at the shared folder (for example, {"entries": [{"path": ".claude/skills"}]} in
.agents/skills.json) instead of copying files.
project/
├── AGENTS.md # single entry file for all agents
├── CLAUDE.md # one line: @AGENTS.md
├── .claude/
│ ├── rules/api.md # paths: ["src/api/**/*.ts"]
│ ├── skills/release/SKILL.md
│ └── settings.json # Claude Code permissions and hooks
└── .agents/
├── rules/api.md # pointer to .claude/rules/api.md
└── skills.json # mounts .claude/skills without copying
Path rules and hooks don’t have a single shared standard yet: glob formats and event names vary across tools. For
rules, use a short pointer file (“before working on these files, read .claude/rules/api.md”) plus a small test
verifying that globs in both folders stay in sync. Configure hooks and permission lists separately per tool.
What you get and what it costs
What you get
- Repeatable starts. Every fresh chat knows your test commands, layout, and bans without re-explaining the repo each time.
- Lean context. Requests carry only the baseline, rules for open files, and the active skill; the rest burns no tokens and causes no distraction (Prompt cache).
- Hard boundaries. Bans and checks in hooks and permissions trigger at the program level even when the model slips up.
- Portability across agents. One
AGENTS.mdandSKILL.mdskills avoid lock-in to a single tool.
What it costs
- Instruction drift. Rename a command or folder without updating
AGENTS.md, and the agent follows the old description until it hits an error. - Fragmented rule and hook formats. Skills and
AGENTS.mdconverged on shared standards, while path globs and hooks still differ per client. - False confidence in prose bans. As long as a ban exists only in Markdown and not in permissions or a hook, it stays advisory.
- Overkill for small scripts. A one-evening script needs none of these five layers — a single prompt and a clean
git statusare enough (Clean git status before the agent).
Where to start
Don’t build all five layers on day one — add each layer when real repetition shows up:
- Write a 40–80 line
AGENTS.md: what the project is, run and test commands, and top constraints. If you use Claude Code, putCLAUDE.mdnext to it with@AGENTS.md. - Work through several sessions and note which corrections you keep repeating for the same directory — move those into a path-scoped rule.
- Turn any 5+ step workflow you run regularly (release, migration, new module) into a
SKILL.mdskill with a sharpdescriptionstating when to open it. - Move everything a linter, test, or hard command ban can check into hooks and permissions, and delete the duplicate
lines from
AGENTS.md.
If it doesn’t work
The agent breaks a rule written right inside AGENTS.md
- Cause
- The file grew to hundreds of lines, burying the rule in the middle or clashing with another item. A text instruction is guidance for the model, not a hard block.
- Fix
- Trim
AGENTS.mdunder 200 lines, move details into path-scoped rules, and move critical bans and code style into a linter,denypermissions, or aPreToolUsehook.
A skill sits in its folder, yet the agent never invokes it
- Cause
- The YAML header lacks required
nameordescriptionfields, the name doesn’t match the folder, or the description is too vague to match how the task is phrased. - Fix
- Check the
SKILL.mdheader and write an exact trigger condition indescription: which task and which files should open this skill.
A path-scoped rule doesn’t load when editing a matching file
- Cause
- A bug in the
pathsorglobspattern: tools match relative and full paths differently, and some engines don’t expand brace expressions like*.{ts,css}. - Fix
- Check pattern syntax against the agent’s docs, list explicit comma-separated patterns for portability, and inspect active rules in a fresh chat.
In another editor or CLI, the agent doesn’t see project instructions
- Cause
- Instructions live in a single tool’s file (
CLAUDE.mdor.cursorrules) that other programs ignore. - Fix
- Put the main text in
AGENTS.mdat the repository root and leave a single line@AGENTS.mdinsideCLAUDE.md.
The agent repeats an old decision you already abandoned
- Cause
- A note from a past session stayed in auto memory (
MEMORY.md) and loads on every startup. - Fix
- Open
MEMORY.md, delete stale entries, and move permanent agreements into git-trackedAGENTS.md.
After adding rules and skills, sessions hit context limits faster
- Cause
- Rules without path globs or long reference docs load on every start and go out with every request.
- Fix
- Inspect what’s in context (for example,
/contextin Claude Code), add path patterns to narrow rules, and move rare procedures into skills.