AI coding

How to Write a Project Context File for AI Coding Agents

Antonio Evans7 min read

A developer pins a large map of the project, drawn with simple icons and arrows, onto a wall while a small robot studies it before starting work.
Key points

Key points

  1. 1A project context file is a plain Markdown file at the root of a repository that a coding agent reads at the start of every session, so it should hold only what the agent needs on most tasks.
  2. 2The most valuable content is the commands to build, test and lint, the layout of the code, the conventions a linter cannot check, and the hard constraints the agent must never cross.
  3. 3Instructions work when they are concrete enough to verify, such as "Run npm test before committing", and fail when they are general advice such as "write clean code".
  4. 4Treat the file as living documentation: add a rule when the agent repeats a mistake, delete rules that went stale, and move narrow guidance into scoped rules or skills.

What a project context file is

Every coding agent session starts with an empty memory. The agent knows programming in general, but it does not know that your team uses pnpm, that the API handlers live in one folder, or that the staging database must never be migrated by hand. A project context file closes that gap. It is a Markdown file at the root of the repository that the agent loads before it does anything else.

The file goes by several names. AGENTS.md is an open format that came out of a shared effort between several agent makers and is stewarded by the Agentic AI Foundation under the Linux Foundation, according to agents.md. The same page describes it as a complement to the README: the README is for humans, and AGENTS.md holds "the extra, sometimes detailed context coding agents need: build steps, tests, and conventions." Claude Code reads CLAUDE.md, and its memory documentation explains that it can also read a repository's AGENTS.md, or pull it into a CLAUDE.md with an @AGENTS.md import line. Cursor keeps scoped rules as .mdc files under .cursor/rules and also accepts a plain AGENTS.md, per the Cursor rules documentation.

If your team uses more than one agent, write one AGENTS.md and point every tool at it. Three separate files for three tools will drift apart within weeks.

A context file is guidance, not enforcement. Claude Code's documentation says the agent treats these files "as context, not enforced configuration," and that blocking an action regardless of what the model decides requires a hook or a permission setting (Claude Code memory docs). Keep that split in mind from the start: the file shapes behavior, and tooling enforces it.

What to put in it

Two researchers at the University of California, Irvine read the rule files in 401 open-source repositories and sorted their content into five themes: conventions, guidelines, project information, LLM directives and examples (Jiang and Nam, arXiv). Guidelines, project information and conventions each appeared in more than eight in ten of those repositories. That matches what the tool makers recommend. A useful file answers six questions.

SectionQuestion it answersExample line
OverviewWhat is this project and who uses it?"Billing service for the web app. Handles Stripe webhooks and invoices."
CommandsHow do I install, build, test and lint?"Run pnpm test --filter api for the API package."
LayoutWhere does each kind of code live?"HTTP handlers live in src/api/handlers/."
ConventionsWhat does the team do that a linter does not check?"Use pathlib, not os.path."
ConstraintsWhat must never happen?"Never edit generated files in src/gen/."
DoneHow does the agent prove the work is finished?"All tests and pnpm lint pass before you report done."

The commands section matters most. The agents.md FAQ notes that agents will try to run the testing commands you list and fix failures before finishing. If you list no commands, the agent guesses, and a guessed command that runs the wrong test suite gives false confidence.

The constraints section matters second. Real Python's AGENTS.md guide describes constraints as "the lines the agent must never cross, even when crossing one would make a test pass or finish a task faster." Write them short and blunt. Typical constraints are no added dependencies without asking, no changes to public interfaces, no blocking I/O in async code, and no direct writes to production.

The layout section saves search time. An agent that knows where things live goes straight to the right file instead of scanning the tree. Real Python's guide frames this as the principle of least privilege applied to agents: tell the agent which files it needs for the task and which to leave alone.

What to leave out

A long file is a worse file. Anthropic's engineering team describes context as a finite resource with diminishing returns, because a model's ability to recall information drops as the number of tokens grows, an effect called context rot (Anthropic). Their guidance is to find "the smallest possible set of high-signal tokens" that produces the outcome you want. Every line in a context file loads on every task, so every line has to earn its place.

Cursor's documentation lists four things to avoid (Cursor):

  • Copying entire style guides. Use a linter instead.
  • Documenting every possible command. The agent already knows npm, git and pytest.
  • Adding instructions for edge cases that rarely apply.
  • Duplicating code that already exists in the repository. Point to a canonical example file instead.

Add two more. Leave out abstract principles with no behavior attached. Packmind's review of real context files found that listing "SOLID, KISS, YAGNI" in a heading has almost no effect on output, because the model already knows the acronyms and gets nothing concrete to act on (Packmind). And leave out secrets. The file is committed to version control and read by every tool that opens the repository.

How to write instructions an agent will follow

The most useful test for each line is whether someone could check that it was followed. Claude Code's documentation gives three pairs that show the difference (Claude Code memory docs):

  • "Use 2-space indentation" instead of "Format code properly".
  • "Run npm test before committing" instead of "Test your changes".
  • "API handlers live in src/api/handlers/" instead of "Keep files organized".

Four more habits raise adherence:

  1. Use headings and bullets. The same documentation says organized sections are easier for the agent to follow than dense paragraphs.
  2. Remove contradictions. If two instructions conflict, the agent may pick one arbitrarily. This often happens when a root file and a nested file disagree.
  3. Give the reason for anything surprising. "Do not use the ORM for reports, because the report queries need hand-tuned SQL" lets the agent handle a case the rule did not name.
  4. Write at the right altitude. Anthropic describes two failure modes: brittle if-else logic that tries to script every step, and vague guidance that assumes shared context (Anthropic). Aim between them. State the rule and the reason, then let the agent handle the details.

