AI Coding Agent Context: How to Give Coding Agents the Right Project Knowledge
What coding agents need to know about your project, how AGENTS.md, CLAUDE.md, rules and skills fit together, and why more context is not better context.
Quick answer
AI coding agents produce better changes when they know how your project works: its architecture, repository structure, commands, conventions, dependencies, test strategy, security rules, deployment environment, database structure and the existing patterns to follow, plus the requirement for the task at hand.
But more context is not better context. Keep always-loaded instructions short and accurate, scope rules to the parts of the codebase they apply to, give each task its own spec and let the agent read code and docs on demand. Context that is stale or contradicts the code is worse than none.
What coding agents actually need to know
| Knowledge | What to provide | Where it lives |
|---|---|---|
| Architecture | Main components, boundaries, data flow, key decisions | Short architecture doc; decision records |
| Repository structure | Where things are; generated vs hand-written code | Root instruction file |
| Commands | Install, build, test one area, lint, type-check, run locally | Root instruction file |
| Coding conventions | Language rules, error handling, naming, patterns to avoid | Root or scoped instruction files; linters |
| Dependencies | Approved libraries; rules for adding new ones | Instruction file; dependency policy |
| Product requirements | The goal, acceptance criteria, out-of-scope items | Task spec or issue |
| Test strategy | Which tests to add, where, how to run them, what not to mock | Instruction file; testing guide |
| Security rules | Auth and data access libraries, secrets, protected paths | Instruction file; policy |
| Deployment environment | Runtimes, environment variables, feature flags, constraints | Docs; config files |
| Database structure | Schema, migration rules, what never to change | Schema files; migration guide |
| Existing patterns | One or two reference implementations to imitate | Links in the spec or instruction file |
More context is not better context
Models have a limited attention budget. Anthropic's engineering guidance on context engineering makes the point that useful context is the smallest set of high-signal information that gets the job done, and that recall degrades as context grows (Anthropic on context engineering). For coding agents this means a 2,000-line instruction file is a liability: rules get ignored, contradictions creep in and the agent spends tokens on things irrelevant to the task.
Three kinds of bad context cause most problems: generic advice ('write clean code') that adds nothing; stale instructions that describe commands or patterns the code no longer uses; and contradictory rules across files. When an agent repeatedly does the wrong thing, check the context before blaming the model. The general principles are in our guide to context engineering.
The context hierarchy
Organize coding agent context in layers, from always-on to on-demand. Each layer should be as small as it can be.
ALWAYS LOADED (keep short)
org / user rules ............ personal or company defaults
root instruction file ....... AGENTS.md / CLAUDE.md /
.github/copilot-instructions.md
│
SCOPED (loaded for matching paths)
subdirectory files .......... packages/api/AGENTS.md
path rules .................. *.instructions.md (applyTo)
│
PER TASK
spec / issue ................ goal, acceptance criteria,
files likely affected, examples
│
ON DEMAND (agent fetches when relevant)
skills / playbooks .......... "how we write migrations"
code search, docs, schema ... read when needed
tool results ................ test output, logs, CI resultsRepository documentation formats
AGENTS.md is an open, plain Markdown format for agent instructions, read by many agents including OpenAI Codex, Cursor and GitHub Copilot; nested files in subdirectories can scope instructions to a package (agents.md). CLAUDE.md is Claude Code's equivalent; it can import or point to an existing AGENTS.md so you keep one source. GitHub Copilot reads repository-wide instructions from .github/copilot-instructions.md and path-specific instructions from .github/instructions/*.instructions.md files with an applyTo pattern, and also supports AGENTS.md (GitHub Copilot custom instructions). Cursor supports project rules as well.
Skills package procedures an agent loads only when relevant: a folder with a SKILL.md file of instructions plus optional scripts and references. Agent Skills started at Anthropic and is now published as an open format supported by several agents (agentskills.io). Skills suit recurring procedures ('add a database migration', 'release a mobile build') that would bloat the root file.
Pro tip
If your team uses several agents, keep one canonical AGENTS.md and make tool-specific files point to it. Duplicated instructions drift apart within weeks.
What goes in the root file, and what does not
A good root file fits on a screen or two. It contains commands, the project map, the non-obvious conventions, boundaries (generated code, protected paths, migrations already merged) and security rules. It does not contain the whole architecture, style rules your linter already enforces, or task-specific details.
# Agent instructions
## Commands
- Install: pnpm i
- Test one package: pnpm --filter api test
- Lint + types: pnpm lint && pnpm typecheck
## Map
- apps/web: Next.js storefront (App Router)
- packages/api: REST API; handlers in src/routes
- packages/db: schema + migrations (Drizzle)
## Conventions
- Money in integer minor units; never floats
- Validate input with zod schemas in packages/api/src/schemas
- Follow packages/api/src/routes/orders.ts as the reference handler
## Boundaries
- Never edit src/generated/ or merged migrations
- Auth, payments (packages/api/src/{auth,payments}): human-led changes
- No new dependencies without noting why in the PR
## Before finishing
- Run tests for touched packages; include output in the PRTask-specific context
Most of the value comes from the task, not the root file. A good task brief states the goal, acceptance criteria, what is out of scope, the files or modules likely involved, one reference implementation to imitate and how to verify the change. Spec-driven development formalizes this into spec, plan and tasks that the agent and reviewer both work from. For larger work, ask the agent to explore and propose a plan before writing code, and correct its understanding early.
Context selection and freshness
Let agents select context rather than pre-loading it. Capable agents search the codebase, read relevant files and run commands; give them good entry points instead of pasting files into the prompt. Keep context fresh by updating instruction files in the same pull request that changes a command, convention or structure, and by adding a rule whenever an agent repeats a mistake that a sentence would prevent. Review these files periodically and delete rules that no longer apply.
- Root file: commands, map, conventions, boundaries, security rules
- Scoped files for packages or paths with different rules
- One reference implementation per common pattern
- Specs for non-trivial tasks, stored with the code
- Skills for recurring multi-step procedures
- Linters and type checks for rules that can be enforced mechanically
- Instruction changes reviewed like code changes
- A periodic prune of stale or duplicated rules
Security and context
Context is also an attack surface. Instruction files, issues, documentation and dependency READMEs are all text the agent may follow, so treat external content as untrusted and keep secrets out of anything agents read. Put security rules (approved auth and data-access libraries, no hard-coded secrets, protected directories) in the root file, and enforce the important ones with permissions and CI rather than instructions alone. See securing AI coding agents and AI-generated code security.
Common mistakes
Writing a long, generic instruction file once and never updating it. Maintaining separate, conflicting files for each agent tool. Describing conventions the linter could enforce. Omitting test commands, so agents guess. Giving no reference implementations, so agents invent new patterns. And blaming the model for mistakes that a single accurate sentence of context would have prevented.
Preparing a codebase for coding agents?
ZSpace Labs sets up repositories, instructions, specs and CI so teams get consistent results from coding agents. See full-stack development.
Conclusion
Coding agents are only as consistent as the project knowledge they receive. Provide the essentials in short, accurate, versioned files; scope rules to where they apply; put the real detail in task specs; let agents fetch everything else on demand; and keep it all fresh as the code changes. Better context, not more context, is what turns agent output into code your team would have written.
Common questions.
The project's architecture, repository structure, commands to build and test, coding conventions, dependencies and approved libraries, the requirement and acceptance criteria for the task, test strategy, security rules, deployment environment, database structure and examples of existing patterns to follow.