Skip to content
Web Development6 min read

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.

01

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.

02

What coding agents actually need to know

KnowledgeWhat to provideWhere it lives
ArchitectureMain components, boundaries, data flow, key decisionsShort architecture doc; decision records
Repository structureWhere things are; generated vs hand-written codeRoot instruction file
CommandsInstall, build, test one area, lint, type-check, run locallyRoot instruction file
Coding conventionsLanguage rules, error handling, naming, patterns to avoidRoot or scoped instruction files; linters
DependenciesApproved libraries; rules for adding new onesInstruction file; dependency policy
Product requirementsThe goal, acceptance criteria, out-of-scope itemsTask spec or issue
Test strategyWhich tests to add, where, how to run them, what not to mockInstruction file; testing guide
Security rulesAuth and data access libraries, secrets, protected pathsInstruction file; policy
Deployment environmentRuntimes, environment variables, feature flags, constraintsDocs; config files
Database structureSchema, migration rules, what never to changeSchema files; migration guide
Existing patternsOne or two reference implementations to imitateLinks in the spec or instruction file
03

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.

04

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.

Coding agent context hierarchy (diagram)
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 results
05

Repository 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.

06

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.

Root instruction file (illustrative)
# 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 PR
07

Task-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.

08

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
09

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.

10

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.

Start a Project
11

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.

FAQ

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.

Get in touch

Have a project in mind?

Whether you're building a new digital product, improving an existing website, or looking to automate part of your business — let's talk.