Birgitta Böckeler, writing on Martin Fowler's site, separates instructions, which tell the agent to do a task a certain way, from guidance, which states conventions that apply everywhere (martinfowler.com). A project context file should be almost all guidance. Step-by-step instructions for one kind of task belong in a skill or a saved prompt that loads only when that task comes up.

Structure for large repositories

One file at the root works for a single application. A monorepo needs more than one.

Nested files. The agents.md format supports one file per package. Agents read the nearest file in the directory tree, so the closest one takes precedence. As read on 2026-10-17, the agents.md site said the main OpenAI repository held 88 AGENTS.md files (agents.md). Put repository-wide rules at the root and package-specific commands and conventions next to each package.

Path-scoped rules. Claude Code supports rule files under .claude/rules/ with a paths field, so a rule about React components loads only when the agent works on matching files (Claude Code memory docs). Cursor rules do the same with globs and offer four modes: always apply, apply intelligently based on a description, apply to specific files, and apply manually when mentioned (Cursor).

Imports. A CLAUDE.md can pull in another file with @path/to/file. Imports keep a long file organized, but Claude Code's documentation points out that imported files also load at launch, so they do not reduce the context cost.

Size targets differ by tool. As read on 2026-10-17, Claude Code's documentation recommended keeping each CLAUDE.md under 200 lines, and Cursor's recommended keeping each rule under 500 lines. Treat both as ceilings, not goals.

A template you can copy

The template below follows the sections above. Replace every line with facts about your project and delete any section that does not apply.

# AGENTS.md

## Project
Order management API for the web shop. TypeScript, Node, Postgres.
Customers never call this service directly; the storefront does.

## Commands
- Install: `pnpm install`
- Test one package: `pnpm test --filter <package>`
- Lint and typecheck: `pnpm lint && pnpm typecheck`
- Local database: `docker compose up db`, then `pnpm dev`

## Layout
- `src/api/handlers/`: HTTP handlers, one file per resource
- `src/domain/`: business rules; no database or HTTP code here
- `src/db/migrations/`: SQL migrations, append only
- `src/gen/`: generated client code; never edit by hand

## Conventions
- Money is integer cents. Never use floats for amounts.
- Endpoints follow the pattern in `src/api/handlers/orders.ts`.
- Errors return `{ error: { code, message } }` with a 4xx or 5xx status.

## Constraints
- Do not add a dependency without asking.
- Do not change a migration that has already been merged.
- Do not commit to `main` or push. Stage changes for review.

## Done means
- Tests, lint and typecheck pass.
- Changed behavior has a test.
- The summary lists every file changed.

Notice what is missing: no paragraph about clean code, no list of common git commands, no copy of the style guide. Every line describes something specific to this repository that the agent could not guess.

Keeping the file accurate

A context file decays like any documentation. A renamed folder or a retired command turns a helpful line into a misleading one, and an agent will follow a stale instruction with full confidence.

Three habits keep the file accurate:

  1. Grow it from mistakes. Cursor's guidance is to start simple and add a rule only when the agent makes the same mistake repeatedly (Cursor). Böckeler gives the same advice: build rules files up gradually, because what a model needed months ago may no longer be necessary (martinfowler.com).
  2. Review it like code. Commit the file, change it through pull requests, and let the whole team see each change. Packmind recommends making context review a normal part of code review (Packmind).
  3. Audit it on a schedule. Check that every path and command in the file still exists. Claude Code's documentation describes a prompt audit that looks for references to missing files and for instructions that contradict each other (Claude Code memory docs).

When a section grows past a screen and only applies to some tasks, move it out. Narrow, task-specific guidance works better as a skill that loads on demand. Quanta Intelligence publishes skills and playbooks in that format, and the same rule applies to any you write yourself: the always-loaded file is for what matters on nearly every task.

FAQ

FAQ

Should I use AGENTS.md or CLAUDE.md?

Use AGENTS.md if more than one agent works in the repository, because many tools read it. Claude Code can read AGENTS.md directly or import it from a one-line CLAUDE.md. Keep tool-specific content in the tool's own file and shared content in AGENTS.md.

How long should a project context file be?

It should be as short as it can be while still covering commands, layout, conventions and constraints. Claude Code's documentation, read on 2026-10-17, suggested under 200 lines per file and warned that longer files reduce adherence. If you are near that limit, move narrow rules into path-scoped rule files.

Can the agent write the file for me?

Yes, as a first draft. Claude Code's /init command scans the codebase and writes a starting file, and the agents.md site notes that most agents can scaffold one if you ask. Review the draft line by line, because the agent cannot know your team's unwritten rules.

Will the agent always obey the file?

No. The file is context that the model weighs, not a rule that the tool enforces. For anything that must never happen, such as writing to a protected path, use permission settings, hooks or CI checks.

Where do personal preferences go?

They go in a personal file that is not committed. Claude Code supports a user-level file for all projects and a CLAUDE.local.md for one project, which you add to .gitignore. The shared file should only hold rules the whole team agrees on.

Sources

Sources

  1. https://agents.md/
  2. https://code.claude.com/docs/en/memory
  3. https://cursor.com/docs/rules
  4. https://arxiv.org/html/2512.18925v1
  5. https://realpython.com/agents-md/
  6. https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
  7. https://packmind.com/context-engineering-ai-coding/context-engineering-best-practices/
  8. https://martinfowler.com/articles/exploring-gen-ai/context-engineering-coding-agents.html
About the author

About the author

Antonio Evans
Antonio Evans

Antonio Evans is the founder of Quanta Intelligence and a fractional CTO who builds HIPAA-ready AI systems for health and life-science companies. Before that he was technical co-founder of FriendsEAT for twenty years, CEO of Socialdraft, and a data engineer at IBM. Antonio lives with his dog Santino in Lisbon, Portugal.

All articles